noether v0.1.1 Noether.Maybe

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.

Given a list, it returns {:ok, list} if every element of the list is different from nil. Otherwise {:error, :nil_found} is returned.

Link to this section Types

Link to this type

fun1()
fun1() :: (any() -> any())

Link to this section Functions

Link to this function

cat_maybe(a, f)
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

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)
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

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

map(a, f)
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

iex> map(nil, &Kernel.abs/1)
nil

iex> map(-1, &Kernel.abs/1)
1
Link to this function

maybe(a, f, default)
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

iex> maybe(-1, &Kernel.abs/1, :hello)
1

iex> maybe(nil, &Kernel.abs/1, :hello)
:hello
Link to this function

required(a, default)
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

iex> required(nil, :hello)
{:error, :hello}

iex> required(1, :hello)
{:ok, 1}
Link to this function

sequence(a)
sequence([any()]) :: Noether.Either.either()

Given a list, it returns {:ok, list} if every element of the list is different from nil. Otherwise {:error, :nil_found} is returned.

Examples

iex> sequence([1, 2])
{:ok, [1, 2]}

iex> sequence([1, nil, 3])
{:error, :nil_found}