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 either(lt, rt) :: ok_t(lt) | error_t(rt)
@type error_t() :: {:error, binary()}
@type error_t(t) :: {:error, t}
@type input_source_t() :: Enumerable.t() | binary() | binaries()
@type maybe(t) :: nil | t
@type natural() :: non_neg_integer()
@type numbered(t) :: {t, number()}
@type numbered_line_t() :: numbered(binary())
@type numbered_lines_t() :: [numbered_line_t()]
@type ok_t() :: {:ok, any()}
@type ok_t(t) :: {:ok, t}
@type reducer_result_t() :: {:halt, error_t()} | {:cont, ok_t()}
@type result_fun_t() :: (any() -> result_t())
@type result_fun_t(t) :: (any() -> result_t(t))
@type result_t() :: either(any(), binary())
@type result_t(t) :: either(t, binary())
@type stream_t() ::
  %IO.Stream{device: term(), line_or_bytes: term(), raw: term()}
  | %File.Stream{
      line_or_bytes: term(),
      modes: term(),
      node: term(),
      path: term(),
      raw: term()
    }

Functions

Link to this function

map_ok(enum, fun, nil_is_error \\ false)

View Source
@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)