Bunch v1.3.0 Bunch.KVList View Source

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.

Link to this section 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.

map_keys(list, f) deprecated

Maps keys of list using function f.

Maps values of list using function f.

Link to this section Types

Link to this type

t(key, value)

View Source
t(key, value) :: [{key, value}]

Link to this section Functions

Link to this function

any_key?(list, f)

View Source
any_key?(t(k, v), (k -> as_boolean(term()))) :: boolean()
when k: any(), v: any()

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
Link to this function

any_value?(list, f)

View Source
any_value?(t(k, v), (v -> as_boolean(term()))) :: boolean()
when k: any(), v: any()

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
Link to this function

each_key(list, f)

View Source
each_key(t(k, v), (k -> any() | no_return())) :: :ok
when k: any(), v: any()

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
Link to this function

each_value(list, f)

View Source
each_value(t(k, v), (v -> any() | no_return())) :: :ok
when k: any(), v: any()

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
Link to this function

filter_by_keys(list, f)

View Source
filter_by_keys(t(k, v), (k -> as_boolean(term()))) :: t(k, v)
when k: any(), v: any()

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]
Link to this function

filter_by_values(list, f)

View Source
filter_by_values(t(k, v), (v -> as_boolean(term()))) :: t(k, v)
when k: any(), v: any()

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]
Link to this function

map_keys(list, f)

View Source
map_keys(t(k1, v), (k1 -> k2)) :: t(k2, v)
when k1: any(), k2: any(), v: any()
This function is deprecated. Use `Bunch.KVEnum` instead.

Maps keys of list using function f.

Example

iex> Bunch.KVList.map_keys([{1, :a}, {2, :b}], & &1+1)
[{2, :a}, {3, :b}]
Link to this function

map_values(list, f)

View Source
map_values(t(k, v1), (v1 -> v2)) :: t(k, v2)
when k: any(), v1: any(), v2: any()

Maps values of list using function f.

Example

iex> Bunch.KVList.map_values([a: 1, b: 2], & &1+1)
[a: 2, b: 3]