View Source Bunch.KVEnum (Bunch v1.6.1)
A bunch of helper functions for manipulating key-value enums (including keyword enums).
Key-value enums are represented as enums of 2-element tuples, where the first element of each tuple is a key, and the second is a value.
Summary
Functions
Returns true
if f
returns truthy value for any key from enum
, otherwise false
.
Returns true
if f
returns truthy value for any value from enum
, otherwise false
.
Executes f
for each key in enum
.
Executes f
for each value in enum
.
Filters elements of enum
by keys using function f
.
Filters elements of enum
by values using function f
.
Returns all keys from the enum
.
Maps keys of enum
using function f
.
Maps values of enum
using function f
.
Returns all values from the enum
.
Types
@type t(_key, _value) :: Enumerable.t()
Functions
Returns true
if f
returns truthy value for any key from enum
, otherwise false
.
Example
iex> Bunch.KVEnum.any_key?([a: 1, b: 2, a: 3], & &1 == :b)
true
iex> Bunch.KVEnum.any_key?([a: 1, b: 3, a: 5], & &1 == :c)
false
Returns true
if f
returns truthy value for any value from enum
, otherwise false
.
Example
iex> Bunch.KVEnum.any_value?([a: 1, b: 2, a: 3], & &1 |> rem(2) == 0)
true
iex> Bunch.KVEnum.any_value?([a: 1, b: 3, a: 5], & &1 |> rem(2) == 0)
false
Executes f
for each key in enum
.
Example
iex> Bunch.KVEnum.each_key([a: 1, b: 2, a: 3], & send(self(), &1))
iex> [:a, :b, :a] |> Enum.each(&receive do ^&1 -> :ok end)
:ok
Executes f
for each value in enum
.
Example
iex> Bunch.KVEnum.each_value([a: 1, b: 2, a: 3], & send(self(), &1))
iex> 1..3 |> Enum.each(&receive do ^&1 -> :ok end)
:ok
Filters elements of enum
by keys using function f
.
Example
iex> Bunch.KVEnum.filter_by_keys([a: 1, b: 2, a: 3], & &1 == :a)
[a: 1, a: 3]
Filters elements of enum
by values using function f
.
Example
iex> Bunch.KVEnum.filter_by_values([a: 1, b: 2, a: 3], & &1 |> rem(2) == 0)
[b: 2]
Returns all keys from the enum
.
Duplicated keys appear duplicated in the final enum of keys.
Examples
iex> Bunch.KVEnum.keys(a: 1, b: 2)
[:a, :b]
iex> Bunch.KVEnum.keys(a: 1, b: 2, a: 3)
[:a, :b, :a]
Maps keys of enum
using function f
.
Example
iex> Bunch.KVEnum.map_keys([{1, :a}, {2, :b}], & &1+1)
[{2, :a}, {3, :b}]
Maps values of enum
using function f
.
Example
iex> Bunch.KVEnum.map_values([a: 1, b: 2], & &1+1)
[a: 2, b: 3]
Returns all values from the enum
.
Values from duplicated keys will be kept in the final enum of values.
Examples
iex> Bunch.KVEnum.values(a: 1, b: 2)
[1, 2]
iex> Bunch.KVEnum.values(a: 1, b: 2, a: 3)
[1, 2, 3]