plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


16615b1 — Peter John 2026-11-11T00:05:45+05:30
std: Map.set is gone; with is the one way to write an update
Files changed (2) hide show
  1. docs/std.md +3 -3
  2. std/collections/Map.plum +1 -4
docs/std.md CHANGED
@@ -119,18 +119,18 @@ fun main() =
119
119
 
120
120
  **`Map<K, V>`** is a persistent ordered map: a weight-balanced binary search tree, as in Haskell's `Data.Map`. It is an `enum` (`MapTip`, or `MapBin(size, key, value, left, right)`), and every node keeps its subtree's size, so neither side of a node holds more than 3 times as many entries as the other and the height stays below 2 log2(n) + 2.
121
121
 
122
- - **Persistent.** Nothing changes in place. `m.with(k, v)` returns a new map that rebuilds the O(log n) nodes on the path to `k` and shares every other node with `m`, and `m` stays as it was, so keeping old versions is cheap (`benchmarks/map_snapshots`). An update is a value, so a loop that fills a map threads it: `fold`, a tail-recursive function, or a `mut` field (`state.m = state.m.with(k, v)`). A call whose result is dropped (`m.set(k, v)` as a statement) is an "unused result" error.
122
+ - **Persistent.** Nothing changes in place. `m.with(k, v)` returns a new map that rebuilds the O(log n) nodes on the path to `k` and shares every other node with `m`, and `m` stays as it was, so keeping old versions is cheap (`benchmarks/map_snapshots`). An update is a value, so a loop that fills a map threads it: `fold`, a tail-recursive function, or a `mut` field (`state.m = state.m.with(k, v)`). A call whose result is dropped (`m.with(k, v)` as a statement) is an "unused result" error.
123
123
  - **Ordered keys.** Keys are compared with their `compare(self, o) -> Int` method (negative before, 0 the same key, positive after). `Int`, `Float`, `Str`, `Bool` and `Byte` have one (see [Numbers](#numbers) for `Float`'s total order); a struct becomes a key by defining one. A key type without it is a compile error that names the type and the call that needed it: `Map.with<Pt, Int> (called at main.plum:8:8): Pt has no method 'compare'`. Keys need no `hash` any more.
124
124
  - **Key order everywhere.** `for e := range m`, `keys`, `values`, `entries`, `fold`, `each`, `toStr` and `==` go in ascending key order, which depends only on the keys, never on the order they were added.
125
125
  - **Build:** `Map.empty()`, `Map<Str, Int>()`, `Map.single(k, v)`, a literal `[k => v, ..]` (it is `Map.empty().with(k, v)...`, so O(n log n), and a later duplicate key wins), `Map.fromSlice(entries)` and `Map.fromList(entries)` (O(n) when the keys already strictly ascend, else O(n log n); a later duplicate wins).
126
126
  - **Read, O(log n):** `get(k)` (an `Option`), `getOr(k, fallback)` (no `Option` made), `contains(k)`; O(1): `length`, `isEmpty`.
127
- - **Update, O(log n), each returning the new map:** `with(k, v)` (also called `set`), `remove(k)`, `putIfAbsent(k, v)`, `upsert(k, v, f)` (`f` of the old value, or `v` when absent: Data.Map's `insertWith`), `update(k, f)` (only when present: `adjust`), `alter(k, f)` (`f` takes the value or `None` and returns the new one or `None` to leave `k` out).
127
+ - **Update, O(log n), each returning the new map:** `with(k, v)` (the one way to write an update: a literal `[k => v]` is `with` calls too), `remove(k)`, `putIfAbsent(k, v)`, `upsert(k, v, f)` (`f` of the old value, or `v` when absent: Data.Map's `insertWith`), `update(k, f)` (only when present: `adjust`), `alter(k, f)` (`f` takes the value or `None` and returns the new one or `None` to leave `k` out).
128
128
  - **Order, O(log n):** `min`, `max` (the entry with the smallest or largest key, an `Option<Pair<K, V>>`), `removeMin`, `removeMax`, the neighbours of a key `below(k)` (largest key `< k`), `atMost(k)` (`<=`), `above(k)` (`>`), `atLeast(k)` (`>=`), `entryAt(i)` (the `i`-th entry from 0; crashes with the index and the length out of range), `getEntryAt(i)` (the same as an `Option`), `indexOf(k)` (an `Option<Int>`), `rank(k)` (how many keys come before `k`, present or not), `height`.
129
129
  - **Ranges, O(log n), sharing nodes with the map:** `from(lo)` (keys `>= lo`), `until(hi)` (keys `< hi`), `range(lo, hi)` (`lo <= key < hi`), `split(k)` (the maps of keys before and after `k`, as a `Pair`), and its inverse `a.join(b)` (every key of `a` must be before every key of `b`, or it crashes). `foldRange(lo, hi, acc, f)` folds over `lo <= key < hi` without building a map.
130
130
  - **Combine, O(m log(n / m + 1)) for sizes m <= n:** `union` (a key in both keeps `self`'s value), `unionWith(o, f)` (`f(self's value, o's value)`), `intersection` (`self`'s values), `difference`.
131
131
  - **Walk, O(n), in key order:** `fold(acc, |acc, k, v| ..)`, `each(|k, v| ..)`, `map(|v| ..)` (same keys and shape), `filter(|k, v| ..)`, `any`, `all`, `keys`, `values` and `entries` (each a new `Slice`), `toStr` (`[1 => one, 2 => two]`; interpolating a map, `"{m}"`, isn't supported yet, so write `"{m.toStr()}"`), `==`. `for e := range m` takes O(log n) a step (each step is `min` and `removeMin`); `entries()` is cheaper when you read every entry. Every walk is O(log n) deep, so a 1,000,000-entry map is fine.
132
132
 
133
- Gone or changed from the old hash table: `set`, `remove` and `putIfAbsent` return the new map instead of changing the receiver (`with` no longer changes it either); `clear` is gone (use `Map.empty()`); keys need `compare` instead of `hash`; iteration is key order, not insertion order; `Map.toJson` writes object fields in key order. Against the hash table, `Map<Int, Int>` gets and sets are about 3x slower (`benchmarks/master_vs_simpler/branch/map`): every insert allocates the O(log n) nodes on its path, and the compiler's cell reuse does not yet reach a generic method, so those nodes are not updated in place even when the map is unique.
133
+ Gone or changed from the old hash table: `set` is gone (use `with`), `remove` and `putIfAbsent` return the new map instead of changing the receiver (`with` no longer changes it either); `clear` is gone (use `Map.empty()`); keys need `compare` instead of `hash`; iteration is key order, not insertion order; `Map.toJson` writes object fields in key order. Against the hash table, `Map<Int, Int>` gets and sets are about 3x slower (`benchmarks/master_vs_simpler/branch/map`): every insert allocates the O(log n) nodes on its path, and the compiler's cell reuse does not yet reach a generic method, so those nodes are not updated in place even when the map is unique.
134
134
 
135
135
  **`Set<T>`** is a persistent ordered set, the keys of a `Map<T, Bool>`, with the same costs and the same `compare` on its members. `Set<Int>()` or `Set.empty()`, `Set.fromSlice`, `Set.fromList`; `add`, `remove` (each returns the new set), `contains`, `length`, `isEmpty`, `min`, `max`, `removeMin`, `removeMax`, `below`, `atMost`, `above`, `atLeast`, `at(i)` (the `i`-th member; crashes out of range), `get(i)` (an `Option`), `indexOf`, `rank`, `from`, `until`, `range`, `foldRange`, `split`, `join`, `union`, `intersection`, `difference`, `isSubsetOf`, `fold`, `each`, `filter`, `any`, `all`, `toSlice` (ascending), `for x := range s`, `==`, `toStr` (`[1, 2, 3]`). `clear` is gone.
136
136
 
std/collections/Map.plum CHANGED
@@ -152,9 +152,6 @@ enum Map<K, V> =
152
152
  else
153
153
  MapBin(size: n, key: k, value: v, left: l, right: r)
154
154
 
155
- # `with`, by its older name.
156
- fun set(self, k: K, v: V) -> Map<K, V> = self.with(k, v)
157
-
158
155
  # The map with `k` mapped to `v` if `k` is absent; the same map otherwise.
159
156
  fun putIfAbsent(self, k: K, v: V) -> Map<K, V> =
160
157
  if self.contains(k) then self else self.with(k, v)
@@ -770,7 +767,7 @@ test "a key type's compare is the order: largest first"
770
767
  assert m.range(MapTestDesc(n: 7), MapTestDesc(n: 1)).keys().map(|k| k.n).toStr() == "[7,5,3]"
771
768
 
772
769
  test "with, get, has, getOr and set"
773
- m := Map<Str, Int>().with("b", 2).with("a", 1).set("c", 3)
770
+ m := Map<Str, Int>().with("b", 2).with("a", 1).with("c", 3)
774
771
  assert m.length() == 3
775
772
  assert m.get("a")! == 1 && m.get("c")! == 3 && m.get("z").isNone()
776
773
  assert m.contains("b") && !m.contains("bb")