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

Link to this function

any_key?(enum, f \\ & &1)

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

any_value?(enum, f \\ & &1)

View Source
@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 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
@spec each_key(t(k, v), (k -> any() | no_return())) :: :ok when k: any(), v: any()

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
@spec each_value(t(k, v), (v -> any() | no_return())) :: :ok when k: any(), v: any()

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
@spec filter_by_keys(t(k, v), (k -> as_boolean(term()))) :: t(k, v)
when k: any(), v: any()

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

filter_by_values(enum, 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 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]
@spec keys(t(key, value)) :: [key] when key: any(), value: any()

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]
@spec map_keys(t(k1, v), (k1 -> k2)) :: t(k2, v) when k1: any(), k2: any(), v: any()

Maps keys of enum using function f.

Example

iex> Bunch.KVEnum.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 enum using function f.

Example

iex> Bunch.KVEnum.map_values([a: 1, b: 2], & &1+1)
[a: 2, b: 3]
@spec values(t(key, value)) :: [value] when key: any(), value: any()

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]