Copy and paste the swift code below into a playground to experiment.
This is a very close emulation of Functor and Monad typeclasses in swift. As of Swift 1.2 and Xcode 6.3, this is no longer very fragile.
Unfortunately, the compiler cannot verify the types when passing a function to (>>=)
. We have to wrap the function in a closure and call it with an explicit argument to compile.
optionalDoubles >>= squareRoot // doesn't compile
optionalDoubles >>= { squareRoot($0) } // compiles