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
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.
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}}
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]
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 (amaporlist).
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}
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”]
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