FunFunc v0.2.0 FunFunc.Nilable

Functions for Nilable valeu.

Link to this section Summary

Functions

Gets the first argument if it is not nil. Otherwise returns the default value

Gets the first argument if it is not nil. Otherwise returns the result of the function

Maps if the first argument is not nil. Otherwise returns nil

Maps the first argument if it is not nil. Otherwise returns the default value

Maps the first argument if it is not nil. Otherwise returns the result of the function

Link to this section Types

Link to this type func()
func() :: (nilable() -> nilable())
Link to this type nilable()
nilable() :: nil | any()

Link to this section Functions

Link to this function get_or(x, default)
get_or(nilable(), any()) :: any()

Gets the first argument if it is not nil. Otherwise returns the default value.

Examples

iex> FunFunc.Nilable.get_or(1, 2)
1
iex> FunFunc.Nilable.get_or(nil, 2)
2
Link to this function get_or_else(x, default)
get_or_else(nilable(), (... -> any())) :: any()

Gets the first argument if it is not nil. Otherwise returns the result of the function.

Examples

iex> FunFunc.Nilable.get_or_else(1, fn -> 2 end)
1
iex> FunFunc.Nilable.get_or_else(nil, fn -> 2 end)
2
Link to this function map(x, f)
map(nilable(), func()) :: nilable()

Maps if the first argument is not nil. Otherwise returns nil.

Examples

iex> FunFunc.Nilable.map(1, &Integer.to_string/1)
"1"
iex> FunFunc.Nilable.map(nil, &Integer.to_string/1)
nil
Link to this function maybe(x, f, default)
maybe(nilable(), (... -> any()), any()) :: any()

Maps the first argument if it is not nil. Otherwise returns the default value.

Examples

iex> FunFunc.Nilable.maybe(1, &Integer.to_string/1, "default")
"1"
iex> FunFunc.Nilable.maybe(nil, &Integer.to_string/1, "default")
"default"
Link to this function maybe_else(x, f, default)
maybe_else(nilable(), (... -> any()), (... -> any())) :: any()

Maps the first argument if it is not nil. Otherwise returns the result of the function.

Examples

iex> FunFunc.Nilable.maybe_else(1, &Integer.to_string/1, fn -> "default" end)
"1"
iex> FunFunc.Nilable.maybe_else(nil, &Integer.to_string/1, fn -> "default" end)
"default"
Link to this function nil_and(x, y)
nil_and(nilable(), nilable()) :: nilable()

and

If the first argument is nil, nil is returned. Otherwise, the second argument is returned.

Examples

iex> FunFunc.Nilable.nil_and(nil, 2)
nil
iex> FunFunc.Nilable.nil_and(1, nil)
nil
iex> FunFunc.Nilable.nil_and(1, 2)
2
Link to this function nil_or(x, y)
nil_or(nilable(), nilable()) :: nilable()

or

If the first argument is nil, the second argument is returned. Otherwise, the first argument is returnded.

Examples

iex> FunFunc.Nilable.nil_or(nil, 2)
2
iex> FunFunc.Nilable.nil_or(1, nil)
1
iex> FunFunc.Nilable.nil_or(nil, nil)
nil