Exercises in Programming Style–Introspective

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

Don’t reinvent the patterns. Catalyst gives you consistent APIs for messaging, data, and workflow with key microservice patterns like circuit-breakers and retries for free.

Try the Catalyst beta

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.

exercises-prog-styles-cover

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.

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:

  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:

Style16_02

Style16_03

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:

Style16_01

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

Style16_04

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:

Style16_05

Finally, to string everything together:

Style16_06

 

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

Whenever you’re ready, here are 3 ways I can help you:

  1. 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.
  2. 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.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

2 thoughts on “Exercises in Programming Style–Introspective”

  1. Pingback: F# Weekly #6, 2016 | Sergey Tihon's Blog

  2. Pingback: Exercises in Programming Style–Reflective | theburningmonk.com

Leave a Comment

Your email address will not be published. Required fields are marked *