monex v0.1.15 MonEx.Option

Option module provides Option type with utility functions.

Link to this section Summary

Types

Option type. some(a) or none() unwraps into {:some, a} or {:none}

Functions

Returns content of option if argument is some(), raises otherwise

Returns content of option if argument is some(), second argument otherwise

Returns true if argument is none() false if some()

Returns true if argument is some() false if none()

Converts an Option into Result if value is present, otherwise returns second argument wrapped in error()

Returns option if argument is some(), second argument which has to be option otherwise. Executes function, if it's supplied

Converts arbitrary term into option, some(term) if not nil, none() otherwise

Link to this section Types

Link to this type

t(a)
t(a) :: {:some, a} | {:none}

Option type. some(a) or none() unwraps into {:some, a} or {:none}

Link to this section Functions

Link to this function

get(arg)
get(t(a)) :: a when a: any()

Returns content of option if argument is some(), raises otherwise

Examples

iex> some(5) |> get
5
Link to this function

get_or_else(arg, f)
get_or_else(t(a), a | (() -> a)) :: a when a: any()

Returns content of option if argument is some(), second argument otherwise.

Examples

iex> some(5) |> get_or_else(2)
5

iex> none() |> get_or_else(2)
2

iex> none() |> get_or_else(fn -> 1 end)
1
Link to this function

is_none(x)
is_none(t(any())) :: boolean()

Returns true if argument is none() false if some()

Examples

iex> is_none(none())
true

iex> is_none(some(5))
false
Link to this function

is_some(arg)
is_some(t(any())) :: boolean()

Returns true if argument is some() false if none()

Examples

iex> is_some(some(5))
true

iex> is_some(none())
false
Link to this macro

none() (macro)

Link to this function

ok_or_else(arg, f)
ok_or_else(t(a), err | (() -> err)) :: MonEx.Result.t(a, err)
when a: any(), err: any()

Converts an Option into Result if value is present, otherwise returns second argument wrapped in error().

Examples

iex> some(5) |> ok_or_else(2)
{:ok, 5} # Essentially ok(5)

...> none() |> ok_or_else(:missing_value)
{:error, :missing_value} # Essentially error(:missing_value)

...> none() |> get_or_else(fn -> :oh_no end)
{:error, :oh_no}
Link to this function

or_else(x, f)
or_else(t(a), t(a) | (() -> t(a))) :: t(a) when a: any()

Returns option if argument is some(), second argument which has to be option otherwise. Executes function, if it's supplied.

Examples

iex> some(5) |> or_else(some(2))
some(5)

iex> none() |> or_else(some(2))
some(2)

iex> none() |> or_else(fn -> some(1) end)
some(1)
Link to this macro

some(val) (macro)

Link to this function

to_option(x)
to_option(a) :: t(a) when a: any()

Converts arbitrary term into option, some(term) if not nil, none() otherwise

Examples

iex> to_option(5)
some(5)

iex> to_option(nil)
none()