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

@type fun1() :: (any() -> any())

Link to this section Functions

Link to this function

cat_maybe(a, f)

@spec cat_maybe([any()], fun1()) :: [any()]

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]
Link to this function

choose(a, f, g)

@spec choose(any(), fun1(), fun1()) :: any()

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
@spec map(any(), fun1()) :: any()

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
Link to this function

maybe(a, f, default)

@spec maybe(any(), fun1(), any()) :: any()

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
Link to this function

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}