Module
pooka/dict
src/pooka/dict.pookaKeep the keys in a small cabinet whose earlier drawers remain intact.
A Dict holds one immutable Erlang map. Updates return a new dictionary without changing the earlier one. Iteration order is unspecified, and duplicate input keys keep their last value.
import pooka/dict
assert dict.from_list([#("oak", 3)]).get("oak") == Some(3)
API reference
class Dict(k, v)
An immutable key/value cabinet backed by a typed native map. Prefer empty and from_list when constructing public dictionaries.
new(storage: _Map(k, v))
fn(pooka/dict._Map(k, v)) -> pooka/dict.Dict(k, v)Create the value from its declared fields. Public factory functions keep the storage details out of ordinary calling code.
get(key: k) -> Option(v)
fn(k) -> Option(v)Look up one key without treating absence as an exception.
import pooka/dict
assert dict.from_list([#("oak", 1)]).get("oak") == Some(1)
insert(key: k, value: v) -> Dict(k, v)
fn(k, v) -> pooka/dict.Dict(k, v)Return a dictionary with the key associated to the new value.
import pooka/dict
assert dict.empty().insert("oak", 2).get("oak") == Some(2)
delete(key: k) -> Dict(k, v)
fn(k) -> pooka/dict.Dict(k, v)Return a dictionary without the key. Removing an absent key is harmless.
import pooka/dict
assert dict.from_list([#("oak", 1)]).delete("oak").is_empty()
get_or(key: k, fallback: v) -> v
fn(k, v) -> vRead a key or return the supplied fallback. An existing value is kept.
import pooka/dict
assert dict.empty().get_or("owl", 0) == 0
upsert(key: k, change: fn(Option(v)) -> v) -> Dict(k, v)
fn(k, fn(Option(v)) -> v) -> pooka/dict.Dict(k, v)Let the callback see the previous value, or None for a new key. Store its result in a new dictionary, leaving the earlier dictionary untouched. The callback is called once, whether or not the key already exists.
import pooka/dict
assert dict.empty().upsert("owl", fn(_) { 1 }).get("owl") == Some(1)
has_key(key: k) -> Bool
fn(k) -> BoolCheck membership without exposing the stored value.
import pooka/dict
assert dict.from_list([#("oak", 1)]).has_key("oak")
size() -> Int
fn() -> IntCount distinct keys.
import pooka/dict
assert dict.from_list([#("oak", 1)]).size() == 1
is_empty() -> Bool
fn() -> BoolAsk whether no keys are stored.
import pooka/dict
assert dict.empty().is_empty()
keys() -> List(k)
fn() -> List(k)Enumerate keys in unspecified order.
import pooka/dict
import pooka/internal/traversal as list
assert list.contains(dict.from_list([#("oak", 1)]).keys(), "oak")
values() -> List(v)
fn() -> List(v)Enumerate stored values in unspecified order.
import pooka/dict
import pooka/internal/traversal as list
assert list.contains(dict.from_list([#("oak", 1)]).values(), 1)
to_list() -> List(#(k, v))
fn() -> List(#(k, v))Enumerate key/value pairs in unspecified order.
import pooka/dict
import pooka/internal/traversal as list
assert list.contains(dict.from_list([#("oak", 1)]).to_list(), #("oak", 1))
map_values(transform: fn(v) -> b) -> Dict(k, b)
fn(fn(v) -> a) -> pooka/dict.Dict(k, a)Transform each value while retaining keys. The value type may change.
import pooka/dict
assert dict.from_list([#("oak", 1)]).map_values(fn(x) { x == 1 }).get("oak") == Some(True)
filter(keep: fn(k, v) -> Bool) -> Dict(k, v)
fn(fn(k, v) -> Bool) -> pooka/dict.Dict(k, v)Keep pairs whose key-and-value predicate returns True.
import pooka/dict
assert dict.from_list([#("oak", 1)]).filter(fn(_, value) { value > 1 }).is_empty()
fold(initial: b, step: fn(b, k, v) -> b) -> b
fn(a, fn(a, k, v) -> a) -> aFold with accumulator, key and value. Entry order is unspecified.
import pooka/dict
assert dict.from_list([#("oak", 2)]).fold(0, fn(acc, _, value) { acc + value }) == 2
equals(right: Dict(k, v)) -> Bool
fn(pooka/dict.Dict(k, v)) -> BoolCompare keys and values regardless of insertion order.
import pooka/dict
assert dict.from_list([#("oak", 1)]).equals(dict.from_list([#("oak", 1)]))
merge(right: Dict(k, v)) -> Dict(k, v)
fn(pooka/dict.Dict(k, v)) -> pooka/dict.Dict(k, v)Merge dictionaries, taking the right-hand value for a shared key.
import pooka/dict
assert dict.from_list([#("oak", 1)]).merge(dict.from_list([#("oak", 2)])).get("oak") == Some(2)
combine(right: Dict(k, v), resolve: fn(k, v, v) -> v) -> Dict(k, v)
fn(pooka/dict.Dict(k, v), fn(k, v, v) -> v) -> pooka/dict.Dict(k, v)Merge both cabinets, asking the callback only for shared keys.
assert dict.from_list([#("a", 1)]).combine(dict.from_list([#("a", 2)]), fn(_, a, b) { a + b }).get("a") == Some(3)
take(keys: List(k)) -> Dict(k, v)
fn(List(k)) -> pooka/dict.Dict(k, v)Keep only requested keys; missing and repeated keys are harmless.
assert dict.from_list([#("a", 1)]).take(["a", "missing"]).size() == 1
drop(keys: List(k)) -> Dict(k, v)
fn(List(k)) -> pooka/dict.Dict(k, v)Remove requested keys, leaving the original dictionary intact.
assert dict.from_list([#("a", 1)]).drop(["a"]).is_empty()
each(visit: fn(k, v) -> Nil) -> Nil
fn(fn(k, v) -> Nil) -> NilVisit every pair once. Dictionary iteration order is unspecified.
dict.from_list([#("a", 1)]).each(fn(_, value) { assert value == 1 })
empty() -> Dict(k, v)
fn() -> pooka/dict.Dict(a, b)Open an empty typed dictionary.
import pooka/dict
assert dict.empty().is_empty()
from_list(pairs: List(#(k, v))) -> Dict(k, v)
fn(List(#(a, b))) -> pooka/dict.Dict(a, b)Build a dictionary from pairs. For repeated keys, the last pair wins.
import pooka/dict
assert dict.from_list([#("oak", 1), #("oak", 2)]).get("oak") == Some(2)