View Source ExAequoBase.Enum (ExAequoBase v0.1.1)
Extensions to Enum
Summary
Functions
map_ok
wrapps map
around functions that return result types result_t
Types
@type atoms() :: [atom()]
@type binaries() :: [binary()]
@type error_t() :: {:error, binary()}
@type error_t(t) :: {:error, t}
@type maybe(t) :: nil | t
@type natural() :: non_neg_integer()
@type numbered(t) :: {t, number()}
@type numbered_lines_t() :: [numbered_line_t()]
@type ok_t() :: {:ok, any()}
@type ok_t(t) :: {:ok, t}
Functions
@spec map_ok(Enumerable.t(), result_fun_t(), boolean()) :: result_t()
map_ok
wrapps map
around functions that return result types result_t
iex(1)> map_ok(1..2, fn x -> {:ok, x + 1} end)
{:ok, [2, 3]}
iex(2)> map_ok(0..2, fn x -> if rem(x, 2) == 0, do: {:ok, x + 1}, else: {:error, "not even even"} end)
{:error, "not even even"}
If the mapper function returning nil shall be captured as an error you need to allow this explicitly
iex(3)> map_ok(0..2, fn x -> if rem(x, 2) == 0, do: {:ok, x} end, true)
{:error, "mapper function returned nil on 1"}
but...
iex(4)> assert_raise(
...(4)> CaseClauseError,
...(4)>fn -> map_ok(0..2, fn x -> if rem(x, 2) == 0, do: {:ok, x} end) end)