std/map
Operations on the insertion-ordered Map<'K, 'V> type. Map-specific ops
(Map.map_values, Map.map_keys, Map.keys/values, Map.size, …)
live here; the generic iteration ops (Iter.reduce, Iter.find,
Iter.each, Iter.count, Iter.to_list, …) are free functions in the
iter module, applied to a Map because it iterates as Iterable<('K, 'V)>.
Map.size is the map’s own O(1) count; the generic count is Iter.count.
For lazy pipelines, use the iter module; materialize back with
Iter.to_map.
Import with import std/map.
Exports
Section titled “Exports”Types
type Map
Section titled “type Map”type Map<'K, 'V>
Immutable hash map from 'K to 'V.
Map.get
Section titled “Map.get”fn get(m: Map<'K, 'V>, key: 'K): Maybe<'V>
type function on Map
Gets a value by key. Returns Some(value) if the key is present, None otherwise.
Interactive Tests
assert Map.get({"a" => 1, "b" => 2}, "a") == Some(1)
assert Map.get({"a" => 1, "b" => 2}, "z") == None
Map.put
Section titled “Map.put”fn put(m: Map<'K, 'V>, key: 'K, value: 'V): Map<'K, 'V>
type function on Map
Returns a new map with the key-value pair added or updated.
Interactive Tests
assert Map.put({"a" => 1}, "b", 2) == {"a" => 1, "b" => 2}
assert Map.put({"a" => 1}, "a", 9) == {"a" => 9}
Map.empty
Section titled “Map.empty”fn empty(): Map<'K, 'V>
type function on Map
Returns an empty map.
Interactive Tests
m: Map<String, Int> = Map.empty()
assert Map.size(m) == 0
Map.remove
Section titled “Map.remove”fn remove(m: Map<'K, 'V>, key: 'K): Map<'K, 'V>
type function on Map
Returns a new map with the key removed. No-op if the key is absent.
Interactive Tests
assert Map.remove({"a" => 1, "b" => 2}, "a") == {"b" => 2}
Map.merge
Section titled “Map.merge”fn merge(a: Map<'K, 'V>, b: Map<'K, 'V>): Map<'K, 'V>
type function on Map
Merges two maps. Values from the second map win on key conflict.
Interactive Tests
assert Map.merge({"a" => 1}, {"a" => 9, "b" => 2}) == {"a" => 9, "b" => 2}
Map.map_next
Section titled “Map.map_next”fn map_next(m: Map<'K, 'V>): Maybe<(('K, 'V), Map<'K, 'V>)>
type function on Map
Returns the next key-value pair and remaining map, or None if empty.
Drives the Iter implementation for Map.
Interactive Tests
assert Map.map_next({"a" => 1}) == Some((("a", 1), Map.empty()))
Map.from_list
Section titled “Map.from_list”fn from_list(entries: List<('K, 'V)>): Map<'K, 'V>
type function on Map
Creates a map from a list of (key, value) tuples. Later entries overwrite earlier ones on duplicate keys.
Interactive Tests
assert Map.from_list([("a", 1), ("b", 2)]) == {"a" => 1, "b" => 2}
Map.size
Section titled “Map.size”fn size(m: Map<'K, 'V>): Int
type function on Map
Returns the number of entries in the map. O(1) — read from the backing map’s size (host-backed), not folded.
Interactive Tests
assert Map.size({"a" => 1, "b" => 2}) == 2
m: Map<String, Int> = Map.empty()
assert Map.size(m) == 0
Map.keys
Section titled “Map.keys”fn keys(m: Map<'K, 'V>): List<'K>
type function on Map
Returns all keys as a list, in insertion order.
Interactive Tests
assert Map.keys({"a" => 1, "b" => 2}) == ["a", "b"]
Map.values
Section titled “Map.values”fn values(m: Map<'K, 'V>): List<'V>
type function on Map
Returns all values as a list, in insertion order of the keys.
Interactive Tests
assert Map.values({"a" => 1, "b" => 2}) == [1, 2]
Map.contains_key?
Section titled “Map.contains_key?”fn contains_key?(m: Map<'K, 'V>, key: 'K): Bool
type function on Map
Returns true if the map contains the key.
Interactive Tests
assert Map.contains_key?({"a" => 1}, "a")
refute Map.contains_key?({"a" => 1}, "z")
Map.map_values
Section titled “Map.map_values”fn map_values(m: Map<'K, 'V>, f: ('V) -> 'U): Map<'K, 'U>
type function on Map
Transforms each value by applying a function. Keys are unchanged.
Interactive Tests
assert Map.map_values({"a" => 1, "b" => 2}, |v| v * 10) == {
"a" => 10,
"b" => 20,
}
Map.map_keys
Section titled “Map.map_keys”fn map_keys(m: Map<'K, 'V>, f: ('K) -> 'J): Map<'J, 'V>
type function on Map
Transforms each key by applying a function. Collisions resolve last-write-wins in insertion order.
Interactive Tests
assert Map.map_keys({"a" => 1, "b" => 2}, |k| k + "!") == {"a!" => 1, "b!" => 2}
Map.next
Section titled “Map.next”impl Iterable
fn next(m: Map<'K, 'V>): Maybe<(('K, 'V), Map<'K, 'V>)>
impl Iterable.next
Map.known_count
Section titled “Map.known_count”impl Iterable
fn known_count(m: Map<'K, 'V>): Maybe<Int>
impl Iterable.known_count
Map.to_string
Section titled “Map.to_string”impl Display
fn to_string(value: Map<'K, 'V>): String
impl Display.to_string
Map.inspect
Section titled “Map.inspect”impl Debug
fn inspect(value: Map<'K, 'V>): String
impl Debug.inspect
Map.equal?
Section titled “Map.equal?”impl Equatable
fn equal?(a: Map<'K, 'V>, b: Map<'K, 'V>): Bool
impl Equatable.equal?
Map.hash
Section titled “Map.hash”impl Hashable
fn hash(m: Map<'K, 'V>): Int
impl Hashable.hash