bubblewrap v0.2.1 Bubblewrap.Option

Option module provides Option type with utility functions.

Link to this section Summary

Types

Option type

Functions

Returns content of option if argument is not nil, raises otherwise

Returns true if argument is nil

Returns true if argument is not nil

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

Returns option if argument is not nil, second argument which has to be option otherwise. Executes function, if it's supplied

Link to this section Types

Link to this type

t(a)
t(a) :: a | nil

Option type.

Link to this section Functions

Link to this function

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

Returns content of option if argument is not nil, raises otherwise

Examples

iex> 5 |> get
5
Link to this function

is_none(arg1)
is_none(t(any())) :: boolean()

Returns true if argument is nil

Examples

iex> is_none(nil)
true

iex> is_none(5)
false
Link to this function

is_some(arg1)
is_some(t(any())) :: boolean()

Returns true if argument is not nil

Examples

iex> is_some(5)
true

iex> is_some(nil)
false
Link to this function

ok_or_else(x, f)
ok_or_else(t(a), err | (() -> err)) :: Bubblewrap.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> 5 |> ok_or_else(2)
{:ok, 5} # Essentially ok(5)

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

...> nil |> ok_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 not nil, second argument which has to be option otherwise. Executes function, if it's supplied.

Examples

iex> 5 |> or_else(2)
5

iex> nil |> or_else(2)
2

iex> nil |> or_else(fn -> 1 end)
1