Skip to content
ry

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 <- function<K: key, V>() -> Dict<K, V>

A dictionary with no entries.

Dict.empty()
#> Dict.empty()

Source

Dict.with_capacity <- function<K: key, V>(capacity: integer) -> Dict<K, V>

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))
#> 64L

Source

Dict.single <- function<K: key, V>(key: K, value: V) -> Dict<K, V>

A dictionary with one entry.

Dict.single("a", 1)
#> Dict.from_list(list(("a", 1)))

Source

Dict.from_list <- function<K: key, V>(pairs: list[Tuple{K, V}]) -> Dict<K, V>

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)))

Source

Dict.to_list <- function<K: key, V>(dict: Dict<K, V>) -> list[Tuple{K, V}]

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))

Source

Dict.len <- function<K: key, V>(dict: Dict<K, V>) -> integer

The number of entries.

Dict.len(Dict.single("a", 1))
#> 1L

Source

Dict.is_empty <- function<K: key, V>(dict: Dict<K, V>) -> logical

Whether the dictionary has no entries.

Dict.is_empty(Dict.empty())
#> TRUE

Source

Dict.get <- function<K: key, V>(dict: Dict<K, V>, key: K) -> Result<V, :KeyNotFound>

The value for key, or :Err(:KeyNotFound).

Dict.get(Dict.single("a", 1), "a")
#> :Ok(1)

Source

Dict.contains <- function<K: key, V>(dict: Dict<K, V>, key: K) -> logical

Whether the dictionary has an entry for key.

Dict.contains(Dict.single("a", 1), "b")
#> FALSE

Source

Dict.insert <- function<K: key, V>(dict: Dict<K, V>, key: K, value: V) -> Dict<K, V>

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)))

Source

Dict.remove <- function<K: key, V>(dict: Dict<K, V>, key: K) -> Dict<K, V>

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)))

Source

Dict.update <- function<K: key, V>(dict: Dict<K, V>, key: K, f: fn(Result<V, :Missing>) -> Result<V, :Missing>) -> Dict<K, V>

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)))

Source

Dict.keys <- function<K: key, V>(dict: Dict<K, V>) -> list[K]

The keys, in insertion order.

Dict.keys(Dict.from_list(list(("a", 1), ("b", 2))))
#> list("a", "b")

Source

Dict.values <- function<K: key, V>(dict: Dict<K, V>) -> list[V]

The values, in insertion order.

Dict.values(Dict.from_list(list(("a", 1), ("b", 2))))
#> list(1, 2)

Source

Dict.walk <- function<K: key, V, S>(dict: Dict<K, V>, state: S, f: fn(S, K, V) -> S) -> S

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)
#> 3

Source

Dict.walk_until <- function<K: key, V, S>(dict: Dict<K, V>, state: S, f: fn(S, K, V) -> :Continue(S) | :Break(S)) -> S

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))
#> 1L

Source

Dict.map <- function<K: key, A, B>(dict: Dict<K, A>, f: fn(K, A) -> B) -> Dict<K, B>

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)))

Source

Dict.join_map <- function<K: key, V, X: key, Y>(dict: Dict<K, V>, f: fn(K, V) -> Dict<X, Y>) -> Dict<X, Y>

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")))

Source

Dict.keep_if <- function<K: key, V>(dict: Dict<K, V>, predicate: fn(K, V) -> logical) -> Dict<K, V>

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)))

Source

Dict.drop_if <- function<K: key, V>(dict: Dict<K, V>, predicate: fn(K, V) -> logical) -> Dict<K, V>

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)))

Source

Dict.insert_all <- function<K: key, V>(first: Dict<K, V>, second: Dict<K, V>) -> Dict<K, V>

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)))

Source

Dict.keep_shared <- function<K: key, V>(first: Dict<K, V>, second: Dict<K, V>) -> Dict<K, V>

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)))

Source

Dict.remove_all <- function<K: key, V>(first: Dict<K, V>, second: Dict<K, V>) -> Dict<K, V>

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)))

Source

Dict.reserve <- function<K: key, V>(dict: Dict<K, V>, spare: integer) -> Dict<K, V>

The dictionary with room for spare more entries.

Dict.reserve(Dict.single("a", 1L), 1000L)
#> Dict.from_list(list(("a", 1L)))

Source

Dict.capacity <- function<K: key, V>(dict: Dict<K, V>) -> integer

How many entries the dictionary holds before its storage must grow.

Dict.capacity(Dict.reserve(Dict.single("a", 1L), 9L))
#> 10L

Source

Dict.release_excess_capacity <- function<K: key, V>(dict: Dict<K, V>) -> Dict<K, V>

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)))
#> 1L

Source

Dict.clear <- function<K: key, V>(dict: Dict<K, V>) -> Dict<K, V>

An empty dictionary of the same type.

Dict.clear(Dict.single("a", 1L))
#> Dict.empty()

Source