This chapter covers
- Free monads and how to use them in domain modeling
- How to apply the functor abstraction
- Some advanced functional concepts
Imagine you’re building a robot from a bunch of preprogrammed modules. Each module can do something specific, like move something forward, turn on the lights, or make a sound. To make the robot do something interesting, you need to create a sequence of actions using these blocks. For different behaviors, you’d rearrange the sequence. Obviously, being able to create and arrange these sequences will make the robot easier to control.
That’s the basic idea behind a free monad. A free monad is a data structure that organizes domain-related actions into interpretable sequences (interpretable scenarios), making it possible to alter those sequences more easily as values. In this chapter, we’ll take a closer look at this concept from functional programming and see how free monads make domain modeling and eDSL design even more powerful and convenient. Free monads play the central role in the rest of the book, serving as functional interfaces that will enhance our programs with good design qualities, such as low coupling, proper subsystems design, and testability.