func v0.4.1 Func.Maybe
Functions that inspired by Haskell, Data.Maybe. Data.Maybe)
Summary
Functions
If the list is empty, it returns nil. Otherwise, it returns the first element of the list. In other words, safe head function
Gets the value. If it is nil, returns the default value
If it isn’t nil, maps the value
If the value is nil, the function returns the default value. Otherwise, it applys the function to the value
It returns the singleton list when not nil. If the value is nil, it returns an empty list
Types
Functions
If the list is empty, it returns nil. Otherwise, it returns the first element of the list. In other words, safe head function.
iex> Func.Maybe.from_list([])
nil
iex> Func.Maybe.from_list([1])
1
iex> Func.Maybe.from_list([1, 2])
1
Gets the value. If it is nil, returns the default value.
iex> Func.Maybe.get_or(1, "default")
1
iex> Func.Maybe.get_or(nil, "default")
"default"
If it isn’t nil, maps the value.
iex> Func.Maybe.map(1, &Integer.to_string/1)
"1"
iex> Func.Maybe.map(nil, & &1)
nil
If the value is nil, the function returns the default value. Otherwise, it applys the function to the value.
iex> Func.Maybe.maybe(nil, & &1, "default")
"default"
iex> Func.Maybe.maybe(1, &Integer.to_string/1, "default")
"1"
It returns the singleton list when not nil. If the value is nil, it returns an empty list.
iex> Func.Maybe.to_list(nil)
[]
iex> Func.Maybe.to_list(1)
[1]