Exercises in Programming Style–Introspective

You can become a serverless blackbelt. Enrol in my course Learn you some Lambda best practice for great good! and learn best practices for performance, cost, security, resilience, observability and scalability. By the end of this course, you should be able to make informed decisions on which AWS service to use with Lambda and how to build highly scalable, resilient and cost efficient serverless applications.

NOTE : read the rest of the series, or check out the source code.

If you enjoy read­ing these exer­cises then please buy Crista’s book to sup­port her work.


Fol­low­ing on from the last post, we will look at the Introspective style today.


Style 16 – Introspective

This marks the start of a couple meta-programming related styles which also includes Reflective, Aspects and Plugins.

If you have come from a .Net or Java background then the subtle difference between introspection and reflection might not be obvious. This wikipedia article gives a pretty good explanation:

type introspection is the ability of a program to examine the type or properties of an object at runtime

Introspection should not be confused with reflection, which goes a step further and is the ability for a program to manipulate the values, meta-data, properties and/or functions of an object at runtime.


  • The problem is decomposed using some form of abstraction (procedures, functions, objects, etc.)
  • The abstractions have access to information about themselves and others, although they cannot modify that information


In her example, Crista used two of Python’s introspective capabilities:

  1. inspecting the stack to see who the caller is and only allow the function to be called from ‘extract_words’
  2. fetching the value of the argument passed into the function

both feel a bit contrived, especially 2.

I don’t know of any ways to do 1. in F#, you can walk the stackframe but it gives you filename, line number, method name, etc. but I didn’t find a way to get the calling function name in any usable form. Since I’m doing this exercise in a F# script might have complicated matters further, as I kept seeing things such as ‘FSI_0004’ as type names.

After giving this some thought, I think a more realistic example in our context here is to use objects to hold data as properties and use introspection to fetch their value.


So first, let’s define the two types which we’ll use introspection on:



We’ll use introspection to get at the DataStorage.Words and StopWordsFilter.StopWords properties. But, let’s do that with style and define the dynamic operator to do the heavy lifting for us:


and now we can have a filterWords function that:

  • takes two arguments  – dataStorage and filter
  • dataStorage can be of any type with a string[] property called Words
  • filter can be of any type with a Set<string> property called StopWords


this function has the signature of

‘a -> ‘b -> seq<string>

Perhaps here lies the strength and weakness of this approach:

  • that it’s much more generic than if we had depended upon the concrete types, and would even work with types that I don’t control (and therefore can’t enforce any interfaces upon them)
  • but the constraints on dataStorage and filter are now implicit and you’ll have to inspect my code to figure them out

a much better solution in F# would be to use statically resolved type parameters, but that’ll be out-of-style I think.

Next, we’ll add another function to count the frequency of the filtered words, nothing special here:


Finally, to string everything together:



You can find the source code for this exer­cise here.

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.
Subscribe to my newsletter

Hi, I’m Yan. I’m an AWS Serverless Hero and the author of Production-Ready Serverless.

I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

Hire me.

Check out my new podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.

Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!

Check out my video course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!

Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!

Find a workshop near you