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}

Macros

Monad do-notation

Pipeline form of the monad

Types

maybe_m :: {:just, any} | :nothing

Functions

bind(m, f)

Specs

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

Bind the value inside Maybe monad m to function f.

Note that the computation shortcircuits if m is :nothing.

cat_maybes(l)

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]
fail(msg)

Specs

fail(any) :: maybe_m

Signal failure, i.e. returns :nothing.

from_just(m)

Specs

from_just(maybe_m) :: any

Extracts value x out of {:just, x} or raises an error if given :nothing.

from_maybe(d, m)

Specs

from_maybe(any, maybe_m) :: any

Extracts value x out of {:just, x} or returns default d if given :nothing.

is_just(arg1)

Specs

is_just(maybe_m) :: boolean

Returns true if given {:just, x} and false if given :nothing.

is_nothing(arg1)

Specs

is_nothing(maybe_m) :: boolean

Returns true if given :nothing value and false if given {:just, x}.

list_to_maybe(l)

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}
map_maybes(f, l)

Specs

map_maybes((any -> maybe_m), [any]) :: [any]

Map function f over the list l and throw out elements for which f returns :nothing.

maybe(d, f, m)

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.

maybe_to_list(m)

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]
pipebind(x, fc)

Callback implementation for Monad.Pipeline.pipebind/2.

return(x)

Specs

return(any) :: maybe_m

Inject x into a Maybe monad, i.e. returns {:just, x}.

Macros

m(list)

Monad do-notation.

See the Monad module documentation and the Monad.Maybemodule documentation

p(pipeline)

Pipeline form of the monad.

See Monad module documentation.