fe v0.1.0 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

Applies value of FE.Maybe to a provided function and returns its return value, that should be of FE.Maybe type

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

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

Creates a FE.Maybe from any Elixir term

Creates a FE.Maybe representing a non-value

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 type t(a) View Source
t(a) :: {:just, a} | :nothing

Link to this section Functions

Link to this function and_then(maybe, f) View Source
and_then(t(a), (a -> t(a))) :: t(a) when a: var

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()
Link to this function fold(elems, f) View Source
fold([b], (b, a -> t(a))) :: t(a) when b: var, a: var

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()
Link to this function fold(maybe, elems, f) View Source
fold(t(a), [b], (b, a -> t(a))) :: t(a) when b: var, a: var

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()
Link to this function just(value) View Source
just(a) :: t(a) when a: var

Creates a FE.Maybe representing value passed as an argument.

Link to this function map(maybe, f) View Source
map(t(a), (a -> a)) :: t(a) when a: var

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)
Link to this function new(term) View Source
new(a | nil) :: t(a) when a: var

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)
Link to this function nothing() View Source
nothing() :: t(any())

Creates a FE.Maybe representing a non-value.

Link to this function to_result(maybe, error) View Source
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")
Link to this function to_review(maybe, issues) View Source
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"])
Link to this function unwrap!(maybe) View Source
unwrap!(t(a)) :: a | no_return() when a: var

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
Link to this function unwrap_or(maybe, default) View Source
unwrap_or(t(a), a) :: a when a: var

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