.nib std
nib 0.3.0

module always in scope

Map

A lookup table from keys to values, written {"a": 1, "b": 2}. Keys are kept in sorted order, and every operation answers a new Map.

Map<'k, 'v> associates each key with one value. Every key has the same type and every value has the same type, and like Vec a Map is immutable: Map.set and Map.remove answer a new Map and leave the one they were given as it was.

A literal is written with braces, and its keys must be String or Int literals: {"tea": 3, "cake": 5} is a Map<String, Int>. Any other comparable value — a Bool, a tuple, a record, a Symbol — can still be a key; add it with Map.set, starting from the empty map {}. Two keys are the same key exactly when == says they are.

A Map keeps its keys sorted — strings bytewise, integers numerically — however they were written or added. That order is what it prints in, and the order Map.keys, Map.pairs and Map.fold walk, so two equal maps always produce the same output.

let price = {"tea": 3, "cake": 5};
let order = #["tea", "cake", "tea", "scone"];
Vec.fold(order, 0, { acc, item -> acc + Map.get_or(price, item, 0) })
11

Looking up a key that may not be there

Map.get raises Err.MissingField when the key is absent, and the checker makes the caller say so. A Map can also be called like a function, m(k), which is the same lookup and raises the same error. When a miss is an ordinary answer, use Map.get_or with a default, Map.has to ask first, or Map.Opt.get for an Option.

let ages = {"ada": 36, "bo": 31};
try "{ages("cy")}"
rescue Err.MissingField(key, msg) => "missing {key}: {msg}" end
missing cy: a Map of 2 keys has no key "cy"

Functions

funcsize
func size<'a includes Eq> :: Map<'a, 'b> -> Int

The number of keys.

Map.size({"a": 1, "b": 2, "c": 3})
3
funcget
func get<'a includes Eq> :: Map<'a, 'b>, 'a -> 'b raises Err.MissingField

The value stored under k. Raises Err.MissingField when there is none; Map.get_or and Map.Opt.get answer a default or an Option instead. Calling the map directly, m(k), does the same thing.

let ages = {"ada": 36, "bo": 31};
(Map.get(ages, "ada"), ages("bo"))
(36, 31)
funcset
func set<'a includes Eq> :: Map<'a, 'b>, 'a, 'b -> Map<'a, 'b>

A new Map with k bound to v. If k was already present its value is replaced; otherwise the key is added.

let m = {"a": 1};
(m, Map.set(m, "a", 10), Map.set(m, "b", 2))
({"a": 1}, {"a": 10}, {"a": 1, "b": 2})
funchas
func has<'a includes Eq> :: Map<'a, 'b>, 'a -> Bool

true if k is a key of the map.

let m = {"a": 1};
(Map.has(m, "a"), Map.has(m, "b"))
(true, false)
funckeys
func keys<'a includes Eq> :: Map<'a, 'b> -> Vec<'a>

The keys, in sorted order, as a Vec: walk them with Vec.fold, Vec.map and the rest of Vec's verbs.

Map.keys({"pear": 1, "apple": 2, "fig": 3})
#["apple", "fig", "pear"]
funcpairs
func pairs<'a includes Eq> :: Map<'a, 'b> -> List<('a, 'b)>

Every key with its value, as (key, value) tuples in key order.

Map.pairs({10: "ten", 2: "two", 7: "seven"})
[(2, "two"), (7, "seven"), (10, "ten")]
funcremove
func remove<'a includes Eq> :: Map<'a, 'b>, 'a -> Map<'a, 'b>

A new Map without k. Removing a key that is not there is not an error: the answer is the same map.

(Map.remove({"a": 1, "b": 2}, "a"), Map.remove({"a": 1}, "zz"))
({"b": 2}, {"a": 1})
funcget_or
func get_or<'a includes Eq> :: Map<'a, 'b>, 'a, 'b -> 'b

The value stored under k, or default when there is none. Never raises.

let stock = {"tea": 4};
(Map.get_or(stock, "tea", 0), Map.get_or(stock, "cake", 0))
(4, 0)
funcfold
func fold<'a includes Eq> :: Map<'a, 'b>, 'c, (('c, 'a, 'b) -> 'c) -> 'c

Combines the entries in key order: f(acc, k, v) is called once per entry, starting from init, and its answer is the next acc. The final acc is the result.

Map.fold({"b": 2, "a": 1, "c": 3}, "", { acc, k, v -> acc ++ k })
abc
func map_values<'a includes Eq> :: Map<'a, 'b>, ('b -> 'c) -> Map<'a, 'c>

A new Map with the same keys, each value replaced by f(value). The value type may change.

Map.map_values({"a": 1, "b": 2}, { v -> v * 100 })
{"a": 100, "b": 200}
func filter_keys<'a includes Eq> :: Map<'a, 'b>, ('a -> Bool) -> Map<'a, 'b>

A new Map keeping only the entries whose key p answers true for.

Map.filter_keys({"apple": 1, "avocado": 2, "banana": 3}, { k -> String.starts_with(k, "a") })
{"apple": 1, "avocado": 2}
funcmerge
func merge<'a includes Eq> :: Map<'a, 'b>, Map<'a, 'b> -> Map<'a, 'b>

Every entry of a and b together. Where both have the same key, the value from b wins — read it as "apply the updates in b to a".

Map.merge({"a": 1, "b": 2}, {"b": 20, "c": 30})
{"a": 1, "b": 20, "c": 30}