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 24 – Quarantine
- Core program functions have no side effects of any kind, including IO.
- All IO actions must be contained in computation sequences that are clearly separated from the pure functions.
- All sequences that have IO must be called from the main program.
This style is similar to The One (aka Monads) style we saw earlier on. The notable difference here is that only the side-effecting IO code need to be contained, so we can interpret this as the IO Monad style.
Taking inspiration from the computation expression we built for The One style, we can make some modifications here.
We’ll start by declaring a generic IO<‘a> type that will encapsulate an IO action. Then we can build a computation expression around this wrapper type.
Unfortunately do is already a reserved keyword in F#, so we’ll have to make do with do’ instead (see what I did there? )
Now we can add a couple of helper types and methods to wrap around IO operations – such as reading from a file or printing to console – in our IO<‘a> type.
We need to read from files in order to extract words from Pride and Prejudice and to remove stop words, so both functions use our do’ computation expression. Notice that we’re using the aforementioned File.ReadAllText helper method, which returns an IO<string> so our do’ computation expression can bind to.
Other pure functions can stay as they are.
But, our main program will need to be contained since it needs to perform IO operations via the extractWords and removeStopWords functions.
Note that no work has actually been done yet.
So far, we have merely composed together what actions (IO and otherwise) we will perform when this program is run. To complete the puzzle we need one more thing – a mechanism to execute these side-effecting programs.
Something like this IO module perhaps?
which we can use to execute our mainProgram.
Now, the IO operations (including printing to the console) will be performed along with other pure functions.
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, 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. Enrol now and enjoy a special preorder price of £9.99 (~$13).
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 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