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.
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
Describe a Result monad.
Specs
some() :: {:some, any()}
Describe a Maybe monad containing some value.
Specs
Describe a Maybe monad.
Link to this section Functions
Specs
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
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
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
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
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
Specs
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
Specs
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
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}
Specs
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
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
Specs
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
Encaspulate a value into a Maybe monad.
Specs
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
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
Specs
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
Specs
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
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}