MonadEx v1.1.3 Monad.Result View Source

A monad that represents success and failure conditions.

In a series of bind operations, if any function returns an error monad, the following binds are skipped. This allows for easy flow control for both success and error cases.

Link to this section Summary

Types

The standard tuple format for representing success and error states

The possible types of results that can occur (i.e. success and failure)

t()

Functions

Callback implementation of Monad.Behaviour.bind/2

Wraps a value in an error monad

Returns true if the given monad contains an error state

Converts a standard success/failure tuple to a Monad.Result

Callback implementation of Monad.Behaviour.return/1

Wraps a value in a success monad

Returns true if the given monad contains a success state

Converts the Monad.Result to a tagged tuple

Unwraps the value from a success monad

Link to this section Types

Link to this type result_tuple() View Source
result_tuple() :: {result_type(), term()}

The standard tuple format for representing success and error states.

These tuples are easily converted to a Monad.Result.

Link to this type result_type() View Source
result_type() :: :ok | :error

The possible types of results that can occur (i.e. success and failure).

Link to this section Functions

Link to this function bind(result, fun) View Source
bind(t(), (term() -> t())) :: t()

Callback implementation of Monad.Behaviour.bind/2.

If the monad contains a success state, then the value is unwrapped and applied to fun.

For monads containing an error state, the error is returned as is.

iex> s = success 42
iex> r = bind s, (& success &1 * 2)
iex> r.value
84
iex> r.error
nil

iex> s = success 42
iex> r = bind s, fn _ -> error "Failed" end
iex> r.value
nil
iex> r.error
"Failed"
Link to this function error(error) View Source
error(term()) :: t()

Wraps a value in an error monad.

iex> e = error "Failed"
iex> e.value
nil
iex> e.error
"Failed"
Link to this function error?(result) View Source
error?(t()) :: boolean()

Returns true if the given monad contains an error state.

iex> e = error "Failed"
iex> error? e
true
Link to this function from_tuple(arg) View Source
from_tuple(result_tuple()) :: t()

Converts a standard success/failure tuple to a Monad.Result.

iex> s = from_tuple {:ok, 42}
iex> s.value
42
iex> s.error
nil

iex> e = from_tuple {:error, "Failed"}
iex> e.value
nil
iex> e.error
"Failed"
Link to this function return(value) View Source
return(term()) :: t()

Callback implementation of Monad.Behaviour.return/1.

Wraps a value in a success monad.

iex> s = return 42
iex> s.value
42
iex> s.error
nil
Link to this function success(value) View Source
success(term()) :: t()

Wraps a value in a success monad.

iex> s = success 42
iex> s.value
42
iex> s.error
nil
Link to this function success?(result) View Source
success?(t()) :: boolean()

Returns true if the given monad contains a success state.

iex> s = success 42
iex> success? s
true
Link to this function to_tuple(result) View Source
to_tuple(t()) :: result_tuple()

Converts the Monad.Result to a tagged tuple.

iex> s = success 42
iex> to_tuple s
{:ok, 42}

iex> e = error :badarg
iex> to_tuple e
{:error, :badarg}
Link to this function unwrap!(result) View Source
unwrap!(t()) :: term()

Unwraps the value from a success monad.

Does not work with error monads.

iex> s = success 42
iex> unwrap! s
42