11 Second abstract nonsense interlude: Monads
In every programming language enthusiast’s life, there comes a time when they try to explain monads to anyone who will listen, and I’m no exception. Monads are surprisingly simple and common design patterns that enable you to add new capabilities on top of existing types. You’ve undoubtedly used monads many times, quite happily, without realizing that you were doing so. Understanding that a deeper mathematical structure underlies seemingly unrelated types changed the way I approach program construction. Monads in particular are a useful ways to structure programs as workflows of chained operations.
To understand why monads are so powerful, we’ll look at some examples of implementing a trivial operation, divide by 2, on integers that have been granted a special power, such as nullability, laziness, or asynchrony. Then we’ll come up with mechanisms for transforming any function taking any type into an equivalent monadic operation.