I am trying to understand monads better. Is this minimal implementation of a Maybe Monad Correct ?
Maybe = (value) ->
@value = value
Maybe.prototype.ret = -> @value
Maybe.prototype.bind = (fn) ->
if not (@value is null)
return fn @value
@value
Maybe.prototype.lift = (fn) -> (val) -> new Maybe fn val
also if this is correct - there is a final function that I am confused about how to generalize.
Maybe.prototype.lift2 = (fn) -> (M1, M2) ->
f = M1.bind ((val1) -> M2.bind (val2) -> fn val1, val2)
new Maybe f
Now how do you generalize this for lift3,lift4.... liftn Source : http://modernjavascript.blogspot.co.uk/2013/06/monads-in-plain-javascript.html
Follow Up Question:
Could you give me a simple example of how to combine the Maybe Monad with another Monad for simplicity
sake lets keep it a Promise with a .then
method
Since the real usefulness of Monads are transforming them.