Rustic.Maybe (Rustic.Maybe v0.1.0) View Source

Implementation of the Maybe monad, inspired by Rust's Option type.

Link to this section Summary

Types

Describe an Error result.

Describe an empty Maybe monad.

Describe an Ok result.

Describe a Result monad.

Describe a Maybe monad containing some value.

t()

Describe a Maybe monad.

Functions

Boolean AND operation on 2 Maybe monads which returns the right one.

Maps the value of a non-empty Maybe monad to a new Maybe monad.

Returns the Maybe monad only if predicate returns true for its contained value.

Returns the Maybe monad contained in a Maybe monad, or nothing.

Check if a value is a Maybe monad.

Check if a value is an empty Maybe monad.

Check if a value is a non-empty Maybe monad.

Apply a function to the value of a Maybe monad.

Return the default result for an empty Maybe monad, or Apply a function to its value and return the result.

Compute the default result for an empty Maybe monad, or Apply a function to its value and return the result.

Returns an empty Maybe monad.

Transform a Maybe monad into a Result monad, turning nothing into an error.

Transform a Maybe monad into a Result monad turning nothing into a computed error.

Returns the Maybe monad or execute a function returing a Maybe monad.

Boolean OR operation on 2 Maybe monads which returns the left one.

Encaspulate a value into a Maybe monad.

Transform a Maybe monad containing a Result monad to a Result monad containing a Maybe monad.

Get the value of a Maybe monad or raise an exception if it were empty.

Get the value of a Maybe monad or a default value if it were empty.

Get the value of a Maybe monad or compute a default value if it were empty.

Boolean XOR operation on 2 Maybe monad which returns some value if and only if one of them is non empty.

Link to this section Types

Specs

error() :: {:error, any()}

Describe an Error result.

Specs

nothing() :: :nothing

Describe an empty Maybe monad.

Specs

ok() :: {:ok, any()}

Describe an Ok result.

Specs

result() :: ok() | error()

Describe a Result monad.

Specs

some() :: {:some, any()}

Describe a Maybe monad containing some value.

Specs

t() :: nothing() | some()

Describe a Maybe monad.

Link to this section Functions

Link to this function

and_other(arg1, other_mval)

View Source

Specs

and_other(t(), t()) :: t()

Boolean AND operation on 2 Maybe monads which returns the right one.

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.and_other(Rustic.Maybe.some(2))
{:some, 2}

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.and_other(Rustic.Maybe.some(2))
:nothing

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.and_other(Rustic.Maybe.nothing())
:nothing

Specs

and_then(t(), (any() -> t())) :: t()

Maps the value of a non-empty Maybe monad to a new Maybe monad.

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.and_then(fn n -> Rustic.Maybe.some(n + 1) end)
{:some, 2}

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.and_then(fn _ -> :nothing end)
:nothing

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.and_then(fn n -> Rustic.Maybe.some(n + 1) end)
:nothing

Specs

filter(t(), (any() -> boolean())) :: t()

Returns the Maybe monad only if predicate returns true for its contained value.

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.filter(fn n -> n > 0 end)
{:some, 1}

iex> Rustic.Maybe.some(-1)
...>   |> Rustic.Maybe.filter(fn n -> n > 0 end)
:nothing

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.filter(fn n -> n > 0 end)
:nothing

Specs

flatten(t()) :: t()

Returns the Maybe monad contained in a Maybe monad, or nothing.

iex> Rustic.Maybe.some(Rustic.Maybe.some(1))
...>   |> Rustic.Maybe.flatten()
{:some, 1}

iex> Rustic.Maybe.some(Rustic.Maybe.nothing())
...>   |> Rustic.Maybe.flatten()
:nothing

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.flatten()
:nothing

Check if a value is a Maybe monad.

iex> Rustic.Maybe.nothing() |> Rustic.Maybe.is_maybe()
true

iex> Rustic.Maybe.some(1) |> Rustic.Maybe.is_maybe()
true

iex> 1 |> Rustic.Maybe.is_maybe()
false

Check if a value is an empty Maybe monad.

iex> Rustic.Maybe.nothing() |> Rustic.Maybe.is_nothing()
true

iex> Rustic.Maybe.some(1) |> Rustic.Maybe.is_nothing()
false

iex> 1 |> Rustic.Maybe.is_nothing()
false

Check if a value is a non-empty Maybe monad.

iex> Rustic.Maybe.nothing() |> Rustic.Maybe.is_some()
false

iex> Rustic.Maybe.some(1) |> Rustic.Maybe.is_some()
true

iex> 1 |> Rustic.Maybe.is_some()
false

Specs

map(t(), (any() -> any())) :: t()

