Dict
A dictionary maps keys to values, typed Dict<K, V>. See Dictionaries and
sets for the rules on keys.
@type Dict<K, V>| Function | Summary |
|---|---|
Dict.empty |
A dictionary with no entries. |
Dict.with_capacity |
An empty dictionary with room for capacity entries, so inserting that many never grows it. |
Dict.single |
A dictionary with one entry. |
Dict.from_list |
A dictionary from (key, value) tuples; a later tuple replaces an earlier one with the same key. |
Dict.to_list |
The entries as (key, value) tuples, in insertion order. |
Dict.len |
The number of entries. |
Dict.is_empty |
Whether the dictionary has no entries. |
Dict.get |
The value for key, or :Err(:KeyNotFound). |
Dict.contains |
Whether the dictionary has an entry for key. |
Dict.insert |
Adds an entry, or replaces the value of an existing key in place. |
Dict.remove |
Removes the entry for key, if there is one. |
Dict.update |
Sets the entry for key from f, which gets :Ok(value) or :Err(:Missing) and returns either; :Err removes the entry. |
Dict.keys |
The keys, in insertion order. |
Dict.values |
The values, in insertion order. |
Dict.walk |
Folds over the entries: f gets the state, a key and its value. |
Dict.walk_until |
Folds the entries until f returns :Break(state). |
Dict.map |
Replaces every value with f of its key and value. |
Dict.join_map |
Unions the dictionaries f returns for each entry; a later entry’s value wins. |
Dict.keep_if |
The entries for which predicate of the key and value is TRUE. |
Dict.drop_if |
The entries for which predicate is FALSE. |
Dict.insert_all |
The entries of both; where a key is in both, second’s value wins. |
Dict.keep_shared |
The entries of first whose key and value are both in second. |
Dict.remove_all |
The entries of first whose key is not in second. |
Dict.reserve |
The dictionary with room for spare more entries. |
Dict.capacity |
How many entries the dictionary holds before its storage must grow. |
Dict.release_excess_capacity |
The dictionary with no room beyond its entries. |
Dict.clear |
An empty dictionary of the same type. |
Dict.empty
Section titled “Dict.empty”Dict.empty <- function<K: key, V>() -> Dict<K, V>#: <K: key, V> fn() -> Dict<K, V>Dict.empty <- function()A dictionary with no entries.
Dict.empty()#> Dict.empty()Dict.with_capacity
Section titled “Dict.with_capacity”Dict.with_capacity <- function<K: key, V>(capacity: integer) -> Dict<K, V>#: <K: key, V> fn(capacity: integer) -> Dict<K, V>Dict.with_capacity <- function(capacity)An empty dictionary with room for capacity entries, so inserting that many never grows it. The
room is rounded up to a power of two.
Dict.capacity(Dict.insert(Dict.with_capacity(64L), "a", 1L))#> 64LDict.single
Section titled “Dict.single”Dict.single <- function<K: key, V>(key: K, value: V) -> Dict<K, V>#: <K: key, V> fn(key: K, value: V) -> Dict<K, V>Dict.single <- function(key, value)A dictionary with one entry.
Dict.single("a", 1)#> Dict.from_list(list(("a", 1)))Dict.from_list
Section titled “Dict.from_list”Dict.from_list <- function<K: key, V>(pairs: list[Tuple{K, V}]) -> Dict<K, V>#: <K: key, V> fn(pairs: list[Tuple{K, V}]) -> Dict<K, V>Dict.from_list <- function(pairs)A dictionary from (key, value) tuples; a later tuple replaces an earlier one with the same key.
Dict.from_list(list(("a", 1), ("b", 2), ("a", 3)))#> Dict.from_list(list(("a", 3), ("b", 2)))Dict.to_list
Section titled “Dict.to_list”Dict.to_list <- function<K: key, V>(dict: Dict<K, V>) -> list[Tuple{K, V}]#: <K: key, V> fn(dict: Dict<K, V>) -> list[Tuple{K, V}]Dict.to_list <- function(dict)The entries as (key, value) tuples, in insertion order.
Dict.to_list(Dict.from_list(list(("a", 1), ("b", 2))))#> list(("a", 1), ("b", 2))Dict.len
Section titled “Dict.len”Dict.len <- function<K: key, V>(dict: Dict<K, V>) -> integer#: <K: key, V> fn(dict: Dict<K, V>) -> integerDict.len <- function(dict)The number of entries.
Dict.len(Dict.single("a", 1))#> 1LDict.is_empty
Section titled “Dict.is_empty”Dict.is_empty <- function<K: key, V>(dict: Dict<K, V>) -> logical#: <K: key, V> fn(dict: Dict<K, V>) -> logicalDict.is_empty <- function(dict)Whether the dictionary has no entries.
Dict.is_empty(Dict.empty())#> TRUEDict.get
Section titled “Dict.get”Dict.get <- function<K: key, V>(dict: Dict<K, V>, key: K) -> Result<V, :KeyNotFound>#: <K: key, V> fn(dict: Dict<K, V>, key: K) -> Result<V, :KeyNotFound>Dict.get <- function(dict, key)The value for key, or :Err(:KeyNotFound).
Dict.get(Dict.single("a", 1), "a")#> :Ok(1)Dict.contains
Section titled “Dict.contains”Dict.contains <- function<K: key, V>(dict: Dict<K, V>, key: K) -> logical#: <K: key, V> fn(dict: Dict<K, V>, key: K) -> logicalDict.contains <- function(dict, key)Whether the dictionary has an entry for key.
Dict.contains(Dict.single("a", 1), "b")#> FALSEDict.insert
Section titled “Dict.insert”Dict.insert <- function<K: key, V>(dict: Dict<K, V>, key: K, value: V) -> Dict<K, V>#: <K: key, V> fn(dict: Dict<K, V>, key: K, value: V) -> Dict<K, V>Dict.insert <- function(dict, key, value)Adds an entry, or replaces the value of an existing key in place.
Dict.insert(Dict.single("a", 1), "b", 2)#> Dict.from_list(list(("a", 1), ("b", 2)))Dict.remove
Section titled “Dict.remove”Dict.remove <- function<K: key, V>(dict: Dict<K, V>, key: K) -> Dict<K, V>#: <K: key, V> fn(dict: Dict<K, V>, key: K) -> Dict<K, V>Dict.remove <- function(dict, key)Removes the entry for key, if there is one.
Dict.remove(Dict.from_list(list(("a", 1), ("b", 2))), "a")#> Dict.from_list(list(("b", 2)))Dict.update
Section titled “Dict.update”Dict.update <- function<K: key, V>(dict: Dict<K, V>, key: K, f: fn(Result<V, :Missing>) -> Result<V, :Missing>) -> Dict<K, V>#: <K: key, V> fn(dict: Dict<K, V>, key: K, f: fn(Result<V, :Missing>) -> Result<V, :Missing>) -> Dict<K, V>Dict.update <- function(dict, key, f)Sets the entry for key from f, which gets :Ok(value) or :Err(:Missing) and returns
either; :Err removes the entry.
Dict.update(Dict.single("hits", 1L), "hits", function(r) :Ok(Result.with_default(r, 0L) + 1L))#> Dict.from_list(list(("hits", 2L)))Dict.keys
Section titled “Dict.keys”Dict.keys <- function<K: key, V>(dict: Dict<K, V>) -> list[K]#: <K: key, V> fn(dict: Dict<K, V>) -> list[K]Dict.keys <- function(dict)The keys, in insertion order.
Dict.keys(Dict.from_list(list(("a", 1), ("b", 2))))#> list("a", "b")Dict.values
Section titled “Dict.values”Dict.values <- function<K: key, V>(dict: Dict<K, V>) -> list[V]#: <K: key, V> fn(dict: Dict<K, V>) -> list[V]Dict.values <- function(dict)The values, in insertion order.
Dict.values(Dict.from_list(list(("a", 1), ("b", 2))))#> list(1, 2)Dict.walk
Section titled “Dict.walk”Dict.walk <- function<K: key, V, S>(dict: Dict<K, V>, state: S, f: fn(S, K, V) -> S) -> S#: <K: key, V, S> fn(dict: Dict<K, V>, state: S, f: fn(S, K, V) -> S) -> SDict.walk <- function(dict, state, f)Folds over the entries: f gets the state, a key and its value.
Dict.walk(Dict.from_list(list(("a", 1), ("b", 2))), 0, function(total, k, v) total + v)#> 3Dict.walk_until
Section titled “Dict.walk_until”Dict.walk_until <- function<K: key, V, S>(dict: Dict<K, V>, state: S, f: fn(S, K, V) -> :Continue(S) | :Break(S)) -> S#: <K: key, V, S> fn(dict: Dict<K, V>, state: S, f: fn(S, K, V) -> :Continue(S) | :Break(S)) -> SDict.walk_until <- function(dict, state, f)Folds the entries until f returns :Break(state).
Dict.walk_until(Dict.from_list(list(("a", 1L), ("b", 5L), ("c", 2L))), 0L, function(total, k, v) if (v > 4L) :Break(total) else :Continue(total + v))#> 1LDict.map
Section titled “Dict.map”Dict.map <- function<K: key, A, B>(dict: Dict<K, A>, f: fn(K, A) -> B) -> Dict<K, B>#: <K: key, A, B> fn(dict: Dict<K, A>, f: fn(K, A) -> B) -> Dict<K, B>Dict.map <- function(dict, f)Replaces every value with f of its key and value.
Dict.map(Dict.from_list(list(("a", 1), ("b", 2))), function(k, v) v * 10)#> Dict.from_list(list(("a", 10), ("b", 20)))Dict.join_map
Section titled “Dict.join_map”Dict.join_map <- function<K: key, V, X: key, Y>(dict: Dict<K, V>, f: fn(K, V) -> Dict<X, Y>) -> Dict<X, Y>#: <K: key, V, X: key, Y> fn(dict: Dict<K, V>, f: fn(K, V) -> Dict<X, Y>) -> Dict<X, Y>Dict.join_map <- function(dict, f)Unions the dictionaries f returns for each entry; a later entry’s value wins.
Dict.join_map(Dict.from_list(list(("a", 1L), ("b", 2L))), function(k, v) Dict.single(v, k))#> Dict.from_list(list((1L, "a"), (2L, "b")))Dict.keep_if
Section titled “Dict.keep_if”Dict.keep_if <- function<K: key, V>(dict: Dict<K, V>, predicate: fn(K, V) -> logical) -> Dict<K, V>#: <K: key, V> fn(dict: Dict<K, V>, predicate: fn(K, V) -> logical) -> Dict<K, V>Dict.keep_if <- function(dict, predicate)The entries for which predicate of the key and value is TRUE.
Dict.keep_if(Dict.from_list(list(("a", 1), ("b", 2))), function(k, v) v > 1)#> Dict.from_list(list(("b", 2)))Dict.drop_if
Section titled “Dict.drop_if”Dict.drop_if <- function<K: key, V>(dict: Dict<K, V>, predicate: fn(K, V) -> logical) -> Dict<K, V>#: <K: key, V> fn(dict: Dict<K, V>, predicate: fn(K, V) -> logical) -> Dict<K, V>Dict.drop_if <- function(dict, predicate)The entries for which predicate is FALSE.
Dict.drop_if(Dict.from_list(list(("a", 1), ("b", 2))), function(k, v) v > 1)#> Dict.from_list(list(("a", 1)))Dict.insert_all
Section titled “Dict.insert_all”Dict.insert_all <- function<K: key, V>(first: Dict<K, V>, second: Dict<K, V>) -> Dict<K, V>#: <K: key, V> fn(first: Dict<K, V>, second: Dict<K, V>) -> Dict<K, V>Dict.insert_all <- function(first, second)The entries of both; where a key is in both, second’s value wins.
Dict.insert_all(Dict.single("a", 1), Dict.from_list(list(("a", 5), ("b", 2))))#> Dict.from_list(list(("a", 5), ("b", 2)))Dict.keep_shared
Section titled “Dict.keep_shared”Dict.keep_shared <- function<K: key, V>(first: Dict<K, V>, second: Dict<K, V>) -> Dict<K, V>#: <K: key, V> fn(first: Dict<K, V>, second: Dict<K, V>) -> Dict<K, V>Dict.keep_shared <- function(first, second)The entries of first whose key and value are both in second.
Dict.keep_shared(Dict.from_list(list(("a", 1), ("b", 2))), Dict.from_list(list(("a", 1), ("b", 0))))#> Dict.from_list(list(("a", 1)))Dict.remove_all
Section titled “Dict.remove_all”Dict.remove_all <- function<K: key, V>(first: Dict<K, V>, second: Dict<K, V>) -> Dict<K, V>#: <K: key, V> fn(first: Dict<K, V>, second: Dict<K, V>) -> Dict<K, V>Dict.remove_all <- function(first, second)The entries of first whose key is not in second.
Dict.remove_all(Dict.from_list(list(("a", 1), ("b", 2))), Dict.single("a", 0))#> Dict.from_list(list(("b", 2)))Dict.reserve
Section titled “Dict.reserve”Dict.reserve <- function<K: key, V>(dict: Dict<K, V>, spare: integer) -> Dict<K, V>#: <K: key, V> fn(dict: Dict<K, V>, spare: integer) -> Dict<K, V>Dict.reserve <- function(dict, spare)The dictionary with room for spare more entries.
Dict.reserve(Dict.single("a", 1L), 1000L)#> Dict.from_list(list(("a", 1L)))Dict.capacity
Section titled “Dict.capacity”Dict.capacity <- function<K: key, V>(dict: Dict<K, V>) -> integer#: <K: key, V> fn(dict: Dict<K, V>) -> integerDict.capacity <- function(dict)How many entries the dictionary holds before its storage must grow.
Dict.capacity(Dict.reserve(Dict.single("a", 1L), 9L))#> 10LDict.release_excess_capacity
Section titled “Dict.release_excess_capacity”Dict.release_excess_capacity <- function<K: key, V>(dict: Dict<K, V>) -> Dict<K, V>#: <K: key, V> fn(dict: Dict<K, V>) -> Dict<K, V>Dict.release_excess_capacity <- function(dict)The dictionary with no room beyond its entries. An empty dictionary keeps its smallest index, which has room for 4.
Dict.capacity(Dict.release_excess_capacity(Dict.reserve(Dict.single("a", 1L), 9L)))#> 1LDict.clear
Section titled “Dict.clear”Dict.clear <- function<K: key, V>(dict: Dict<K, V>) -> Dict<K, V>#: <K: key, V> fn(dict: Dict<K, V>) -> Dict<K, V>Dict.clear <- function(dict)An empty dictionary of the same type.
Dict.clear(Dict.single("a", 1L))#> Dict.empty()