... and for me to understand them better.
"In functional programming, a monad is a structure that represents computations defined as sequences of steps."
From the Monad typeclass in Haskell.
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
class None
constructor: () ->
none = new None
class Some
constructor: (@value) ->
some = (x) -> new Some x
Maybe =
return: (x) -> some x
bind: (x, f) ->
if x instanceof None then return x
if x instanceof Some then return (f x.value)
throw "#{x} is not an instance of the Maybe monad"
Promise =
return: Q
bind: (x, f) -> x.then f
item = M.chain Maybe, getUserInput, createTodoItem, save
# If getUserInput returns None then createTodoItem and save will never be called.
M.do Promise,
someDude: () -> getUser 'some.dude'
mine: () -> getFriendsOf loggedInUser
his: () -> getFriendsOf this.someDude
_: () ->
inCommon = _.intersection this.mine, this.his
asString = inCommon.join ', '
log "#{inCommon.length} friends in common: #{asString}."