Yan Cui
I help clients go faster for less using serverless technologies.
NOTE : read the rest of the series, or check out the source code.
If you enjoy reading these exercises then please buy Crista’s book to support her work.
Following 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.
Constraints
- 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.
Whenever you’re ready, here are 3 ways I can help you:
- Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
- I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
- Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.
Pingback: F# Weekly #6, 2016 | Sergey Tihon's Blog
Pingback: Exercises in Programming Style–Reflective | theburningmonk.com