This is my first crack at a identity monad and a maybe monad in Elixir:
defmodule Monad do
@doc "v is value to be wrapped as monadic value"
def return(v), do: fn -> v end
@doc "m is monad function, mv is monadic value"
def bind(m, mv), do: m.(mv.())
end
#Simple identity monad
id = fn s -> s end
h = Monad.return(:ok)
r = Monad.bind(id, h)
#Maybe monad
maybe = fn s -> if is_nil(s), do: :error, else: s end
r = Monad.bind(maybe, h)
I'd like to see if there is a way to make the monad into a protocol since the return and bind seem to be common to all monads.