Skip to content

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.

Types

type Map<'K, 'V>

Immutable hash map from 'K to 'V.

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
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}
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
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}
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}
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()))
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}
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
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"]
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]
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")
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,
}
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}

impl Iterable

fn next(m: Map<'K, 'V>): Maybe<(('K, 'V), Map<'K, 'V>)>

impl Iterable.next

impl Iterable

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

impl Iterable.known_count

impl Display

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

impl Display.to_string

impl Debug

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

impl Debug.inspect

impl Equatable

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

impl Equatable.equal?

impl Hashable

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

impl Hashable.hash