Monads Explained Quickly

综合编程 2016-04-07

Most monad tutorials are long, confusing, and ineffective. I cannot promise this one will be more clear, more interesting, or even more effective, but I can at least promise to make it brisk. You have nothing to lose by reading it.

Take a look at this object, Foo
. It’s a monad, but what that means
isn’t relevant right now. Let’s just focus on how Foo specifically works:

function Foo(value) {
    this.get = ()=> value;
    this.bind = fn => {
        let result = fn(value);
        return new Foo(result);

Foo holds a single value
that never changes. It provides a method for getting
it, and a curious method named bind
. Bind gets a function, passes it the value
, and then returns the result in a new Foo.

Because bind returns a new Foo, it’s chainable:

let one = new Foo(1);
let two = one.bind(x => x + 7).bind(x => x / 2).bind(x => x - 2);
two.get() === 2;

This chaining is kinda neat – it lets me write the operations I want to perform on x
in order, rather than with nested function calls, like below:

let two = minusTwo(divideByTwo(addSeven(1))); // I have to read this right-to-left, which is awkward

But the real value to an object like Foo is that if I put logic inside bind
, I can abstract away an action that I want to perform between each step of an operation.

Consider another monad, named Bar

function Bar(value) {
  this.get = ()=> value;
  this.bind = fn => {
      let result = fn(value);
      return new Bar(result);

If I have an operation that makes multiple changes to a value, and I want to log how that value mutates at each turn, Bar
lets me swap code like this…

let stepOne = something(1);
let stepTwo = somethingElse(stepOne);
let stepThree = somethingDifferent(stepTwo);

…for code like this:

new Bar(1)
  .bind(something)           // console >> logs new value
  .bind(somethingElse)       // console >> logs new value
  .bind(somethingDifferent); // console >> logs new value

You now understand monads. I did promise this would be quick. Monads can be boiled down - roughly - to the following rules:

  1. A monad contains a value
  2. A monad has a method named bind
    that takes a function
  3. Bind has some logic around calling that function and processing the result
  4. Bind then returns the result in a new monad, so that calls to bind can be chained

That’s it. If you understand how the above code works, you understand monads. Sorry if you were hoping for something more magical and mind-bending.

We can do anything we like in bind
. Anything that we want to do between the steps of an operation – be that deciding how we pass the value into the next step, or doing something with the result that comes back out – we can put inside bind
and abstract away. Null checking is a great example:

function Maybe(value) {
  this.get = ()=> value;
  this.bind = fn => {
      if (value === null) {
          return new Baz(null);
      } else {
          return new Baz(fn(value));

In Maybe
, bind only calls the supplied function if it can hand it a valid, non-null value. Otherwise it quietly returns a clone of itself. This lets us turn verbose, repetitive code like this…

let connection = getConnection();
let user = connection ? connection.getUser() : null;
let address = user ? user.getAddress() : null;
let zipCode = address ? address.getZip() : null;

…into a much more elegant alternative:

let zipCode =
    new Maybe(getConnection())
    .bind(c => c.getUser())
    .bind(u => u.getAddress())
    .bind(a => a.getZip);

zipCode.get(); // returns either a zip code (if every step worked) or a null (if any step returned one)

Hopefully these two examples are enough to show you why monads and their bind methods are so powerful. No doubt you can imagine uses of your own.

There are scores of other monads, all with quite diverse uses and functionality. But they all follow those same four rules we saw in Foo
and Bar
. Provided you stick to them, you are using the monad pattern, and now have sufficient functional programming credence to immediately declare yourself a greybeard FP maven, lording it over the programming masses.

责编内容by:Jimmy Breck-McKye (源链)。感谢您的支持!


Haskell’s State monad is not a monad (2014) Haskell’s State monad has a shameful secret. It’s not a monad ...
Continuation 与 Monad 结构 Monad 与 Continuation 都用于表示计算。用两个例子可以看到它们的结构非常相似: foo :: M...
Difference Lists and the Codensity Monad Mio Alter shares two tricks for Haskell code optimization: the difference l...
Monads for Dummies February 28, 2014 Don’t mind the linkbait title—I know you’re not a dummy. And...
Monads are confusing. Let us help Scala developers love to discuss Monads, their metaphors, and their many use cas...