Check out my new course Learn you some Lambda best practice for great good! and learn the best practices for performance, cost, security, resilience, observability and scalability.
If you enjoy reading these exercises then please buy Crista’s book to support her work.
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:
- evaluating the code snippets above
- capture the results and cast them to corresponding types
- invoke them with the paths to the stop words file and the input file
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 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!
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 wrong about serverless and vendor lock-in
- You are thinking about serverless costs all wrong
- Just how expensive is the full AWS SDK?
- Many faced threats to Serverless security
- We can do better than percentile latencies
- 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
- Top 10 Serverless framework best practices