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.

map_keys(list, f) deprecated

Maps keys of list using function f.

Maps values of list using function f.

Types

@type t(key, value) :: [{key, value}]

Functions

@spec 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
@spec 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
@spec 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
@spec 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
@spec 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
@spec 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]
This function is deprecated. Use `Bunch.KVEnum` instead.
@spec map_keys(t(k1, v), (k1 -> k2)) :: t(k2, v) when k1: any(), k2: any(), v: any()

Maps keys of list using function f.

Example

iex> Bunch.KVList.map_keys([{1, :a}, {2, :b}], & &1+1)
[{2, :a}, {3, :b}]
@spec 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]