In computer science, a Monad is a design pattern, and like all design patterns, its goal is to solve a problem with simplicity.
Use Case
Let’s define an object containing one value, and see how we can manipulate that value:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 

That’s a lot of code to write every time we need to manipulate the object. To solve that problem we could add a method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 

This is better, but it means that we have to keep adding methods every time we need a new calculation.
Using Simple Monads
Think of a Monad as the opposite of a function: a function contains code and takes a value for argument; a Monad contains a value and take code for argument. Both of the following are equivalent:
1 2 

The constructor wraps an initial value:
1 2 3 

The bind()
method applies a function to the value and returns the Monad for chaining:
1 2 3 4 

The return()
method unwraps the value:
1 2 3 

Monads allow you to execute multiple arbitrary functions in a sequence:
1 2 3 4 5 6 

The 3 Monadic Laws
There are various types of Monads, but their definition is beyond the scope of this article. What is important to know is that all Monads obey a few axioms.
For our proofs below, let’s define a few constants:
1 2 3 4 

Left Identity
return()
is a neutral function. Applying return()
to a Monad returns its value:
1


Right Identity
Binding return()
to a Monad doesn’t alter the Monad:
1


Associativity
Binding is independent of the grouping of elements. Binding two functions in succession is equivalent to binding one function determined from them:
1 2 3 4 5 
