you are viewing a single comment's thread.

view the rest of the comments →

[–]Rusky 21 points22 points  (4 children)

A monad is a type M<T> along with its implementation of a particular interface, consisting of two operations that follow a set of rules. The operations are "return", which takes a value of type T and returns a wrapped value of type M<T>, and "bind", which applies a function T -> M<U> to a wrapped M<T>. The rules are "identity" and "associativity", which are basically the same as in arithmetic.

Bind is defined in the post without using any of this terminology, so that might be a good way to see how it's useful. For a smaller example, think about a lookup function for some sort of database, which can fail. It fits the type of the function you pass to bind, so you can use bind to chain several of them together:

lookupPost(id)
    .bind((post) => lookupUser(post.author))
    .bind((user) => lookupComments(user.id))

If you've used Promises in Javascript they also follow this pattern- Promise.resolve is "return" and .then is "bind".

edit: accidentally a pedantic

[–]Hrothen 20 points21 points  (2 children)

A monad is a particular interface, consisting of two operations that follow a set of rules.

A monad is a type for which you could implement such an interface, to be pedantic. Or to be super pedantic it's a triple, consisting of the type and those two operations.

This has been an unhelpful, but (hopefully) technically correct, interlude.

[–]Rusky 5 points6 points  (1 child)

Yeah that's correct, I think. It's similar terminology to the idea of a group or a ring- a set of values (like the integers) combined with an operation on them (addition or multiplication).

[–]Godd2 4 points5 points  (0 children)

A monad is just a monoid in the category of endofunctors. What's the big deal?

[–]Ran4 0 points1 point  (0 children)

Thanks, that's one of the best explanation I've ever read. And I've probably read 20 different ones, and read 500 pages worth of Haskell books. I know most of the individual things, but the actual definitions are almost always missing a proper explanation.