edited to add: the functional programmer weenies have found this and are calling me names, so i'm done with this post.
here's the quick version
- imagine you have an object
o, with a hidden value inside of it - you can only access the value through a callback, like
o.Do(func(x) {fmt.PrintLn(x)}) - good news: method chaining works, you can do
o.Do(...).Do(....)too - this is, roughly speaking, a monad. a miserable pile of secret.
Q: why would you want something like this?
- putting everything in a callback means you can control the evaluation order
- for example, "if my hidden value is null, don't run any callbacks"
- this turns out to be really useful in a lazy programming language, like haskell.
- being able to control when or if things gets run lets haskell programmers take on hard programming tasks like "reading things from a file" and "method chaining but also handling nulls"
Q: why doesn't anyone else care?
- it isn't so useful in any other language, for one big honking reason
- writing callback soup sucks ass
- haskell avoids this by having
do notation, which is their way of doing async/await, or more accurately, "doing a continuation passing style transformation on the code, to turn it into a series of nested callbacks" - monads are the epitome of that old "we just used a pencil" urban myth. "hey everyone. we just invented a really neat way of solving a problem we created for ourselves"
Q: why do people keep blogging about it?
i genuinely don't know? continuation passing and method chaining is so 1997.
i guess they were in fashion, and you got free blog hits, but now everything is about event streams again.
In JS, they're called Promise and "do notation" is called async/await. It's just that there's only one monad that really gets the red carpet treatment like this. (Nullable types kind of do in the sense that there's syntax support for method chaining via ?., but it's not as full-throated as async/await.)
I kind of suspect that if there were one or two more monad patterns that were really common in widely-used programming languages, people would get the concept as a generalization a lot more readily.
