Map
Map module: persistent hash-array-mapped-trie map.
Map(k, v) is an immutable, hash-indexed key-value store backed by a HAMT (Hash Array Mapped Trie). Lookup, insert, and delete are O(log₃₂ n) ≈ O(1) amortized. The HAMT provides excellent structural sharing under Perceus reference counting (path-copy only touches 1–7 nodes per update).
Operations that need key identity take an explicit comparator: cmp : k -> k -> Bool where cmp(a)(b) = true means a < b
Equality between keys is derived from the comparator: eq(a, b) = not cmp(a)(b) and not cmp(b)(a)
Standard comparators: fn(a) -> fn(b) -> a < b for Int keys fn(a) -> fn(b) -> a < b for String keys (lexicographic)
Iteration order is hash-traversal order (not sorted by key). Use to_list then sort externally if sorted order is needed.
Performance (n entries): Lookup / insert / delete O(log₃₂ n) ≈ O(1) size O(n) Traversals (fold / keys) O(n)
Types
Functions
Returns all (key, value) pairs as a list (hash-traversal order).
Left fold over all entries. Argument order: collection first, init second, callback last (uncurried-collection convention). The callback f is called uncurried as f(acc, key, val) = new_acc.
Builds a map from a list of (key, value) pairs. Later entries overwrite earlier ones for duplicate keys. cmp : k -> k -> Bool where cmp(a)(b) = true means a < b.
Build a Map(Int, v) from a list of pairs using the default Int comparator. Equivalent to Map.from_list(pairs, Map.int_cmp).
Build a Map(String, v) from a list of pairs using the default String comparator. Equivalent to Map.from_list(pairs, Map.str_cmp).
Returns the value for key, or default if the key is absent.
Curried less-than comparator for Int keys. Pass to Map.* functions.
Merges two maps with a combining function for conflicting keys. merge_with(a, b, f, cmp): when key exists in both, value: f(val_a)(val_b). cmp : k -> k -> Bool; f : v -> v -> v (curried).
Returns the number of key-value pairs in the map. O(n).
Curried less-than comparator for String keys. Pass to Map.* functions.
Converts a map to a list of (key, value) pairs (hash-traversal order).
Returns all values as a list (hash-traversal order).