Rivet.Utils.Enum (rivet_utils v2.0.8)

View Source

Contributor: Brandon Gillespie

Summary

Functions

like Enum.find_value() on a list of [{rx, fn}, ..], calling fn on the matched rx and returning the result.

short-circuit map: like regular map but short circuits if an element maps to {:error, err}

Functions

enum_rx(elems, str)

like Enum.find_value() on a list of [{rx, fn}, ..], calling fn on the matched rx and returning the result.

iex> opts = [ ...> {~r/^(\d+)\s(m|min(s)?|minute(s)?)$/, fn match, _ -> {:min, match} end}, ...> {~r/^(\d+)\s(h|hour(s)?|hr(s)?)$/, fn match, _ -> {:hr, match} end}, ...> ] ...> enum_rx(opts, "30 m")

iex> enum_rx(opts, "1.5 hr") # doesn't match because of the period nil

scmap(elems, fxn)

short-circuit map: like regular map but short circuits if an element maps to {:error, err}

iex> scmap([1,2,3], fn x -> {:ok, x + 1} end)

iex> scmap([3,4,6], fn 5 -> {:error, "BAD"}; x -> {:ok, x} end)

iex> scmap([3,4,5,6], fn 5 -> {:error, "BAD"}; x -> {:ok, x} end)

scmap(list, results, fxn)

@spec scmap([a], [b], (a -> {:ok, b} | {:error, e})) :: {:error, e} | {:ok, [b]}
when a: term(), b: term(), e: term()