View Source Bunch.KVList (Bunch v1.6.1)
A bunch of helper functions for manipulating key-value lists (including keyword lists).
Key-value lists are represented as lists 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 list, otherwise false.
Returns true if f returns truthy value for any value from list, otherwise false.
Executes f for each key in list.
Executes f for each value in list.
Filters elements of list by keys using function f.
Filters elements of list by values using function f.
Maps keys of list using function f.
Maps values of list using function f.
Types
@type t(key, value) :: [{key, value}]
Functions
Returns true if f returns truthy value for any key from list, otherwise false.
Example
iex> Bunch.KVList.any_key?([a: 1, b: 2, a: 3], & &1 == :b)
true
iex> Bunch.KVList.any_key?([a: 1, b: 3, a: 5], & &1 == :c)
false
Returns true if f returns truthy value for any value from list, otherwise false.
Example
iex> Bunch.KVList.any_value?([a: 1, b: 2, a: 3], & &1 |> rem(2) == 0)
true
iex> Bunch.KVList.any_value?([a: 1, b: 3, a: 5], & &1 |> rem(2) == 0)
false
Executes f for each key in list.
Example
iex> Bunch.KVList.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 list.
Example
iex> Bunch.KVList.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 list by keys using function f.
Example
iex> Bunch.KVList.filter_by_keys([a: 1, b: 2, a: 3], & &1 == :a)
[a: 1, a: 3]
Filters elements of list by values using function f.
Example
iex> Bunch.KVList.filter_by_values([a: 1, b: 2, a: 3], & &1 |> rem(2) == 0)
[b: 2]
Maps keys of list using function f.
Example
iex> Bunch.KVList.map_keys([{1, :a}, {2, :b}], & &1+1)
[{2, :a}, {3, :b}]
Maps values of list using function f.
Example
iex> Bunch.KVList.map_values([a: 1, b: 2], & &1+1)
[a: 2, b: 3]