swiss v3.4.2 Swiss.Enum View Source
Helper functions for dealing with Enumerables.
Link to this section Summary
Functions
Calculates the average of values in an enumerable. Currently supports maps and lists only.
Finds an element and its index in enumerable
for which fun
returns true.
Finds the first element in enumerable
where its key
equals value
.
Returns default
if not found.
Finds the first element in enumerable
where its key
equals value
. Raises
if not found.
Same as Enum.group_by/3
but expects each group to have a single element, and
therefore returns only that element per key, instead of a list.
Finds the index of a value inside an enumerable.
Applies cb
to all elements in enum
, ignores the return and returns enum
.
Link to this section Functions
Calculates the average of values in an enumerable. Currently supports maps and lists only.
Examples
iex> Swiss.Enum.avg([1, 2, 3, 4])
2.5
iex> Swiss.Enum.avg([%{key: 1}, %{key: 2}, %{key: 3}, %{key: 4}], & &1.key)
2.5
iex> Swiss.Enum.avg(%{a: 1, b: 2, c: 3, d: 4}, &elem(&1, 1))
2.5
iex> Swiss.Enum.avg(%{})
0
iex> Swiss.Enum.avg([])
0
Finds an element and its index in enumerable
for which fun
returns true.
Examples
iex> Swiss.Enum.find_both([42, 44, 46], fn num -> num == 44 end)
{44, 1}
iex> Swiss.Enum.find_both([42, 44, 46], fn num -> num == 45 end)
{nil, nil}
find_by(enumerable, default \\ nil, key, value)
View Sourcefind_by(Enumerable.t(), any(), any(), any()) :: any()
Finds the first element in enumerable
where its key
equals value
.
Returns default
if not found.
Examples
iex> Swiss.Enum.find_by([%{life: 11}, %{life: 42}], :life, 42)
%{life: 42}
iex> Swiss.Enum.find_by([%{life: 11}, %{life: 42}], :wat, 42)
nil
iex> Swiss.Enum.find_by([%{life: 11}, %{life: 42}], 42, :wat, 42)
42
iex> Swiss.Enum.find_by([%Swiss.TestStruct{life: 42}], :life, 42)
%Swiss.TestStruct{life: 42}
find_by!(enumerable, key, value)
View Sourcefind_by!(Enumerable.t(), any(), any()) :: any()
Finds the first element in enumerable
where its key
equals value
. Raises
if not found.
Examples
iex> Swiss.Enum.find_by!([%{life: 11}, %{life: 42}], :life, 42)
%{life: 42}
iex> Swiss.Enum.find_by!([%{life: 11}, %{life: 42}], :wat, 42)
** (Swiss.Enum.KeyValueError) key :wat with value 42 not found in: [%{life: 11}, %{life: 42}]
Same as Enum.group_by/3
but expects each group to have a single element, and
therefore returns only that element per key, instead of a list.
Examples
iex> Swiss.Enum.group_by_single(
...> [%{k: "life", v: 42}, %{k: "death", v: 13}, %{k: "ooo", v: 0}],
...> & &1.k,
...> & &1.v
...> )
%{"life" => 42, "death" => 13, "ooo" => 0}
index_of(enum, value)
View Sourceindex_of(Enumerable.t(), any()) :: non_neg_integer() | nil
Finds the index of a value inside an enumerable.
Examples
iex> Swiss.Enum.index_of([1, 2, 3, 4], 3)
2
iex> Swiss.Enum.index_of([1, 2, 3, 4], 1)
0
iex> Swiss.Enum.index_of([1, 2, 3, 4], 5)
nil
Applies cb
to all elements in enum
, ignores the return and returns enum
.
Examples
iex> Swiss.Enum.thru([1, 2, 3], fn a -> a + 1 end)
[1, 2, 3]