Module
pooka/dict
src/pooka/dict.pookaKeep the keys in a small cabinet whose earlier drawers remain intact.
Dict wraps native immutable Erlang maps behind typed storage capabilities. Updates return a new dictionary. Iteration order is unspecified, duplicate input keys keep their last value, and equals compares contents rather than closure identities.
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 typed native-map capabilities. Prefer empty and from_list when constructing public dictionaries.
new(storage: Storage(k, v))
fn(pooka/internal/map.Storage(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()
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/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/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/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, independent of storage closure identity.
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)
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)