Toolbox.Utils.Enum (toolbox v1.1.0)

A set of utility functions for Enums.

Link to this section Summary

Functions

Returns :ok if fun applied to every element of enumerable returns :ok. Otherwise it returns the first error occurred during fun call.

Flattens the enum by the specified amount of levels.

Returns {:ok, list_of_values} if fun applied to every element of enumerable returns {:ok, value}. Otherwise it returns the first error occurred during fun call.

Invokes fun for each element in the enumerable with the accumulator. If every fun call returns {:ok, new_acc}, functions returns {:ok, last_acc}. Otherwise it returns the first error occurred during fun call.

Link to this section Functions

Link to this function

each_while_ok(enumerable, fun)

@spec each_while_ok(Enumerable.t(), (any() -> result)) :: result
when result: :ok | {:error, reason :: any()}

Returns :ok if fun applied to every element of enumerable returns :ok. Otherwise it returns the first error occurred during fun call.

example

Example

iex> each_while_ok([], fn _ -> :not_called_at_all end)
:ok

iex> each_while_ok([1, 2, 3], fn _ -> :ok end)
:ok

iex> each_while_ok([1, 2, 3], &(if &1 == 2, do: {:error, &1}, else: :ok))
{:error, 2}
Link to this function

flatten_levels(list, levels)

Flattens the enum by the specified amount of levels.

Flattens only the specified amount of levels, contrary to the standard List.flatten/2 which flattens the list recursively. Handy if you e.g. want to flatten only the first level.

Importantly, this operation maintains order of the elements in the list.

examples

Examples

iex> flatten_levels([[:hello, :team], ["this", "is"], ["now", ["flat", ["list"]]]], 1)
[:hello, :team, "this", "is", "now", ["flat", ["list"]]]

iex> flatten_levels([[:hello, :team], ["this", "is"], ["now", ["flat", ["list"]]]], 2)
[:hello, :team, "this", "is", "now", "flat", ["list"]]

iex> flatten_levels([[:hello, :team], ["this", "is"], ["now", ["flat", ["list"]]]], 3)
[:hello, :team, "this", "is", "now", "flat", "list"]

iex> flatten_levels([[:hello, :team], ["this", "is"], ["now", ["flat", ["list"]]]], 4)
[:hello, :team, "this", "is", "now", "flat", "list"]

iex> flatten_levels([[:hello, :team], ["this", "is"], ["now", ["flat", ["list"]]]], 0)
[[:hello, :team], ["this", "is"], ["now", ["flat", ["list"]]]]
Link to this function

map_while_ok(enumerable, fun)

@spec map_while_ok(
  Enumerable.t(),
  (any() -> {:ok, any()} | {:error, reason :: any()})
) ::
  {:ok, list()} | {:error, reason :: any()}

Returns {:ok, list_of_values} if fun applied to every element of enumerable returns {:ok, value}. Otherwise it returns the first error occurred during fun call.

example

Example

iex> map_while_ok([], fn _ -> :not_called_at_all end)
{:ok, []}

iex> map_while_ok([1, 2, 3], &({:ok, &1 * 2}))
{:ok, [2, 4, 6]}

iex> map_while_ok([1, 2, 3], &(if &1 == 2, do: {:error, &1 * 2}, else: {:ok, &1 * 2}))
{:error, 4}
Link to this function

reduce_while_ok(enumerable, acc, fun)

@spec reduce_while_ok(
  Enumerable.t(),
  any(),
  (any(), any() -> {:ok, any()} | {:error, [{:reason, any()}]})
) :: {:ok, any()} | {:error, reason :: any()}

Invokes fun for each element in the enumerable with the accumulator. If every fun call returns {:ok, new_acc}, functions returns {:ok, last_acc}. Otherwise it returns the first error occurred during fun call.

example

Example

iex> reduce_while_ok([], :initial, fn _, _ -> :not_called_at_all end)
{:ok, :initial}

iex> reduce_while_ok([1, 2, 3], 0, &({:ok, &1 + &2}))
{:ok, 6}

iex> reduce_while_ok([1, 2, 3], 0, &(if &1 == 2, do: {:error, &1 + &2}, else: {:ok, &1 + &2}))
{:error, 3}