Skip to content
Jennifer Programming Language

maps - map manipulation

Enable with use maps;; every function is called as maps.NAME(...). Each function returns a new map (or list) and never mutates the input:

jennifer
use io;
use maps;

def scores as map of string to int init {"alice": 90, "bob": 80};
def names as list of string init maps.keys($scores);   # ["alice", "bob"]
def values as list of int init maps.values($scores);   # [90, 80]
def shrunk as map of string to int init maps.delete($scores, "bob");
def none as map of string to int init maps.without($scores, ["alice", "bob"]);
def withDave as map of string to int init maps.merge($scores, {"dave": 75});

Functions

CallReturnsNotes
maps.keys(m)list of <key type>The map's keys, in insertion order.
maps.values(m)list of <value type>The map's values, in insertion order.
maps.has(m, key)boolKey membership test. The non-erroring companion to $m[key].
maps.delete(m, key)mapNew map without key. Missing key is an error - see below.
maps.without(m, keys)mapNew map without every key in the list keys, one copy. Missing key errors.
maps.merge(a, b)mapNew map: a's entries with b's layered on top (b wins on collision).

maps.has

Reports whether the map contains the key. Check before an indexed read to avoid the missing-key runtime error:

jennifer
def m as map of string to int init {"a": 1};
if (maps.has($m, "a")) {
    io.printf("%d\n", $m["a"]);   # cannot fail: the key is there
}

The list counterpart is lists.contains, which checks values rather than keys; the different names keep that distinction visible at the call site.

maps.delete is strict

Deleting a key that isn't in the map raises a positioned runtime error, matching the read-side rule that $m[missing] is an error. For "delete if present", guard with maps.has:

jennifer
if (maps.has($m, "stale")) {
    $m = maps.delete($m, "stale");
}

A silent no-op would let a misspelt key go unnoticed. maps.without is strict the same way: every key in the list must be present; a key listed twice is removed once.

maps.merge ordering

merge(a, b) returns a's entries in a's insertion order, with b's values overwriting where keys collide. New keys from b are appended in b's insertion order. So merging {"x": 1, "y": 2} with {"y": 99, "z": 3} yields {"x": 1, "y": 99, "z": 3}, as Python's {**a, **b} does.

Value semantics

The library never modifies its inputs: maps.delete, maps.without and maps.merge return a new, independent map and leave the sources intact. Each copies the whole map, so deleting k keys one call at a time is O(n·k); maps.without($m, $keys) removes them with one copy.

See also: lists.md, index.md. len(m) is a language built-in (no import needed).