Monad.Error

The Error monad.

Allows shortcutting computations in typical Elixir/Erlang style.

Works on values of the form {:error, reason} | {:ok, value}. If an error value is passed to bind it is immediately returned, if an ok value is passed the value inside the tuple is given to the function passed to bind.

Examples

iex> alias Monad.Error
iex> require Error
iex> Error.m do
...>   a <- {:ok, 1}
...>   b <- return 2
...>   return a + b
...> end
{:ok, 3}

iex> alias Monad.Error
iex> require Error
iex> Error.m do
...>   a <- fail "aborted"
...>   b <- {:ok, 1}
...>   return a + b
...> end
{:error, "aborted"}

Summary

Functions

Bind the value inside Error monad m to function f

Signal failure, i.e. returns {:error, msg}

Callback implementation for Monad.Pipeline.pipebind/2

Inject x into a Error monad, i.e. returns {:ok, x}

Macros

Monad do-notation

Pipeline form of the monad

Types

error_m :: {:error, any} | {:ok, any}

Functions

bind(m, f)

Specs

bind(error_m, (any -> error_m)) :: error_m

Bind the value inside Error monad m to function f.

Note that the computation shortcircuits if m is an error value.

fail(msg)

Specs

fail(any) :: error_m

Signal failure, i.e. returns {:error, msg}.

pipebind(x, fc)

Callback implementation for Monad.Pipeline.pipebind/2.

return(x)

Specs

return(any) :: error_m

Inject x into a Error monad, i.e. returns {:ok, x}.

Macros

m(list)

Monad do-notation.

See the Monad module documentation and the Monad.Errormodule documentation

p(pipeline)

Pipeline form of the monad.

See Monad module documentation.