If you've heard about Monads but never had time learn them, here's a simple explanation. Not a theoretical nonsense. It's a simple, practical tutorial for JavaScript developers showing how some monads can be used. It's for engineers, not scientists.

In theory there is no difference between theory and practice. In practice there is.

Yogu Berra

All examples are based on monet.js - a tool bag that assists Functional Programming by providing a rich set of Monads and other useful functions. I'll use arrow functions introduced in ES6. They are much more readable than regular functions when used as single operation "callbacks". Also in some examples TypeScript type definitions will be added to enhance overall readability.





Monad

Monad is a box containing some value. People say it has something to do with freaky category theory but we can ignore that fact. Our box is not just a wrapper. It has tools to bind computations to the value. It can serve multiple different purposes - asynchronicity, fail-fast, error accumulation, lazy evaluation, etc… but these will be covered in future articles.

Monads are defined by 3 axioms. One of the things that axioms tell us is that there have to be some function that binds calculations to the value contained in a monad box. It's called bind (or flatMap ) and has to meet 3 requirements: