Noether.Maybe (noether v0.2.5)
Link to this section Summary
Functions
Given a list of values, the function is mapped only on the elements different from nil
. nil
values will be discarded. A list of the results is returned.
Given a value and two functions, it applies the first one and returns the result if it's different from nil. Otherwise the second function is applied.
Given a value and a function, the function is applied only if the value is different from nil
. nil
is returned otherwise.
Given a value, a function, and a default, it applies the function on the value if the latter is different from nil
. It returns the default otherwise.
Given a value and a default, {:ok, value}
is returned only if the value is different from nil
. {:error, default}
is returned otherwise.
Link to this section Types
fun1()
Link to this section Functions
cat_maybe(a, f)
Given a list of values, the function is mapped only on the elements different from nil
. nil
values will be discarded. A list of the results is returned.
examples
Examples
iex> cat_maybe([1], &(&1 + 1))
[2]
iex> cat_maybe([1, nil, 3], &(&1 + 1))
[2, 4]
choose(a, f, g)
Given a value and two functions, it applies the first one and returns the result if it's different from nil. Otherwise the second function is applied.
examples
Examples
iex> choose(0, fn a -> a + 1 end, fn b -> b + 2 end)
1
iex> choose(0, fn _ -> nil end, fn b -> b + 2 end)
2
iex> choose(0, fn _ -> nil end, fn _ -> nil end)
nil
map(a, f)
Given a value and a function, the function is applied only if the value is different from nil
. nil
is returned otherwise.
examples
Examples
iex> Noether.Maybe.map(nil, &Kernel.abs/1)
nil
iex> Noether.Maybe.map(-1, &Kernel.abs/1)
1
maybe(a, f, default)
Given a value, a function, and a default, it applies the function on the value if the latter is different from nil
. It returns the default otherwise.
examples
Examples
iex> maybe(-1, &Kernel.abs/1, :hello)
1
iex> maybe(nil, &Kernel.abs/1, :hello)
:hello
required(a, default)
@spec required(any(), any()) :: Noether.Either.either()
Given a value and a default, {:ok, value}
is returned only if the value is different from nil
. {:error, default}
is returned otherwise.
examples
Examples
iex> required(nil, :hello)
{:error, :hello}
iex> required(1, :hello)
{:ok, 1}