monex v0.1.11 MonEx.Option

Option module provides Option type with utility functions.

Link to this section Summary

Types

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

Functions

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

some(5) |> get == 5

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

some(5) |> get_or_else(2) == 5
none() |> get_or_else(2) == 2
none() |> get_or_else(fn -> 1) == 1

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

is_none(none()) == true

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

is_some(some(5)) == true

Returns option if argument is some(), second argument wrapped in some otherwise. Executes function, if it's supplied.

some(5) |> or_else(2) == some(5)
none() |> or_else(2) == some(2)
none() |> or_else(fn -> some(1)) == some(1)

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

to_option(5) == some(5)
to_option(nil) == none()

Link to this section Types

Link to this type

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

Option type. some(x) or none() unwraps into {:some, x} 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

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.

some(5) |> get_or_else(2) == 5
none() |> get_or_else(2) == 2
none() |> get_or_else(fn -> 1) == 1
Link to this function

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

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

is_none(none()) == true
Link to this function

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

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

is_some(some(5)) == true
Link to this macro

none() (macro)

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 wrapped in some otherwise. Executes function, if it's supplied.

some(5) |> or_else(2) == some(5)
none() |> or_else(2) == some(2)
none() |> or_else(fn -> some(1)) == 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

to_option(5) == some(5)
to_option(nil) == none()