You can become a serverless blackbelt. Enrol to my 4-week online workshop Production-Ready Serverless and gain hands-on experience building something from scratch using serverless technologies. At the end of the workshop, you should have a broader view of the challenges you will face as your serverless architecture matures and expands. You should also have a firm grasp on when serverless is a good fit for your system as well as common pitfalls you need to avoid. Sign up now and get 15% discount with the code yanprs15!
Do you remember that scene from the movie 21 where Kevin Spacey gave our protagonist Ben the game show host problem?
Marilyn’s answer caused quite a bit of controversy at the time and whilst I don’t understand the formal mathematical proof, it’s a problem that can be easily proved through simulations.
Simulation with F#
First, let’s set up a few types to represent the problem domain:
Pretty self-explanatory here:
- behind every Door is a Prize;
- the Prize is either a Car or a Goat;
- you Win if you get the Car in the end, otherwise you Lose
When you start a new game you always have three doors, arranged in some random order.
After you had given your initial choice, the game show host will reveal one of the doors that has a Goat?.
Here we have a function that takes in the doors and the player’s initial choice; and return the index of a door that:
- the player didn’t choose; and
- has a goat
Putting it together, we have a function that takes in the doors as well as the player’s strategy and return whether the player Win or Lose at the end of the game.
From the code above you can guess the type signature of the strategy function to be int –> int –> int. That is, it takes two integers – the player’s initial choice and the index of the door the host has revealed – and return the player’s final choice.
Now, given the two choices we have – to stay or to change, we can represent it with two strategies:
- strategyA would stay with the player’s original choice;
- strategyB would change
We can now use these two functions to call play with.
p.s. notice we have essentially implemented the Strategy pattern, but without all the boilerplates and interfaces and classes? With just functions! Isn’t that sweet?
To be kinder to ourselves, let’s add another helper function that will take in our chosen strategy and run a simulation of 1000 games, and return the number of games that we have won with this strategy:
Finally, let’s run our simulations and see how the two strategies perform.
Running this simulation over and over, strategyB consistently outperforms strategyA by roughly 2-to-1, which is exactly what we expected.
You can also just run the simulation yourself on .Net Fiddle here, just click the Run button at the top.
Improving our code
Whilst the above is sufficient for its purpose and it’s a pretty short solution, there’re couple of things that can be improved in hindsight:
- player’s choice – to Stay, or Change – is not explicitly represented in the domain;
- whilst a Change can only lead to one outcome – changing to the door that isn’t revealed nor initially chosen – it is expressed implicitly by the logic in strategyB;
- if we were to introduce another strategy (e.g. decide to stay or change randomly) then we’d have to duplicate the logic for change.
To make these improvements is really easy. We can start by adding a type to present the two choices available to the player:
Then we can update the play function to work with it:
So now, our strategy implementations become much simpler:
Again, you can try out this updated version on .Net Fiddle here. It also includes a ‘random’ strategy which chooses to Stay or Change using the rand function we created earlier.
There are other things we can improve still. For instance, how do we encapsulate the constraint of having only 3 doors as part of our model?
This way, we can eliminate another invalid state using the type system. I’ll leave that as a teaser to you and feel free to share your solution in the comments!
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