Appendix B. Monads

 

After reading this book, you might be surprised (and possibly frustrated) by the fact that I didn’t talk about monads. Monads are a hot topic, and you can find many so-called “Monad tutorials” on the web. The topic of monads seems to be very intimidating, and many programmers read these tutorials one after the other in the hope that they’ll eventually understand what monads are. Of course, many other programmers do understand monads, but very few are able to explain monads in simple terms.

The reason why there are so many monad tutorials is probably because there’s no definitive tutorial, so people keep trying to roll their own. This appendix is not another monad tutorial. I wouldn’t want to write one for two reasons:

  • If you have read this book, you don’t need a monad tutorial. Although I never used the term monad, you already know what a monad is. You know the concept and have made heavy use of it throughout this book. You just have to name it.
  • There’s an old saying about monads having a kind of magic: as soon as you understand them, you lose the ability to explain them to others.

But let’s see what others say about monads. Searching on the internet, you can find many definitions:

  • “A monad is just a monoid in the category of endofunctors.”
  • “A monad is a computational context for some value.”
  • “A monad is a class with a unit method and a flatmap method.”

You may also find some more exotic definitions:

  • “Monads are burritos.”
  • “Monads are elephants.”