Category Theory Abstractions
Inspired by cats library in Scala.
Macro Syntax.monadic
rewrites with
clauses
to nested Functor.map
and Monad.flat_map
applications.
If clause has else
expression, it's mapped to ``MonadError.recover`.
Syntax.monadic do
with a <- Either.right(1),
_ = IO.puts("x1 = #{a}"),
b <- Either.left("!"),
_ = IO.puts("b = #{b}")
do a + b
else
failure ->
IO.puts(failure)
Either.right("-")
end
end
Is rewritten to
MonadError.recover(Monad.flat_map(Either.right(1), fn a ->
IO.puts("x1 = \#{a}")
Functor.map(Either.left("!"), fn b ->
IO.puts("b = \#{b}")
a + b
end)
end), fn failure ->
IO.puts(failure)
Either.right("-")
end)