Additional utility functions to extend the power of Elixir's Enum module.
Similar to filter/2
, but returns the value of the function invocation instead of the element itself.
assert Enumx.filter_value([1, 2, 3, 4, 5], &(&1 > 3 && &1 * 2)) == [8, 10]
assert Enumx.filter_value([foo: 1, bar: 2, baz: 3], fn {k, v} -> v > 1 && k end) == [:bar, :baz]
assert Enumx.filter_value(%{foo: 1, bar: 2, baz: 3}, fn {k, v} -> v > 1 && k end) == [:bar, :baz]
Returns true
if all elements in enumerable
are equal.
assert Enumx.all_equal?([1, 1])
assert Enumx.all_equal?([foo: 1, foo: 1])
Returns true
if all elements in enumerable
are equal.
assert Enumx.unique_value!([1, 1]) == 1
assert Enumx.unique_value!(%{foo: 1}) == {:foo, 1}
assert Enumx.unique_value!([foo: 1, foo: 1]) == {:foo, 1}
assert_raise ArgumentError, fn ->
Enumx.unique_value!([]) # raises error: cannot call `unique_value!/1` on an empty list
end
assert_raise RuntimeError, fn ->
Enumx.unique_value!([1, 2]) # raises error: elements in the list [1, 2] are not equal
end
Add enumx
for Elixir as a dependency in your mix.exs
file:
def deps do
[
{:enumx, "~> 0.2.0"}
]
end
HexDocs documentation can be found at https://hexdocs.pm/enumx.