plum

#treesitter#compiler#wasm

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

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


e832110 — Peter John 2026-11-10T14:38:56+05:30
std/collections: text.plum is gone, List gets join
docs/std.md CHANGED
@@ -74,7 +74,7 @@ To build a string in steps, use `Buffer`: `write`, `writeByte`, `toStr`.
74
74
 
75
75
  ## Sequences
76
76
 
77
- **`List`** suits recursion: `map`, `filter`, `fold`, `append`, `reverse`, `length`, `isEmpty`, `get`, `toSlice`.
77
+ **`List`** suits recursion: `map`, `filter`, `fold`, `append`, `reverse`, `length`, `isEmpty`, `get`, `join(sep)` (each element's `toStr()` with `sep` between, like `Slice.join`), `toSlice`.
78
78
 
79
79
  **`Slice`** is a growable array:
80
80
 
@@ -137,7 +137,7 @@ Gone or changed from the old hash table: `set`, `remove` and `putIfAbsent` retur
137
137
 
138
138
  `std/collections` also holds persistent trees, heaps and graphs, written as recursive `enum`s whose operations are methods that `match self` and recurse. No `for` or `while` loop and no `Slice`, `Map`, `Set`, `Buffer` or `Array` inside a structure or its algorithms: results are threaded through accumulators. Every update returns a new value that shares its unchanged subtrees. Sequences come back as `List` (`Cons`/`Nil`).
139
139
 
140
- Plum types are global by name, so each type and variant name here is unique in std (`AvlBranch`, `RbBranch`, `WbBranch`). Import each file (`import std/collections/AvlTree`). `std/collections/text` is their text helper (`text.join(parts, sep)` joins a `List[Str]`), which `toStr` uses instead of a `Buffer`.
140
+ Plum types are global by name, so each type and variant name here is unique in std (`AvlBranch`, `RbBranch`, `WbBranch`). Import each file (`import std/collections/AvlTree`). Their `toStr` joins the entries with `List.join`.
141
141
 
142
142
  Only true tail calls run in constant stack; other recursion overflows at a few tens of thousands of frames. So anything whose depth depends on the data (a chain of vertices, a key's bytes, a heap's root list) walks with a tail call, keeping the path it passed in an accumulator and rebuilding it with a second tail call. Balanced trees recurse plainly, O(log n) deep.
143
143
 
std/collections/AvlTree.plum CHANGED
@@ -8,7 +8,6 @@ import std/Pair
8
8
  import std/collections/List
9
9
  import std/Slice
10
10
  import std/collections/Map
11
- import std/collections/text
12
11
 
13
12
  # The persistent collections of std/collections (the trees, heaps, tries and graphs
14
13
  # next to `List`, `Map` and `Set`) are written as recursive enums, with no loop,
@@ -84,7 +83,7 @@ struct AvlTree[K, V](root: AvlBranch[K, V], size: Int, before: fn(K, K) -> Bool)
84
83
  fun values(self) -> List[V] = self.entries().map(|e| e.second)
85
84
 
86
85
  # The entries in key order: `[1: one, 2: two]`.
87
- fun toStr(self) -> Str = text.bracket(self.entries().map(|e| e.toStr()), ", ")
86
+ fun toStr(self) -> Str = "[" + self.entries().join(", ") + "]"
88
87
 
89
88
  # The nodes of an `AvlTree`: `AvlLeaf`, or a key and value with the subtree of
90
89
  # smaller keys on the left, larger on the right, and the node's height cached.
std/collections/BPlusTree.plum CHANGED
@@ -9,7 +9,6 @@ import std/Array
9
9
  import std/math/Vec2
10
10
  import std/collections/List
11
11
  import std/Slice
12
- import std/collections/text
13
12
 
14
13
  # `BPlusTree`, written functionally (see std/collections/AvlTree
15
14
  # for the rules these persistent collections follow): a persistent B+ tree, an ordered map from `K` to `V`,
@@ -227,7 +226,7 @@ struct BPlusTree[K, V](root: BpNode[K, V], size: Int, order: Int, before: fn(K,
227
226
 
228
227
  # `[k0: v0, k1: v1]` in key order.
229
228
  fun toStr(self) -> Str =
230
- text.bracket(self.entries().map(|e| e.toStr()), ", ")
229
+ "[" + self.entries().join(", ") + "]"
231
230
 
232
231
  # A node of a `BPlusTree`: a leaf of entries (keys ascending), or an inner node of
233
232
  # `kids` with one separator key between each two.
std/collections/BTree.plum CHANGED
@@ -9,7 +9,6 @@ import std/Float
9
9
  import std/Array
10
10
  import std/math/Vec2
11
11
  import std/collections/List
12
- import std/collections/text
13
12
 
14
13
  # `BTree`, written functionally (see std/collections/AvlTree
15
14
  # for the rules these persistent collections follow): a persistent B-tree, an ordered map from `K` to `V`
@@ -184,7 +183,7 @@ struct BTree[K, V](root: BtNode[K, V], size: Int, t: Int, before: fn(K, K) -> Bo
184
183
 
185
184
  # `[1: a, 2: b]`, in key order.
186
185
  fun toStr(self) -> Str =
187
- text.bracket(self.entries().map(|e| e.toStr()), ", ")
186
+ "[" + self.entries().join(", ") + "]"
188
187
 
189
188
  # The keys node by node: a leaf is `(1 2)`, a branch `((1) 2 (3))`.
190
189
  fun shape(self) -> Str =
@@ -349,10 +348,10 @@ enum BtNode[K, V] =
349
348
  BtBranch(es, kids) => btCollect(es.reverse(), kids.reverse(), acc)
350
349
 
351
350
  fun shape(self) -> Str = match self
352
- BtLeaf(es) => "({text.join(es.map(|e| "{e.first}"), " ")})"
351
+ BtLeaf(es) => "({es.map(|e| "{e.first}").join(" ")})"
353
352
  BtBranch(es, kids) =>
354
353
  nil: List[Str] := Nil
355
- "({text.join(btWeave(kids, es, nil).reverse(), " ")})"
354
+ "({btWeave(kids, es, nil).reverse().join(" ")})"
356
355
 
357
356
  # A leaf when `kids` is empty, else a branch.
358
357
  fun btMake[K, V](es: List[Pair[K, V]], kids: List[BtNode[K, V]]) -> BtNode[K, V] = match kids
@@ -543,7 +542,7 @@ test "insert, get and has; insert replaces an existing value"
543
542
  test "a custom comparator orders Float keys descending; min and max"
544
543
  empty: BTree[Float, Str] := BTree.by(|a, b| a > b, 2)
545
544
  tree := [2.5, -1.0, 9.0, 0.5, 3.0, 7.25].fold(empty, |acc, x| acc.insert(x, x.toStr()))
546
- assert text.join(tree.keys().map(|x| x.toStr()), ",") == "9.0,7.25,3.0,2.5,0.5,-1.0"
545
+ assert tree.keys().join(",") == "9.0,7.25,3.0,2.5,0.5,-1.0"
547
546
  assert tree.min()!.first == 9.0
548
547
  assert tree.max()!.first == -1.0
549
548
  assert tree.get(0.5).unwrapOr("") == "0.5"
std/collections/Graph.plum CHANGED
@@ -5,7 +5,6 @@ import std/Str
5
5
  import std/Option
6
6
  import std/collections/List
7
7
  import std/collections/AvlTree
8
- import std/collections/text
9
8
 
10
9
  # A graph written functionally (see std/collections/AvlTree for the rules
11
10
  # these persistent collections follow): a persistent directed graph, a chain of vertices, each holding its
@@ -95,7 +94,7 @@ enum Graph =
95
94
  # One line per vertex: `Vertex 0 -> 2, 1`, or `Vertex 3 -> None` with no edges.
96
95
  fun toStr(self) -> Str =
97
96
  none: List[Str] := Nil
98
- text.join(self.lines(none).reverse(), "")
97
+ self.lines(none).reverse().join("")
99
98
 
100
99
  # The lines, last vertex first, in front of `acc`.
101
100
  fun lines(self, acc: List[Str]) -> List[Str] = match self
@@ -109,7 +108,7 @@ fun graphHas(xs: List[Int], x: Int) -> Bool = match xs
109
108
 
110
109
  fun graphEdges(edges: List[Int]) -> Str = match edges
111
110
  Nil => "None"
112
- Cons(_, _) => text.join(edges.map(|n| n.toStr()), ", ")
111
+ Cons(_, _) => edges.join(", ")
113
112
 
114
113
  fun graphSample() -> Graph =
115
114
  Graph.ofSize(4).addEdge(0, 1).addEdge(0, 2).addEdge(1, 2).addEdge(2, 0).addEdge(2, 3)
std/collections/Hamt.plum CHANGED
@@ -6,7 +6,6 @@ import std/Pair
6
6
  import std/Option
7
7
  import std/collections/List
8
8
  import std/collections/Map
9
- import std/collections/text
10
9
 
11
10
  # `Hamt`, written functionally (see std/collections/AvlTree
12
11
  # for the rules these persistent collections follow): a persistent hash map, a hash array mapped trie. Each key's
@@ -95,7 +94,7 @@ struct Hamt[K, V](root: HamtNode[K, V], size: Int) =
95
94
 
96
95
  # `[k: v, ...]` in trie order; `[]` when empty.
97
96
  fun toStr(self) -> Str =
98
- text.bracket(self.entries().map(|e| e.toStr()), ", ")
97
+ "[" + self.entries().join(", ") + "]"
99
98
 
100
99
  # A trie node. A branch's `bitmap` has bit i set when it has a child for 5-bit slot i;
101
100
  # `kids` holds just those children, in slot order. Keys whose full hashes are equal share
@@ -483,7 +482,7 @@ test "keys, values, entries and toStr see every entry once"
483
482
  one: Hamt[Str, Int] := Hamt.empty()
484
483
  assert one.set("x", 1).toStr() == "[x: 1]"
485
484
  assert m.toStr().length() > 0
486
- assert m.toStr() == text.bracket(es.map(|e| "{e.first}: {e.second}"), ", ")
485
+ assert m.toStr() == "[" + es.map(|e| "{e.first}: {e.second}").join(", ") + "]"
487
486
 
488
487
  test "forced collisions: keys with a bad hash"
489
488
  m0: Hamt[HamtBadKey, Int] := Hamt.empty()
std/collections/IntervalTree.plum CHANGED
@@ -6,7 +6,6 @@ import std/Str
6
6
  import std/Option
7
7
  import std/collections/List
8
8
  import std/Slice
9
- import std/collections/text
10
9
 
11
10
  # `IntervalTree`, written functionally (see std/collections/AvlTree
12
11
  # for the rules these persistent collections follow): a persistent interval tree over closed Int intervals
@@ -21,7 +20,7 @@ import std/collections/text
21
20
  # are tail calls here, its reassigned locals in `join` are `Int.max`, and its queries,
22
21
  # which pushed onto a `Slice`, cons each hit onto an accumulator argument: the right
23
22
  # subtree is walked first, so the hits come out ordered by (lo, hi) with no reverse.
24
- # `toStr` joins the entries with `text.bracket` instead of a `Buffer`.
23
+ # `toStr` joins the entries with `List.join`.
25
24
  #
26
25
  # `insert`/`remove`/`has` are O(log n); `overlapping` and `containing` are
27
26
  # O(log n + k log n) at worst for k results; `length` is O(1). Every recursion follows
@@ -85,7 +84,7 @@ struct IntervalTree[V](root: IvBranch[V], size: Int) =
85
84
 
86
85
  # The intervals in order: `[[1, 5]: a, [2, 3]: b]`.
87
86
  fun toStr(self) -> Str =
88
- text.bracket(self.entries().map(|e| e.toStr()), ", ")
87
+ "[" + self.entries().join(", ") + "]"
89
88
 
90
89
  # One stored interval `[lo, hi]` and its value.
91
90
  struct IvEntry[V](lo: Int, hi: Int, value: V) =
@@ -282,7 +281,7 @@ fun ivValid(t: IntervalTree[Int]) -> Bool =
282
281
 
283
282
  # Intervals as `lo,hi` words, for comparing results whatever their values.
284
283
  fun ivSpans(es: List[IvEntry[Int]]) -> Str =
285
- text.join(es.map(|e| "{e.lo},{e.hi} "), "")
284
+ es.map(|e| "{e.lo},{e.hi} ").join("")
286
285
 
287
286
  # The brute-force answer: every model interval meeting `[lo, hi]`, in (lo, hi) order.
288
287
  fun ivScan(model: Slice[IvEntry[Int]], lo: Int, hi: Int) -> List[IvEntry[Int]] =
std/collections/KdTree.plum CHANGED
@@ -8,7 +8,6 @@ import std/Pair
8
8
  import std/math/Vec2
9
9
  import std/Slice
10
10
  import std/collections/List
11
- import std/collections/text
12
11
 
13
12
  # `KdTree`, written functionally (see
14
13
  # std/collections/AvlTree for the rules these persistent collections follow): a persistent 2-d
@@ -152,7 +151,7 @@ struct KdTree[V](root: KdNode[V], size: Int) =
152
151
 
153
152
  # The entries as `[(1.0, 2.0): a, (3.0, 4.0): b]`, in `entries` order.
154
153
  fun toStr(self) -> Str =
155
- text.bracket(self.entries().map(|e| "({e.first.x}, {e.first.y}): {e.second.toStr()}"), ", ")
154
+ "[" + self.entries().map(|e| "({e.first.x}, {e.first.y}): {e.second.toStr()}").join(", ") + "]"
156
155
 
157
156
  # The nodes of a `KdTree`: `KdLeaf`, or an entry with the subtrees on either side of
158
157
  # its split line. The split axis is not stored; it comes from the depth.
std/collections/LeftistHeap.plum CHANGED
@@ -6,7 +6,6 @@ import std/Option
6
6
  import std/Pair
7
7
  import std/collections/List
8
8
  import std/Slice
9
- import std/collections/text
10
9
 
11
10
  # `LeftistHeap`, written functionally (see std/collections/AvlTree
12
11
  # for the rules these persistent collections follow): a persistent mergeable MIN-heap kept as a leftist tree, a
@@ -92,7 +91,7 @@ struct LeftistHeap[T](root: LhTree[T], size: Int, before: fn(T, T) -> Bool) =
92
91
 
93
92
  # The elements smallest first: `[1,2,3]`.
94
93
  fun toStr(self) -> Str =
95
- text.bracket(self.toList().map(|x| x.toStr()), ",")
94
+ "[" + self.toList().join(",") + "]"
96
95
 
97
96
  # A leftist tree: empty, or an element above two subtrees with the node's rank
98
97
  # (1 + the rank of its right subtree; an empty tree has rank 0).
std/collections/List.plum CHANGED
@@ -94,17 +94,21 @@ enum List[T] =
94
94
  )
95
95
  out
96
96
 
97
+ # Each element's `toStr()` with `sep` between each two, in order, like `Slice.join`:
98
+ # `[1, 2, 3].join(", ")` is `1, 2, 3`, and a `List[Str]` joins its strings. O(total
99
+ # length), constant stack.
97
- fun toStr(self) -> Str =
100
+ fun join(self, sep: Str = ",") -> Str =
98
101
  res := Buffer()
99
- res.write("[")
100
102
  if Cons(h, t) := self
101
103
  res.write(h.toStr())
102
104
  for x := range t
103
- res.write(",")
105
+ res.write(sep)
104
106
  res.write(x.toStr())
105
- res.write("]")
106
107
  res.toStr()
107
108
 
109
+ fun toStr(self) -> Str =
110
+ "[" + self.join(",") + "]"
111
+
108
112
  # `==` and `!=`: the elements compare equal, in order (see `equals`).
109
113
  fun eq(self, other: List[T]) -> Bool = self.equals(other)
110
114
 
@@ -151,3 +155,20 @@ test "list fold, each, find: a million elements, find exits early"
151
155
  )
152
156
  assert found.unwrapOr(-1) == 999998
153
157
  assert hits.get(0) == 3
158
+
159
+ test "join puts the separator between the elements, in order"
160
+ none: List[Str] := Nil
161
+ assert none.join(",") == "" && none.toStr() == "[]"
162
+ assert ["a"].join(",") == "a"
163
+ assert ["a", "b"].join(", ") == "a, b"
164
+ assert ["a", "b", "c"].join(", ") == "a, b, c"
165
+ assert ["a", "b", "c"].join("") == "abc"
166
+ assert [1, 2, 3].join() == "1,2,3" && [1, 2, 3].join(" + ") == "1 + 2 + 3"
167
+ assert ["0", "1"].toStr() == "[0,1]"
168
+
169
+ test "join of 100,000 pieces"
170
+ nil: List[Str] := Nil
171
+ s := 99999.downTo(0).fold(nil, |acc, i| Cons(head: i.toStr(), tail: acc)).join(",")
172
+ assert s.startsWith("0,1,2,")
173
+ assert s.endsWith(",99998,99999")
174
+ assert s.length() == 588889
std/collections/PairingHeap.plum CHANGED
@@ -6,7 +6,6 @@ import std/Option
6
6
  import std/Pair
7
7
  import std/collections/List
8
8
  import std/Slice
9
- import std/collections/text
10
9
 
11
10
  # `PairingHeap`, written functionally (see std/collections/AvlTree
12
11
  # for the rules these persistent collections follow): a persistent mergeable MIN-heap kept as a pairing heap, a
@@ -94,7 +93,7 @@ struct PairingHeap[T](root: PhTree[T], size: Int, before: fn(T, T) -> Bool) =
94
93
 
95
94
  # The elements smallest first: `[1,2,3]`.
96
95
  fun toStr(self) -> Str =
97
- text.bracket(self.toList().map(|x| x.toStr()), ",")
96
+ "[" + self.toList().join(",") + "]"
98
97
 
99
98
  # A pairing tree: empty, or an element above its child trees (never `PhEmpty`).
100
99
  enum PhTree[T] =
std/collections/Quadtree.plum CHANGED
@@ -9,7 +9,6 @@ import std/math/Vec2
9
9
  import std/math/Aabb
10
10
  import std/Slice
11
11
  import std/collections/List
12
- import std/collections/text
13
12
 
14
13
  # `Quadtree`, written functionally (see
15
14
  # std/collections/AvlTree for the rules these persistent collections follow): a persistent
@@ -28,7 +27,7 @@ import std/collections/text
28
27
  # keeps the entries it passed reversed and puts them back with `revOnto`); `query`
29
28
  # and `nearby` thread a `List` accumulator through the four quarters instead of
30
29
  # pushing to a `Slice`, and reverse it once at the end; the `while` over the quarters
31
- # is four nested calls; `toStr` joins with `text` (std/collections/text).
30
+ # is four nested calls; `toStr` joins with `List.join`.
32
31
  #
33
32
  # `insert`/`remove` are O(depth + capacity); `query` and `nearby` visit only the
34
33
  # quarters that touch the area; `length` is O(1). Stack: the tree recursion is at most
@@ -88,7 +87,7 @@ struct Quadtree[V](root: QuadNode[V], bounds: Aabb, capacity: Int, maxDepth: Int
88
87
 
89
88
  # The entries as `[(1.0, 2.0): a, (3.0, 4.0): b]`, in `entries` order.
90
89
  fun toStr(self) -> Str =
91
- text.bracket(self.entries().map(|e| "({e.first.x}, {e.first.y}): {e.second.toStr()}"), ", ")
90
+ "[" + self.entries().map(|e| "({e.first.x}, {e.first.y}): {e.second.toStr()}").join(", ") + "]"
92
91
 
93
92
  # The nodes of a `Quadtree`: a leaf of entries (newest first) with their count, or
94
93
  # four quarters (numbered as in `quadQuadrantOf`) with the total count below. A
std/collections/RadixTree.plum CHANGED
@@ -7,7 +7,6 @@ import std/Pair
7
7
  import std/collections/List
8
8
  import std/Slice
9
9
  import std/collections/Map
10
- import std/collections/text
11
10
  import std/collections/Trie
12
11
 
13
12
  # `RadixTree`, written functionally (see std/collections/AvlTree
@@ -112,7 +111,7 @@ struct RadixTree[V](root: RadixNode[V], size: Int) =
112
111
 
113
112
  # `[a: 1, ab: 2]`, in sorted key order.
114
113
  fun toStr(self) -> Str =
115
- text.bracket(self.entries().map(|e| e.toStr()), ", ")
114
+ "[" + self.entries().join(", ") + "]"
116
115
 
117
116
  # How many bytes of `b`, from index `c` on, match `a` from index `ai + c` on, plus
118
117
  # `c` (call with `c` = 0). Tail call.
@@ -342,7 +341,7 @@ enum RadixEdges[V] =
342
341
 
343
342
  # The labels in order, as `abc mx z `.
344
343
  fun labelsStr(self) -> Str =
345
- text.join(self.labelList(Nil).reverse().map(|l| "{l} "), "")
344
+ self.labelList(Nil).reverse().map(|l| "{l} ").join("")
346
345
 
347
346
  fun labelList(self, acc: List[Str]) -> List[Str] =
348
347
  match self
std/collections/RedBlackTree.plum CHANGED
@@ -7,7 +7,6 @@ import std/Pair
7
7
  import std/collections/List
8
8
  import std/collections/Map
9
9
  import std/Slice
10
- import std/collections/text
11
10
 
12
11
  # A red-black tree written functionally (see std/collections/AvlTree for
13
12
  # the rules these persistent collections follow): a persistent ordered map kept as a red-black tree. Every
@@ -22,7 +21,7 @@ import std/collections/text
22
21
  # ascending. `entries`, `keys` and `values` return a `List`.
23
22
  # `get`, `min` and `max` are tail-recursive methods on the enum, cascades of pattern
24
23
  # tests are `match` arms (with small helpers where a cascade fell through), `collect`
25
- # conses onto an accumulator, and `toStr` joins with `text` (std/collections/text) instead of a `Buffer`.
24
+ # conses onto an accumulator, and `toStr` joins with `List.join`.
26
25
  # `get`/`has`/`insert`/`remove`/`min`/`max` are O(log n); `length` is O(1). Recursion
27
26
  # follows one path, so the stack depth is O(log n); `entries` is O(log n) deep too.
28
27
  struct RedBlackTree[K, V](root: RbBranch[K, V], size: Int, before: fn(K, K) -> Bool) =
@@ -82,7 +81,7 @@ struct RedBlackTree[K, V](root: RbBranch[K, V], size: Int, before: fn(K, K) -> B
82
81
  fun values(self) -> List[V] = self.entries().map(|e| e.second)
83
82
 
84
83
  # The entries in key order: `[1: one, 2: two]`.
85
- fun toStr(self) -> Str = text.bracket(self.entries().map(|e| e.toStr()), ", ")
84
+ fun toStr(self) -> Str = "[" + self.entries().join(", ") + "]"
86
85
 
87
86
  # The nodes of a `RedBlackTree`: `RbLeaf` (black), or a red or black node with its
88
87
  # key and value, the subtree of smaller keys on the left and larger on the right. The
std/collections/Treap.plum CHANGED
@@ -8,7 +8,6 @@ import std/math/Random
8
8
  import std/collections/List
9
9
  import std/Slice
10
10
  import std/collections/Map
11
- import std/collections/text
12
11
 
13
12
  # `Treap`, written functionally (see std/collections/AvlTree
14
13
  # for the rules these persistent collections follow): a persistent ordered map kept as a treap, a binary
@@ -29,7 +28,7 @@ import std/collections/text
29
28
  # Same API as the twin, except `entries`/`keys`/`values` return a `List`. The
30
29
  # twin's `while` walks (`get`, `min`, `max`) are tail calls here, its `collect` into a
31
30
  # `Slice` conses onto an accumulator, the `v = fv` in `union` is an `if` value, and `toStr`
32
- # joins a `List[Str]` with `text` (std/collections/text) instead of writing into a `Buffer`.
31
+ # joins a `List` with `List.join`.
33
32
  # Stack: `get`/`min`/`max` run in constant stack; everything else recurses along paths
34
33
  # of the tree, O(log n) deep expected (`entries` too).
35
34
  struct Treap[K, V](root: TreapNode[K, V], before: fn(K, K) -> Bool, rng: Random) =
@@ -143,7 +142,7 @@ struct Treap[K, V](root: TreapNode[K, V], before: fn(K, K) -> Bool, rng: Random)
143
142
 
144
143
  # The entries in key order: `[1: one, 2: two]`.
145
144
  fun toStr(self) -> Str =
146
- text.bracket(self.entries().map(|e| e.toStr()), ", ")
145
+ "[" + self.entries().join(", ") + "]"
147
146
 
148
147
  # The three parts of a treap split at a key: the keys before it, the node holding
149
148
  # the key itself (a single node, or `TreapLeaf` when absent), and the keys after it.
std/collections/Trie.plum CHANGED
@@ -7,7 +7,6 @@ import std/Pair
7
7
  import std/collections/List
8
8
  import std/Slice
9
9
  import std/collections/Map
10
- import std/collections/text
11
10
 
12
11
  # `Trie`, written functionally (see std/collections/AvlTree
13
12
  # for the rules these persistent collections follow): a persistent map from `Str` keys to values, a trie
@@ -109,7 +108,7 @@ struct Trie[V](root: TrieNode[V], size: Int) =
109
108
 
110
109
  # `[a: 1, ab: 2]`, in sorted key order.
111
110
  fun toStr(self) -> Str =
112
- text.bracket(self.entries().map(|e| e.toStr()), ", ")
111
+ "[" + self.entries().join(", ") + "]"
113
112
 
114
113
  # A node of a `Trie`: the value of the key that ends here, if any, and the
115
114
  # children, one per next byte.
@@ -206,7 +205,7 @@ struct TrieFrame[V](prefix: Str, pending: List[Int], node: TrieNode[V]) =
206
205
  fun key(self) -> Str =
207
206
  match self.pending
208
207
  Nil => self.prefix
209
- Cons(_, _) => self.prefix.concat(text.join(self.pending.reverse().map(|b| Str.byteToStr(b)), ""))
208
+ Cons(_, _) => self.prefix.concat(self.pending.reverse().map(|b| Str.byteToStr(b)).join(""))
210
209
 
211
210
  # The children of a `TrieNode`: a list of (byte, child) sorted by byte, no byte
212
211
  # twice.
@@ -289,7 +288,7 @@ enum TrieKids[V] =
289
288
 
290
289
  # The children's bytes in order, as `1 5 9 `.
291
290
  fun bytesStr(self) -> Str =
292
- text.join(self.byteList(Nil).reverse().map(|b| "{b} "), "")
291
+ self.byteList(Nil).reverse().map(|b| "{b} ").join("")
293
292
 
294
293
  fun byteList(self, acc: List[Int]) -> List[Int] =
295
294
  match self
std/collections/WeightBalancedTree.plum CHANGED
@@ -7,7 +7,6 @@ import std/Pair
7
7
  import std/collections/List
8
8
  import std/Slice
9
9
  import std/collections/Map
10
- import std/collections/text
11
10
 
12
11
  # A weight-balanced tree written functionally (see std/collections/AvlTree
13
12
  # for the rules these persistent collections follow): a persistent ordered map kept as a weight-balanced tree
@@ -28,7 +27,7 @@ import std/collections/text
28
27
  #
29
28
  # `entries`, `keys` and `values` return a `List`. Lookups (`get`, `min`, `max`, `rank`,
30
29
  # `at`) are tail calls, with `rank` and `at` threading their counter through an
31
- # argument; `collect` conses onto an accumulator; `toStr` joins with `text` (std/collections/text) instead of
30
+ # argument; `collect` conses onto an accumulator; `toStr` joins with `List.join` instead of
32
31
  # a `Buffer`.
33
32
  # Stack depth: lookups are tail calls (constant stack); updates, `cut`, `link`, `merge`
34
33
  # and the set operations recurse along one path per level, so O(log n); `collect` is
@@ -119,7 +118,7 @@ struct WeightBalancedTree[K, V](root: WbBranch[K, V], before: fn(K, K) -> Bool)
119
118
  fun values(self) -> List[V] = self.entries().map(|e| e.second)
120
119
 
121
120
  # The entries in key order: `[1: one, 2: two]`.
122
- fun toStr(self) -> Str = text.bracket(self.entries().map(|e| e.toStr()), ", ")
121
+ fun toStr(self) -> Str = "[" + self.entries().join(", ") + "]"
123
122
 
124
123
  # A subtree cut at a key: the entries before it, its value if present, and the
125
124
  # entries after it.
std/collections/text.plum DELETED
@@ -1,46 +0,0 @@
1
- module std
2
-
3
- import std/Int
4
- import std/Str
5
- import std/collections/List
6
-
7
- # Text helpers for the persistent collections of std/collections: joining the pieces
8
- # of a `toStr` without a `Buffer`. This file is a function module, reached as
9
- # `text.join(..)`.
10
-
11
- # The strings of `parts` with `sep` between each two, in order. Adjacent pieces are
12
- # joined pairwise, pass after pass, so each character is copied O(log n) times, not
13
- # O(n). Both passes are tail calls: constant stack.
14
- fun join(parts: List[Str], sep: Str) -> Str = match parts
15
- Nil => ""
16
- Cons(h, Nil) => h
17
- Cons(_, _) => join(pairUp(parts, sep, Nil), sep)
18
-
19
- # One pass: `[a, b, c, d, e]` becomes `[a+b, c+d, e]` (with `sep` inside each pair).
20
- fun pairUp(parts: List[Str], sep: Str, acc: List[Str]) -> List[Str] = match parts
21
- Nil => acc.reverse()
22
- Cons(a, Nil) => Cons(head: a, tail: acc).reverse()
23
- Cons(a, Cons(b, rest)) => pairUp(rest, sep, Cons(head: "{a}{sep}{b}", tail: acc))
24
-
25
- # `[` + the parts joined by `sep` + `]`.
26
- fun bracket(parts: List[Str], sep: Str) -> Str = "[{join(parts, sep)}]"
27
-
28
- fun textUpTo(i: Int, acc: List[Str]) -> List[Str] =
29
- if i < 0 then acc else textUpTo(i - 1, Cons(head: i.toStr(), tail: acc))
30
-
31
- test "text_join_empty: no pieces join to the empty string"
32
- none: List[Str] := Nil
33
- assert join(none, ",") == ""
34
- assert bracket(none, ",") == "[]"
35
-
36
- test "join puts the separator between pieces, in order"
37
- none: List[Str] := Nil
38
- assert join(Cons(head: "a", tail: none), ",") == "a"
39
- assert join(Cons(head: "a", tail: Cons(head: "b", tail: none)), ", ") == "a, b"
40
- assert join(Cons(head: "a", tail: Cons(head: "b", tail: Cons(head: "c", tail: none))), ", ") == "a, b, c"
41
- assert bracket(textUpTo(4, Nil), ",") == "[0,1,2,3,4]"
42
-
43
- test "join of 100,000 pieces"
44
- s := join(textUpTo(99999, Nil), ",")
45
- assert s.startsWith("0,1,2,")
46
- assert s.endsWith(",99998,99999")