ex_maybe v1.1.1 ExMaybe

This library fills a bunch of important niches. A Maybe can help you with optional arguments, error handling, and records with optional fields.

Link to this section Summary

Functions

Convert result to maybe.

Transform a Maybe value with a given function.

Provide a default value, turning an optional value into a normal value.

Link to this section Types

Link to this type

result(error, value)
result(error, value) :: {:error, error} | {:ok, value}

Link to this type

t(value)
t(value) :: value | nil

Link to this section Functions

Link to this function

from_result(arg)
from_result(result(any(), a)) :: t(a)

Convert result to maybe.

Examples

iex> ExMaybe.from_result({:ok, 10})
10

iex> ExMaybe.from_result({:error, "Error message!!!"})
nil
Link to this function

map(maybe, fun)
map(t(a), (a -> b)) :: t(b) when a: var, b: var

Transform a Maybe value with a given function.

Examples

iex> ExMaybe.map(nil, fn(_) -> "TEST" end)
nil

iex> ExMaybe.map(10, fn(val) -> val + 1 end)
11
Link to this function

with_default(maybe, default)
with_default(t(a), a) :: a when a: var

Provide a default value, turning an optional value into a normal value.

Examples

iex> ExMaybe.with_default(nil, 10)
10

iex> ExMaybe.with_default(11, 20)
11