3 ms·
Functor is something that has a map function. A monad is any generic class that has a flatMap function and a factory function that together satisfy 3 laws (exp
by spion 13y ago
Functor is something that has a map function.
A monad is any generic class that has a flatMap function and a factory function that together satisfy 3 laws (explained below)
flatMap is like map, except the function passed to flatMap must return a monad (of the same kind).
The factory can make a monad of a regular value (it wraps it)
For example, an array's flatMap function takes a function that takes an element and returns an array. You can return an empty array if you wish, or an array of two elements (of the same type). flatMap "flattens" the result - it returns an array of elements of that same type.
The factory function and flatMap must satisfy 3 laws: left identity, right identity and associativity. That is, flatMapping with the factory function should result with the same thing as what you started with; wrapping a value with a factory then flatMapping it should result with that same value being passed to the function you passed to flatMap; and it shouldn't matter whether multiple flatMap calls are chained, or nested.
Like design patterns in OO languages, it isn't immediately clear why its useful to give a name (or a type) to all generic classes that satisfy this "interface" called monad, but it turns out that the concept pops up everywhere, from arrays to futures to Maybe (to replace null values) to Either to Observable and so on.
Not sure if I managed to be correct, understandable, concise or neither :P
- theophrastus 13y agook i'm going on a limb here and suggest that you're probably "correct". but you need to use more terms selected from the following list (random usage is encouraged but idempotent): closure, adjoint, operator, endofunctor, universal algebra, maybe, iteratee, continuation, surjective, type constructor, simonpeytonjonesing