Exercises in Programming Style–Reflective

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

Hookdeck: The Serverless Event Gateway

Hookdeck is a reliable and scalable serverless event gateway for sending, receiving, authenticating, transforming, filtering, and routing events between services in your event-driven architecture.

Learn more

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 Reflective style today.


Style 17 – Reflective

We’re now officially at the halfway mark in this series, I hope you’ve enjoyed the series thus far and today’s style is a funky one!


  • The program has access to information about itself, i.e. introspection (see last post on how it differs from reflection)
  • The program can modify itself – adding more abstractions, variables, etc. at runtime


In her example, Crista used Python’s metaprogramming capabilities to:

  • dynamically inject additional functions based on the arguments passed into the script
  • eval these new functions and capture the return values into the running application

The metaprogramming capabilities of .Net languages have improved significantly since the availability of Roslyn and the F# compiler service, but it’s still nowhere near what one can easily do in Clojure, Ruby, Python and other dynamically typed languages.


First, we need to go and grab the F# Compiler Service package from Nuget.

Then, we need to reference it in our script and follow the tutorial on embedding the F# Interactive (FSI) in our application:


Once we have an instance of FsiEvaluationSession, we’re mostly interested in its EvalExpression method. Before we move on, let’s add another helper function to deal with its result:


Now we can start writing our code as strings, yay! 

Couple of things I noticed whilst experimenting with the following:

  • you can’t open namespaces in the source code
  • you can’t read files in the source code

which is why I used fully qualified names for System.IO.File.ReadAllText, and why it’s only used in composition and not directly invoked in the source code.

Instead, the code that’s captured in the string below will return an extractWords function with the signature

string -> string -> string[]

When we later evaluate it we’ll need to cast the result to that type and invoke the function with the paths to the stop words file and the input file for Pride and Prejudice.


We’ll apply the same approach and create a piece of code that returns a function with the signature :

string[] -> (string * int)[]


and another function that’ll take the word frequencies generated by the code above, sort them and print the top 25 results on screen:


Finally, all the pieces are in place.

Now, we can string everything together (pun intended) by:

  1. evaluating the code snippets above
  2. capture the results and cast them to corresponding types
  3. invoke them with the paths to the stop words file and the input file


and voila!


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

Whenever you’re ready, here are 4 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. Do you want to know how to test serverless architectures with a fast dev & test loop? Check out my latest course, Testing Serverless Architectures and learn the smart way to test serverless.
  3. 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.
  4. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

1 thought on “Exercises in Programming Style–Reflective”

  1. Pingback: F# Weekly #7-#8, 2016 | Sergey Tihon's Blog

Leave a Comment

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