Rivet.Utils.Enum (rivet_utils v2.7.0)

Copy Markdown 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.

map_while_ok maps then aggregates [{:ok, result1}, {:ok, result2}, ...] into {:ok, results} map_while_ok short circuits if an element maps to {:error, err}

ok_flat_map(a, b) deprecated
ok_map(a, b) deprecated
scmap(a, b) deprecated

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")
{:min, ["30 m", "30", "m"]}
iex> enum_rx(opts, "1.5 hr") # doesn't match because of the period
nil

flat_map_while_ok(elems, fxn)

@spec flat_map_while_ok([a], (a -> {:ok, [b]} | {:error, e})) ::
  {:error, e} | {:ok, [b]}
when a: term(), b: term(), e: term()
iex> flat_map_while_ok([1,2,3], fn x -> {:ok, [x + 1]} end)
{:ok, [2,3,4]}
iex> flat_map_while_ok([3,4,6], fn 5 -> {:error, "BAD"}; x -> {:ok, [x]} end)
{:ok, [3,4,6]}
iex> flat_map_while_ok(["i", "i", "o"], fn x -> {:ok, ["e", x]} end)
{:ok, ["e", "i", "e", "i", "e", "o"]}
iex> flat_map_while_ok([3,4,5,6], fn 5 -> {:error, "BAD"}; x -> {:ok, [x]} end)
{:error, "BAD"}
iex> flat_map_while_ok([], &(&1))
{:ok, []}

flat_map_while_ok(list, acc, fxn)

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

map_only_ok(elems, fxn)

iex> map_only_ok([{:ok, 1}, {:error, "bad"}, {:ok, 2}, {:error, "bad"}], &(&1))
{:ok, [1, 2]}
iex> map_only_ok([3, 4, 5, 6], fn 5 -> {:error, "BAD"}; x -> {:ok, x} end)
{:ok, [3, 4, 6]}
iex> map_only_ok([], &(&1))
{:ok, []}

map_only_ok(list, results, fxn)

map_while_ok(elems, fxn)

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

map_while_ok maps then aggregates [{:ok, result1}, {:ok, result2}, ...] into {:ok, results} map_while_ok short circuits if an element maps to {:error, err}

iex> map_while_ok([1,2,3], fn x -> {:ok, x + 1} end)
{:ok, [2,3,4]}
iex> map_while_ok([3,4,6], fn 5 -> {:error, "BAD"}; x -> {:ok, x} end)
{:ok, [3,4,6]}
iex> map_while_ok([3,4,5,6], fn 5 -> {:error, "BAD"}; x -> {:ok, x} end)
{:error, "BAD"}
iex> map_while_ok([], &(&1))
{:ok, []}

map_while_ok(list, results, fxn)

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

ok_flat_map(a, b)

This function is deprecated. Use flat_map_while_ok/2 instead.

ok_map(a, b)

This function is deprecated. Use map_while_ok/2 instead.

reduce_while_ok(list, acc, fxn)

@spec reduce_while_ok([a], b, (a, b -> {:ok, b} | {:error, e})) ::
  {:error, e} | {:ok, b}
when a: term(), b: term(), e: term()
iex> reduce_while_ok([1,2,3], 0, fn x, acc -> {:ok, x + acc} end)
{:ok, 6}
iex> reduce_while_ok([1,2,-1,3], 0, fn -1, _ -> {:error, :out_of_bounds}; x, acc -> {:ok, x + acc};  end)
{:error, :out_of_bounds}

scmap(a, b)

This function is deprecated. Use map_while_ok/2 instead.