Monads you can understand (2015)
I&#x27;ve been trying to find idiomatic ways of explaining monads to people who already know how to program, but may not be using functional languages.  Possibly this explanation will be useful.  I would be happy for some criticism.<p>A monad is a type of functor.  In terms of programming a functor is a collection: a data structure that can hold a piece of information.  Functors have a function related to them, usually called &quot;map&quot; or &quot;fmap&quot; (for &#x27;functor map&#x27;).  &quot;map&quot; takes the information out of the collection, transforms it with a function that you pass to &quot;map&quot; and then puts it back in to the same kind of collection.  Most programmers have experience with this if they use a modern programming language (though they may not have known it was a &quot;functor&quot; that they were using).<p>Quick note: I always found the word &quot;functor&quot; to be confusing because it sounds like it should <i>do</i> something rather than <i>be</i> something.  This is true in category theory where the concept originates, but the computer programming implementation turns out differently.  I&#x27;m going to try to avoid category theory in this explanation, but it&#x27;s still interesting to look into.<p>Like I said, a monad is just a special type of functor.  With a functor, it is possible to change the type of the information you are working on with the function you pass to &quot;map&quot;.  You might have a function that converts an character to an integer (maybe just giving its ASCII representation).  Then you can pass that function to map and your collection of characters will be converted to a collection of integers.  Monads (used as monads) don&#x27;t allow this.<p>Monads use a function called &quot;bind&quot;.  It works a bit like &quot;map&quot; except that the function you pass to &quot;bind&quot; needs to return the resultant monad rather than just the transformed information.  For example, if you have a container that contains an integer, you need to pass a function to &quot;bind&quot; that returns a container that contains an integer.  You can transform the integer in the function, but you must always return a container with the integer inside.<p>Why do you want a monad?  It is so you can chain actions knowing that bind will always return the exact same type.  A place where I often build monads is in refining a search.  I start with a collection of data.  I call &quot;bind&quot; with the refinement function.  This will return a collection of data.  I call &quot;bind&quot; with the next refinement function.  I just keep doing that until I&#x27;ve finished the refinement and I&#x27;m left with the refined collection of data.  I can always add more refinements to the chain because I know that &quot;bind&quot; will always return a container of search results.<p>Implementation detail: You might be wondering why &quot;bind&quot; has to return a monad, while &quot;map&quot; just returns the transformed data (after which &quot;map&quot; puts it back into the functor).  &quot;map&quot;&#x27;s behaviour seems much more convenient.  The reason is so you can handle data structures that have optional data.  Imagine our monad can contain either search results <i>or</i> an error.  If the monad returns search results, &quot;bind&quot; will call the function passed to it.  Otherwise it will just return the monad containing the error.  This allows you to return a monad containing an error from your function.  If you were using &quot;map&quot;, there is no way to return that error.<p>And that&#x27;s basically it.  Collections are almost always functors meaning that you can define map on them.  You use map to apply general transformations on the data in the collection.  Most functors can also be monads which means you can implement bind on them.  You use bind to successively apply operations on a data structure, always getting the exact same type of data structure back.  bind forces the function passed it it to return a monad, which allows you to store optional information in the monad.<p>Final aside: I think that monads are as important in OO as they are in FP, IMHO.  In fact, an object itself is a monad: Write a method on an object that does a transformation of the data in the object and returns a new object of the same type with the transformed data.  In this case, the application of the method (usually &quot;.&quot; in most programming languages) is &quot;bind&quot; and the method is the function that you passed to bind.  However, I&#x27;ll warn you that if you are implementing monads this way, avoid subclass polymorphism unless you want to go crazy ;-)<p>Edit: spelling