What is a monad?

We already talked about laws earlier in this chapter. The monoid is defined based on some laws it follows, and these laws allow us to implement generic functionality with certainty, just because we expect certain conditions to hold. If a law is broken, then there is no way for us to know for sure what to expect in terms of how something will behave. In such cases, things would most probably end up returning wrong results.

Similar to the other concepts we already saw in this chapter, monads are defined in terms of the laws they follow. In order for a structure to be considered a monad, it must satisfy all the rules. Let's start with a short definition, which we will expand on later:

Monads are functors that have the unit

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.