Monad.Maybe
The Maybe monad.
The Maybe monad encapsulates an optional value. A maybe monad either
contains a value x (represented as “{:just, x}”) or is empty (represented
as “:nothing”).
Maybe can be used a simple kind of error monad, where all errors are
represented by :nothing.
Examples
iex> require Monad.Maybe, as: Maybe
iex> Maybe.m do
...> x <- {:just, 1}
...> y <- {:just, 2}
...> return x + y
...> end
{:just, 3}
iex> require Monad.Maybe, as: Maybe
iex> Maybe.m do
...> x <- {:just, 1}
...> y <- :nothing
...> return x + y
...> end
:nothing
Summary
Functions
Bind the value inside Maybe monad m to function f
Takes a list of maybes and returns a list of all the just values
Signal failure, i.e. returns :nothing
Extracts value x out of {:just, x} or raises an error if given :nothing
Extracts value x out of {:just, x} or returns default d if given
:nothing
Returns true if given {:just, x} and false if given :nothing
Returns true if given :nothing value and false if given {:just, x}
Converts list l to a maybe value
Map function f over the list l and throw out elements for which f
returns :nothing
Call function f with x if m is {:just, x}, otherwise call function f
with default value d
Converts maybe value m to a list
Callback implementation for Monad.Pipeline.pipebind/2
Inject x into a Maybe monad, i.e. returns {:just, x}
Types
maybe_m :: {:just, any} | :nothing
Functions
Bind the value inside Maybe monad m to function f.
Note that the computation shortcircuits if m is :nothing.
Specs
cat_maybes([maybe_m]) :: [any]
Takes a list of maybes and returns a list of all the just values.
Example
iex> cat_maybes [{:just, 1}, :nothing, {:just, 2}, :nothing, {:just, 3}]
[1, 2, 3]
Specs
from_just(maybe_m) :: any
Extracts value x out of {:just, x} or raises an error if given :nothing.
Specs
from_maybe(any, maybe_m) :: any
Extracts value x out of {:just, x} or returns default d if given
:nothing.
Specs
is_just(maybe_m) :: boolean
Returns true if given {:just, x} and false if given :nothing.
Specs
is_nothing(maybe_m) :: boolean
Returns true if given :nothing value and false if given {:just, x}.
Specs
list_to_maybe([any]) :: maybe_m
Converts list l to a maybe value.
Returns :nothing if given the empty list; returns {:just, x} when given
the nonempty list l, where x is the head of l.
Examples
iex> list_to_maybe []
:nothing
iex> list_to_maybe [1, 2, 3]
{:just, 1}
Specs
map_maybes((any -> maybe_m), [any]) :: [any]
Map function f over the list l and throw out elements for which f
returns :nothing.
Specs
maybe(any, (any -> any), maybe_m) :: any
Call function f with x if m is {:just, x}, otherwise call function f
with default value d.
Specs
maybe_to_list(maybe_m) :: [any]
Converts maybe value m to a list.
Returns an empty list if given :nothing or returns a list [x] if given
{:just, x}.
Examples
iex> maybe_to_list :nothing
[]
iex> maybe_to_list {:just, 42}
[42]
Callback implementation for Monad.Pipeline.pipebind/2.
Macros
Monad do-notation.
See the Monad module documentation and the
Monad.Maybemodule documentation
Pipeline form of the monad.
See Monad module documentation.