Extra v0.2.0 Enum.Extra

Extensions to the standard library’s Enum module.

Link to this section Summary

Functions

Runs the fn for each element in the enum, or short circuits if fn returns an error for any given item, returning the error

Returns a Map.t where the elements in list are indexed by the value returned by calling index_fn on each element. The last writer wins in this implementation

Applies mapping_fun if predicate is not false or nil, otherwise returns enum

Runs the func over every value in the Enum.t, returning ({:ok, Enum.t}) if all the funcs return {:ok, term}

Behaves like Enum.reduce/3 but will short-circuit if fun returns an error tuple

Returns the given enum with the keys in keymap renamed to their corresponding values in keymap. Keys in keymap that don’t exist in the passed enum will be ignored

Returns whether enum is unique

Link to this section Functions

Link to this function each_or_error(enum, func)
each_or_error(Enum.t, (term -> {:error, reason} | any)) ::
  :ok |
  {:error, reason} when reason: any

Runs the fn for each element in the enum, or short circuits if fn returns an error for any given item, returning the error.

Link to this function index_by(list, index_fn)
index_by([map], (map -> any)) :: %{optional(any) => map}

Returns a Map.t where the elements in list are indexed by the value returned by calling index_fn on each element. The last writer wins in this implementation.

Examples

iex> txs = [%{id: "A", amt: 10_000}, %{id: "B", amt: 15_000}]
...> txs |> Enum.Extra.index_by(& &1.id)
%{"A" => %{id: "A", amt: 10_000}, "B" => %{id: "B", amt: 15_000}}
Link to this function map_if(enum, predicate, mapping_fun)
map_if(Enum.t, boolean | nil, (any -> any)) :: Enum.t

Applies mapping_fun if predicate is not false or nil, otherwise returns enum.

Examples

iex> Enum.Extra.map_if([1, 2, 3], true, &(&1 + 1))
[2, 3, 4]

iex> Enum.Extra.map_if([1, 2, 3], false, &(&1 + 1))
[1, 2, 3]
Link to this function map_or_error(enum, func, opts \\ [])
map_or_error(Enum.t, (term -> {:ok, term} | {:error, reason}), [{:into, Collectable.t}]) ::
  {:ok, Enum.t} |
  {:error, reason} when reason: any

Runs the func over every value in the Enum.t, returning ({:ok, Enum.t}) if all the funcs return {:ok, term}.

Short circuits if func does not return {:ok, term}.

Examples

iex> Enum.Extra.map_or_error(%{a: 1, b: 2}, fn {key, val} -> {:ok, {key, val * 2}} end)
{:ok, %{a: 2, b: 4}}

iex> Enum.Extra.map_or_error([a: 1, b: 2], fn {key, val} -> {:ok, {key, val * 2}} end)
{:ok, [a: 2, b: 4]}

iex> Enum.Extra.map_or_error([1, 2], fn val -> {:ok, val * 2} end)
{:ok, [2, 4]}

Options

  • :into: if passed, the passed Enum.t will be collected into :into. By default, this function will attempt to push the Enum.t into the same structure that was passed in (a map or list).
Link to this function reduce_or_error(enum, acc, fun)
reduce_or_error(Enum.t, any, (Enum.element, any -> {:ok, any} | {:error, any})) ::
  {:ok, any} |
  {:error, any}

Behaves like Enum.reduce/3 but will short-circuit if fun returns an error tuple.

Examples

iex> Enum.Extra.reduce_or_error(%{a: 1, b: 2}, 0, fn {_key, val}, count -> {:ok, val + count} end)
{:ok, 3}

iex> Enum.Extra.reduce_or_error([1, 2], 0, fn _val, _acc -> {:error, :fail} end)
{:error, :fail}
Link to this function rename_keys(enum, keymap)
rename_keys(Enum.t, keymap :: map) :: keyword

Returns the given enum with the keys in keymap renamed to their corresponding values in keymap. Keys in keymap that don’t exist in the passed enum will be ignored

Examples

iex> Enum.Extra.rename_keys([foo: “bar”, baz: “qux”], %{foo: :new_foo, a: :b}) [new_foo: “bar”, baz: “qux”]

iex> Enum.Extra.rename_keys(%{foo: “bar”, baz: “qux”}, %{foo: :new_foo, a: :b}) %{new_foo: “bar”, baz: “qux”}

iex> Enum.Extra.rename_keys([foo: “bar”], %{a: :b}) [foo: “bar”]

Link to this function unique?(enum)
unique?(Enum.t) :: boolean

Returns whether enum is unique.

Note that this implementation will stop enumerating as soon as it finds a non-unique entry (returning false) making it more efficient than simply comparing Enum.uniq(enum) to the original enum.

Examples

iex> Enum.Extra.unique?([1, 2, 3])
true

iex> Enum.Extra.unique?([1, 1, 2])
false