Musings about FP and CS

A log of my journey through FP and CS

Build up your monad intuition… in JS

by Clement Delafargue on December 16, 2014

Tagged as: fp, monad, javascript.

Most people start to grasp monads with the so called “wrapped metaphor”. It works well with List, Either a and Maybe, but it can become a problem with other monads, like IO for instance. If you have a wrapped value, why wouldn’t you be able to extract it?

A more general intuition could be “computational context which encodes sequentiality”… which doesn’t make sense until it does.

I’ve found the best way to get rid of the intuition of wrapped values is by looking at how the continuation monad works. After all, it’s The Mother of all Monads.

A few days ago, I came upon an article about Monads in PHP by Anthony Ferrara, which was quite interesting (I have a sweet spot for monads in php). Unfortunately, the implementation proposed in the article had two issues:

  • fmap and bind are conflated
  • the monad interface has an extract method

Tweets being a very ineffective means of communication, I came up with a bit of javascript to illustrate my point. As a side effect it’s a nice way to play with the essence of monads, even if you don’t know Haskell.

Please note you need a basic understanding of monads to play with it. Else, it could be a bit daunting.

Implement the continuation monad in JS

A solution