If you enjoy reading these exercises then please buy Crista’s book to support her work.
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:
- inspecting the stack to see who the caller is and only allow the function to be called from ‘extract_words’
- 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 exercise here.
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.
Check out my new 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. Including basic concepts, HTTP and event triggers, activities, callbacks, nested workflows, design patterns and best practices.
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong