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

func()
func() :: (nilable -> any)
nilable()
nilable() :: nil | any

Functions

from_list(list)
from_list([] | [any]) :: nilable

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
get_or(val, default)
get_or(nilable, any) :: any

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"
map(val, fun)
map(nilable, func) :: any

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
maybe(val, fun, default)
maybe(nilable, func, any) :: any

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"
to_list(val)
to_list(nilable) :: [] | [any]

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]