Apply a function to the value of a Maybe monad.

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.map(fn n -> n + 1 end)
{:some, 2}

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.map(fn n -> n + 1 end)
:nothing
Link to this function

map_or(arg1, default_val, func)

View Source

Specs

map_or(t(), any(), (any() -> any())) :: any()

Return the default result for an empty Maybe monad, or Apply a function to its value and return the result.

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.map_or(3, fn n -> n + 1 end)
2

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.map_or(3, fn n -> n + 1 end)
3
Link to this function

map_or_else(arg1, default_func, func)

View Source

Specs

map_or_else(t(), (() -> any()), (any() -> any())) :: any()

Compute the default result for an empty Maybe monad, or Apply a function to its value and return the result.

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.map_or_else(fn -> 3 end, fn n -> n + 1 end)
2

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.map_or_else(fn -> 3 end, fn n -> n + 1 end)
3

Specs

nothing() :: nothing()

Returns an empty Maybe monad.

Specs

ok_or(t(), any()) :: result()

Transform a Maybe monad into a Result monad, turning nothing into an error.

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.ok_or(:no_value)
{:ok, 1}

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.ok_or(:no_value)
{:error, :no_value}
Link to this function

ok_or_else(arg1, err_func)

View Source

Specs

ok_or_else(t(), any()) :: result()

Transform a Maybe monad into a Result monad turning nothing into a computed error.

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.ok_or_else(fn -> :no_value end)
{:ok, 1}

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.ok_or_else(fn -> :no_value end)
{:error, :no_value}

Specs

or_else(t(), (() -> t())) :: t()

Returns the Maybe monad or execute a function returing a Maybe monad.

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.or_else(fn -> Rustic.Maybe.some(2) end)
{:some, 1}

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.or_else(fn -> Rustic.Maybe.some(2) end)
{:some, 2}

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.or_else(fn -> Rustic.Maybe.nothing() end)
:nothing
Link to this function

or_other(mval, other_mval)

View Source

Specs

or_other(t(), t()) :: t()

Boolean OR operation on 2 Maybe monads which returns the left one.

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.or_other(Rustic.Maybe.some(2))
{:some, 1}

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.or_other(Rustic.Maybe.some(2))
{:some, 2}

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.or_other(Rustic.Maybe.nothing())
:nothing

Specs

some(any()) :: some()

Encaspulate a value into a Maybe monad.

Specs

transpose(t()) :: result()

Transform a Maybe monad containing a Result monad to a Result monad containing a Maybe monad.

iex> Rustic.Maybe.some({:ok, 1})
...>   |> Rustic.Maybe.transpose()
{:ok, {:some, 1}}

iex> Rustic.Maybe.some(:ok)
...>   |> Rustic.Maybe.transpose()
{:ok, {:some, nil}}

iex> Rustic.Maybe.some({:error, :no_value})
...>   |> Rustic.Maybe.transpose()
{:error, :no_value}

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.transpose()
{:ok, :nothing}

Specs

unwrap!(t()) :: any()

Get the value of a Maybe monad or raise an exception if it were empty.

iex> Rustic.Maybe.some(1) |> Rustic.Maybe.unwrap!()
1
iex> Rustic.Maybe.nothing() |> Rustic.Maybe.unwrap!()
** (ArgumentError) trying to unwrap an empty Maybe monad
Link to this function

unwrap_or(arg1, default_value)

View Source

Specs

unwrap_or(t(), any()) :: any()

Get the value of a Maybe monad or a default value if it were empty.

iex> Rustic.Maybe.some(1) |> Rustic.Maybe.unwrap_or(2)
1
iex> Rustic.Maybe.nothing() |> Rustic.Maybe.unwrap_or(2)
2
Link to this function

unwrap_or_else(arg1, default_func)

View Source

Specs

unwrap_or_else(t(), (() -> any())) :: any()

Get the value of a Maybe monad or compute a default value if it were empty.

iex> Rustic.Maybe.some(1) |> Rustic.Maybe.unwrap_or_else(fn -> 2 end)
1
iex> Rustic.Maybe.nothing() |> Rustic.Maybe.unwrap_or_else(fn -> 2 end)
2

Specs

xor_other(t(), t()) :: t()

Boolean XOR operation on 2 Maybe monad which returns some value if and only if one of them is non empty.

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.xor_other(Rustic.Maybe.some(2))
:nothing

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.xor_other(Rustic.Maybe.nothing())
:nothing

iex> Rustic.Maybe.some(1)
...>   |> Rustic.Maybe.xor_other(Rustic.Maybe.nothing())
{:some, 1}

iex> Rustic.Maybe.nothing()
...>   |> Rustic.Maybe.xor_other(Rustic.Maybe.some(2))
{:some, 2}