Skip to content

std/maps

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 Iter functions, applied to a Map because it iterates as Iter<(K, V)>. Map.size is the map’s own O(1) count; the generic count is Iter.count. For lazy pipelines, use Iter; materialize back with Iter.to_map.

Import with import std/maps.

Types

type Map<K, V>

Immutable hash map from K to V.

fn get<K, V>(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
fn put<K, V>(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}
fn empty<K, V>(): Map<K, V>

type function on Map

Returns an empty map.

Interactive Tests

m: Map<String, Int> = Map.empty()
assert Map.size(m) == 0
fn remove<K, V>(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}
fn merge<K, V>(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}
fn map_next<K, V>(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()))
fn from_list<K, V>(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}
fn size<K, V>(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
fn keys<K, V>(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"]
fn values<K, V>(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]
fn contains_key?<K, V>(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")
fn map_values<K, V, U>(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,
}
fn map_keys<K, V, J>(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}
fn each_while(m: Map<K, V>, yield: ((K, V)) -> Bool): Bool

impl Iter.each_while

fn known_count(m: Map<K, V>): Maybe<Int>

impl Iter.known_count

fn to_string(value: Map<K, V>): String

impl Display.to_string

fn inspect(value: Map<K, V>): String

impl Debug.inspect

fn equal?(a: Map<K, V>, b: Map<K, V>): Bool

impl Equatable.equal?

fn hash(m: Map<K, V>): Int

impl Hashable.hash