Comment Re:Monads! (Score 1) 536
You've got me interested... but how does it work?
I hate to be the lazy bum, but since it is your made-up notation would you mind adding in the return value checks and pseudo-code for handler calls to allow some better understanding? Feel free to add pseudo-annotations if that helps your cause 8)
Thank you for bringing this up! This topic has certainly engaged me more than any other on slashdot in a few months 8)
Here's a quick example of a monad. It's something that wraps a value where the value can be freely generic while the rest of the type is not.
So let's use the Option monad in scala. It has two important functions called map and flatMap (map and bind in Haskell). All monads have these properties.
If I have val a0 = Some(25); val a1 = None;
map has a type signature of def map[Q, B](f: A => B): Option[B]
flatMap has a type signature of def flatMap[Q, B](f: A => Option[B]): Option[B]
a0.map((b: Int) => b + 5) would become Some(30)
a0.map((b: Int) => b + 5).map((c: Int) => c - 10) would become Some(20)
a1.map((b: Int) => b + 5) would become None
a1.map((b: Int) => b + 5).map((c: Int) => c - 10) would become None
a0.flatMap((b: Int) => Some(b + 5)) would become Some(25)
a0.flatMap((b: Int) => Some(b + 5)).flatMap((c: Int) => Some(c - 10)) would become Some(20)
a0.flatMap((b: Int) => None) would become None
a0.flatMap((b: Int) => None).flatMap((c: Int) => Some(c - 10)) would become None
a1.flatMap is always None
There's other monads that also wrap a value. The Promise monad wraps the result of an asynchronous computation so that the maps and flatMaps are chained asynchronously. The Error monad is the useful monad. Instead of having a Java function return a value through the return type or an exception through the throws clause, both return and exception values could be returned through the return type. The error monad is most similar to the option monad, which is called the anonymous exception sometimes. But the error monad doesn't have a zero so it's not quite the same as the option monad with zero of None, or the List monad with the zero of empty list.
Every List is a monad as it's a structure that wraps a generic value. The value just happens to be indeterminate.
Monads are just a way to chain events sequentially without having to deal with the background implementations of the structure we're dealing with.