Exploring monads by applying test-driven development
Monads are used in functional programming to build simple components. They are used to chain a series of operations in a safe way. Each component encapsulates a value and makes sure that the next component that is called could handle its output as input. For example, if component A
generates nil
(null
) as output and the next component in the chain cannot handle nil
as input, the computation of the chain will be stopped automatically.
In Haskell, a pure functional programming language, monads are used prominently. Still, they can be useful in other functional programming languages as well. We will take a brief and very high-level look at monads here and skip a lot of complex theory and background information.
We'll create a simple monad that will return a fancy (well, kind of) formatted message. It adds a number of asterisks before and after the passed string. Open the src/exploring-monads/core.clj
file and replace the code with the following...