Exercises in Programming Style–Quarantine

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

 

Style 24 – Quarantine

Constraints

  • 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? )

Style24_01

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.

Style24_02

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.

Style24_03

Other pure functions can stay as they are.

Style24_04

But, our main program will need to be contained since it needs to perform IO operations via the extractWords and removeStopWords functions.

Style24_05

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?

Style24_06

which we can use to execute our mainProgram.

Style24_07

and voila!

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 exer­cise here


 

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

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


1 thought on “Exercises in Programming Style–Quarantine”

  1. Pingback: F# Weekly #10, 2016 – Sergey Tihon's Blog

Leave a Comment

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