fe v0.1.1 FE.Maybe View Source
FE.Maybe is an explicit data type for representing values that might or might not exist.
Link to this section Summary
Functions
Folds over provided list of elements applying it and current accumulator to the provided function
Creates a FE.Maybe representing value passed as an argument
Transforms a FE.Maybe value using a provided function
Returns the value stored in a FE.Maybe, raises a FE.Maybe.Error if a non-value is passed
Returns the value stored in a FE.Maybe or a provided default if it’s a non-value
Link to this section Types
Link to this section Functions
Applies value of FE.Maybe to a provided function and returns its return value,
that should be of FE.Maybe type.
Useful for chaining together a computation consisting of multiple steps, each of which
takes value wrapped in FE.Maybe as an argument and returns a FE.Maybe.
Examples
iex> FE.Maybe.and_then(FE.Maybe.nothing(), fn s -> FE.Maybe.just(String.length(s)) end)
FE.Maybe.nothing()
iex> FE.Maybe.and_then(FE.Maybe.just("foobar"), fn s -> FE.Maybe.just(String.length(s)) end)
FE.Maybe.just(6)
iex> FE.Maybe.and_then(FE.Maybe.just("foobar"), fn _ -> FE.Maybe.nothing() end)
FE.Maybe.nothing()
Works like fold/3, except that the first element of the provided list is removed
from it, wrapped in a FE.Maybe and treated as the initial accumulator.
Then, fold is executed over the remainder of the provided list.
Examples
iex> FE.Maybe.fold([1], fn _, _ -> FE.Maybe.nothing() end)
FE.Maybe.just(1)
iex> FE.Maybe.fold([1, 2, 3], &(FE.Maybe.just(&1 + &2)))
FE.Maybe.just(6)
iex> FE.Maybe.fold([1, 2, 3], fn
...> _, 3 -> FE.Maybe.nothing()
...> x, y -> FE.Maybe.just(x+y)
...> end)
FE.Maybe.nothing()
Folds over provided list of elements applying it and current accumulator to the provided function.
The provided function returns a new accumulator, that should be a FE.Maybe.
The provided FE.Maybe is the initial accumulator.
Returns last value returned by the function.
Stops and returns nothing if at any moment the function returns nothing.
Examples
iex> FE.Maybe.fold(FE.Maybe.nothing(), [], &FE.Maybe.just(&1))
FE.Maybe.nothing()
iex> FE.Maybe.fold(FE.Maybe.just(5), [], &FE.Maybe.just(&1))
FE.Maybe.just(5)
iex> FE.Maybe.fold(FE.Maybe.nothing(), [1, 2], &FE.Maybe.just(&1 + &2))
FE.Maybe.nothing()
iex> FE.Maybe.fold(FE.Maybe.just(5), [6, 7], &FE.Maybe.just(&1 + &2))
FE.Maybe.just(18)
iex> FE.Maybe.fold(FE.Maybe.just(5), [6, 7, 8], fn
...> _, 18 -> FE.Maybe.nothing()
...> x, y -> FE.Maybe.just(x+y)
...> end)
FE.Maybe.nothing()
Creates a FE.Maybe representing value passed as an argument.
Transforms a FE.Maybe value using a provided function.
Examples
iex> FE.Maybe.map(FE.Maybe.nothing(), &String.length/1)
FE.Maybe.nothing()
iex> FE.Maybe.map(FE.Maybe.just("foo"), &String.length/1)
FE.Maybe.just(3)
Creates a FE.Maybe from any Elixir term.
It creates a non-value from nil and a value from any other term.
Examples
iex> FE.Maybe.new(nil)
FE.Maybe.nothing()
iex> FE.Maybe.new(:x)
FE.Maybe.just(:x)
Creates a FE.Maybe representing a non-value.
to_result(t(a), b) :: FE.Result.t(a, b) when b: var
Transforms FE.Maybe to a FE.Result.
A FE.Maybe with a value wrapped becomes a successful value of a FE.Result.
A FE.Maybe without a value wrapped becomes an errornous FE.Result with the
output passed to the function.
Examples
iex> FE.Maybe.to_result(FE.Maybe.just(3), "error")
FE.Result.ok(3)
iex> FE.Maybe.to_result(FE.Maybe.nothing(), "error")
FE.Result.error("error")
to_review(t(a), [b]) :: FE.Review.t(a, b) when b: var
Transforms FE.Maybe to a FE.Review.
A FE.Maybe with a value wrapped becomes an accepted FE.Review with the same value.
A FE.Maybe without a value wrapped becomes a rejected FE.Review with the
issues the same as passed to the function.
Examples
iex> FE.Maybe.to_review(FE.Maybe.just(3), ["error"])
FE.Review.accepted(3)
iex> FE.Maybe.to_review(FE.Maybe.nothing(), ["error"])
FE.Review.rejected(["error"])
Returns the value stored in a FE.Maybe, raises a FE.Maybe.Error if a non-value is passed.
Examples
iex> FE.Maybe.unwrap!(FE.Maybe.just(:value))
:value
Returns the value stored in a FE.Maybe or a provided default if it’s a non-value.
Examples
iex> FE.Maybe.unwrap_or(FE.Maybe.nothing(), :default)
:default
iex> FE.Maybe.unwrap_or(FE.Maybe.just(:value), :default)
:value