plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
46982ad
— Peter John
2026-11-11T00:03:22+05:30
std: Deque gains first, last, fold, each, toSlice and ==; Heap gains toSlice, fold, each and ==; Range gains any and all
- docs/language.md +1 -1
- docs/std.md +2 -2
- std/Int.plum +20 -0
- std/collections/Deque.plum +70 -0
- std/collections/Heap.plum +51 -0
docs/language.md
CHANGED
|
@@ -422,7 +422,7 @@ fun sumDigits(n: Int, total: Int) -> Int =
|
|
|
422
422
|
sumDigits(n / 10, total + n % 10)
|
|
423
423
|
```
|
|
424
424
|
|
|
425
|
-
A `Slice` is folded over its indexes: `0.until(xs.length()).fold(0, |sum, i| sum + xs.at(i))`. `repeatUntil(start, done, step)` in `std/Int` applies `step` until `done` holds and gives back the last state. Every one of these runs in constant stack.
|
|
425
|
+
A `Slice` is folded over its indexes: `0.until(xs.length()).fold(0, |sum, i| sum + xs.at(i))`. A range (`until`, `to`, `downTo`, `stepBy`) has `fold`, `each`, `find`, `any` and `all`; `find`, `any` and `all` stop at the first element that decides. `repeatUntil(start, done, step)` in `std/Int` applies `step` until `done` holds and gives back the last state. Every one of these runs in constant stack.
|
|
426
426
|
|
|
427
427
|
Make your own type loopable by giving it `next`. Return `None` when done, or `Some` of the value and the remaining sequence:
|
|
428
428
|
|
docs/std.md
CHANGED
|
@@ -134,8 +134,8 @@ Gone or changed from the old hash table: `set`, `remove` and `putIfAbsent` retur
|
|
|
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
|
|
|
137
|
-
- **`Deque`:** a queue you can use from both ends: `pushBack`, `pushFront`, `popBack`, `popFront`, `get`, `at` (crashes out of range)
|
|
138
|
-
- **`Heap`:** gives back first the item nothing comes `before`: `h: Heap<Int> := Heap.by(|a, b| a < b)`, or `Heap.byKey(|n| n.cost)` by a key of any type with `<`; `push`, `pop`, `peek`
|
|
137
|
+
- **`Deque`:** a queue you can use from both ends: `pushBack`, `pushFront`, `popBack`, `popFront`, `first` and `last` (the ends without removing them, each an `Option`), `get`, `at` (crashes out of range), `length`, `isEmpty`, `clear`, and front to back `fold`, `each`, `toSlice` and `==` (the same elements in order; where they sit in the ring doesn't count)
|
|
138
|
+
- **`Heap`:** gives back first the item nothing comes `before`: `h: Heap<Int> := Heap.by(|a, b| a < b)`, or `Heap.byKey(|n| n.cost)` by a key of any type with `<`; `push`, `pop`, `peek`, `length`, `isEmpty`, `clear`, and in pop order `toSlice`, `fold` and `each` (each works on a copy, O(n log n), and leaves the heap as it was). `==` is true when two heaps hold the same elements in the same pop order; their `before` is not compared, and equal elements may pop in either order, so two heaps built in different orders can hold the same values and still differ when their ties pop differently
|
|
139
139
|
|
|
140
140
|
## Persistent trees, heaps and graphs
|
|
141
141
|
|
std/Int.plum
CHANGED
|
@@ -265,6 +265,14 @@ struct Range(start: Int, stop: Int, step: Int) =
|
|
|
265
265
|
else
|
|
266
266
|
None
|
|
267
267
|
|
|
268
|
+
# Whether `pred` holds for some element. Stops at the first hit.
|
|
269
|
+
fun any(self, pred: fn(Int) -> Bool) -> Bool =
|
|
270
|
+
self.find(pred).isSome()
|
|
271
|
+
|
|
272
|
+
# Whether `pred` holds for every element (true for an empty range). Stops at the first miss.
|
|
273
|
+
fun all(self, pred: fn(Int) -> Bool) -> Bool =
|
|
274
|
+
self.find(|x| !pred(x)).isNone()
|
|
275
|
+
|
|
268
276
|
# Every `by`-th element, starting with the first (`by` > 0).
|
|
269
277
|
fun stepBy(self, by: Int) -> Range =
|
|
270
278
|
Range(start: self.start, stop: self.stop, step: if self.step > 0 then by else -by)
|
|
@@ -283,6 +291,18 @@ test "range fold, each, find"
|
|
|
283
291
|
assert 1.to(10).find(|x| x * x > 20).unwrapOr(-1) == 5
|
|
284
292
|
assert 1.to(3).find(|x| x > 9).isNone()
|
|
285
293
|
|
|
294
|
+
test "range any and all"
|
|
295
|
+
assert 1.to(10).any(|x| x == 7) && !1.to(10).any(|x| x > 10)
|
|
296
|
+
assert 1.to(10).all(|x| x > 0) && !1.to(10).all(|x| x < 10)
|
|
297
|
+
assert 5.until(5).all(|x| false) && !5.until(5).any(|x| true)
|
|
298
|
+
assert 3.downTo(1).all(|x| x >= 1) && 0.until(10).stepBy(3).any(|x| x == 9)
|
|
299
|
+
calls := Array.filled(1, 0)
|
|
300
|
+
assert 0.until(100).any(|x|
|
|
301
|
+
calls.set(0, calls.at(0) + 1)
|
|
302
|
+
x == 2
|
|
303
|
+
)
|
|
304
|
+
assert calls.at(0) == 3
|
|
305
|
+
|
|
286
306
|
test "range fold, each, find, repeatUntil run a million iterations"
|
|
287
307
|
assert 0.until(1000000).fold(0, |a, x| a + 1) == 1000000
|
|
288
308
|
assert 0.until(1000000).find(|x| x == 999999).unwrapOr(-1) == 999999
|
std/collections/Deque.plum
CHANGED
|
@@ -76,10 +76,38 @@ struct Deque<T>(mut buf: Array<T>, mut head: Int, mut size: Int) =
|
|
|
76
76
|
fun at(self, i: Int) -> T =
|
|
77
77
|
if i < 0 || i >= self.size then slice.outOfRange("Deque.at", i, self.size) else self.buf.at({self.head + i} % self.buf.length())
|
|
78
78
|
|
|
79
|
+
# The front element without removing it, or None if empty.
|
|
80
|
+
fun first(self) -> Option<T> =
|
|
81
|
+
self.get(0)
|
|
82
|
+
|
|
83
|
+
# The back element without removing it, or None if empty.
|
|
84
|
+
fun last(self) -> Option<T> =
|
|
85
|
+
self.get(self.size - 1)
|
|
86
|
+
|
|
79
87
|
fun clear(self) -> Unit =
|
|
80
88
|
self.head = 0
|
|
81
89
|
self.size = 0
|
|
82
90
|
|
|
91
|
+
# Left fold from front to back: `f(f(f(acc, front), second), third)`.
|
|
92
|
+
fun fold<A>(self, acc: A, f: fn(A, T) -> A) -> A =
|
|
93
|
+
0.until(self.size).fold(acc, |a, i| f(a, self.buf.at({self.head + i} % self.buf.length())))
|
|
94
|
+
|
|
95
|
+
# Calls `f` on each element, front to back.
|
|
96
|
+
fun each(self, f: fn(T) -> Unit) -> Unit =
|
|
97
|
+
for i := range self.size
|
|
98
|
+
f(self.buf.at({self.head + i} % self.buf.length()))
|
|
99
|
+
|
|
100
|
+
# The elements front to back, as a new `Slice`.
|
|
101
|
+
fun toSlice(self) -> Slice<T> =
|
|
102
|
+
out := Slice<T>()
|
|
103
|
+
self.each(|x| out.push(x))
|
|
104
|
+
out
|
|
105
|
+
|
|
106
|
+
# `==` and `!=`: the same length and the elements `==` in order, front to back (where
|
|
107
|
+
# each sits in the ring, and the capacity, don't count).
|
|
108
|
+
fun eq(self, o: Deque<T>) -> Bool =
|
|
109
|
+
self.size == o.size && 0.until(self.size).all(|i| self.at(i) == o.at(i))
|
|
110
|
+
|
|
83
111
|
test "deque_wraparound: head wraps past the end of the ring and growth unwraps it"
|
|
84
112
|
d := Deque<Int>()
|
|
85
113
|
# keep size small so head keeps advancing around the 8-slot ring
|
|
@@ -111,6 +139,48 @@ test "pushFront builds the front in reverse order"
|
|
|
111
139
|
assert d.popBack()! == 0
|
|
112
140
|
assert d.length() == 10
|
|
113
141
|
|
|
142
|
+
test "first and last peek at the ends without removing them"
|
|
143
|
+
d := Deque<Int>()
|
|
144
|
+
assert d.first().isNone() && d.last().isNone()
|
|
145
|
+
d.pushBack(2)
|
|
146
|
+
d.pushFront(1)
|
|
147
|
+
d.pushBack(3)
|
|
148
|
+
assert d.first()! == 1 && d.last()! == 3 && d.length() == 3
|
|
149
|
+
_ := d.popFront()
|
|
150
|
+
_ := d.popBack()
|
|
151
|
+
assert d.first()! == 2 && d.last()! == 2
|
|
152
|
+
|
|
153
|
+
test "fold, each and toSlice go front to back, also across the ring's wrap"
|
|
154
|
+
d := Deque<Int>()
|
|
155
|
+
for i := range 6
|
|
156
|
+
d.pushBack(i)
|
|
157
|
+
for i := range 5
|
|
158
|
+
_ := d.popFront()
|
|
159
|
+
for i := range 6
|
|
160
|
+
d.pushBack(10 + i)
|
|
161
|
+
d.pushFront(-1)
|
|
162
|
+
assert d.toSlice().toStr() == "[-1,5,10,11,12,13,14,15]"
|
|
163
|
+
assert d.fold(0, |a, x| a * 2 + x) == {{{{{{{-1 * 2 + 5} * 2 + 10} * 2 + 11} * 2 + 12} * 2 + 13} * 2 + 14} * 2 + 15}
|
|
164
|
+
seen := Slice<Int>()
|
|
165
|
+
d.each(|x| seen.push(x))
|
|
166
|
+
assert seen == d.toSlice()
|
|
167
|
+
assert Deque<Int>().toSlice().length() == 0
|
|
168
|
+
|
|
169
|
+
test "== compares the elements in order, not the ring layout"
|
|
170
|
+
a := Deque<Int>()
|
|
171
|
+
b := Deque<Int>()
|
|
172
|
+
for i := range 3
|
|
173
|
+
a.pushBack(i)
|
|
174
|
+
b.pushFront(2)
|
|
175
|
+
b.pushFront(1)
|
|
176
|
+
b.pushFront(0)
|
|
177
|
+
assert a == b
|
|
178
|
+
b.pushBack(3)
|
|
179
|
+
assert a != b
|
|
180
|
+
a.pushBack(4)
|
|
181
|
+
assert a != b
|
|
182
|
+
assert Deque<Str>() == Deque<Str>()
|
|
183
|
+
|
|
114
184
|
test "pop on empty is an error"
|
|
115
185
|
d := Deque<Int>()
|
|
116
186
|
assert d.popFront().unwrapOr(-1) == -1
|
std/collections/Heap.plum
CHANGED
|
@@ -13,6 +13,9 @@ import std/Str
|
|
|
13
13
|
# fits A* directly. The order is asked on each comparison, so keep it cheap
|
|
14
14
|
# and don't change an element's order while it is in the heap. `push`/`pop` are
|
|
15
15
|
# O(log n); `peek`/`length` are O(1). Equal elements pop in no guaranteed order.
|
|
16
|
+
# `toSlice`, `fold`, `each` and `==` go in pop order, on a copy (O(n log n)); the heap is
|
|
17
|
+
# left as it was. Two heaps are `==` when they hold the same elements in the same pop
|
|
18
|
+
# order (their `before` is not compared).
|
|
16
19
|
struct Heap<T>(items: Slice<T>, before: fn(T, T) -> Bool) =
|
|
17
20
|
# An empty heap ordered by `before`.
|
|
18
21
|
fun by(before: fn(T, T) -> Bool) -> Heap<T> =
|
|
@@ -76,6 +79,26 @@ struct Heap<T>(items: Slice<T>, before: fn(T, T) -> Bool) =
|
|
|
76
79
|
self.siftDown(0)
|
|
77
80
|
Some(top)
|
|
78
81
|
|
|
82
|
+
# The elements in pop order, as a new `Slice`; the heap is unchanged. O(n log n).
|
|
83
|
+
fun toSlice(self) -> Slice<T> =
|
|
84
|
+
copy := Heap(items: self.items.sublist(0, self.items.length()), before: self.before)
|
|
85
|
+
out := Slice<T>()
|
|
86
|
+
for i := range self.items.length()
|
|
87
|
+
out.push(copy.pop()!)
|
|
88
|
+
out
|
|
89
|
+
|
|
90
|
+
# Left fold in pop order: `f(f(acc, first), second)`. The heap is unchanged.
|
|
91
|
+
fun fold<A>(self, acc: A, f: fn(A, T) -> A) -> A =
|
|
92
|
+
self.toSlice().fold(acc, f)
|
|
93
|
+
|
|
94
|
+
# Calls `f` on each element in pop order. The heap is unchanged.
|
|
95
|
+
fun each(self, f: fn(T) -> Unit) -> Unit =
|
|
96
|
+
self.toSlice().each(f)
|
|
97
|
+
|
|
98
|
+
# `==` and `!=`: the same elements in the same pop order.
|
|
99
|
+
fun eq(self, o: Heap<T>) -> Bool =
|
|
100
|
+
self.items.length() == o.items.length() && self.toSlice() == o.toSlice()
|
|
101
|
+
|
|
79
102
|
test "heap_pop_order: 1000 pseudo-random keys pop in ascending order"
|
|
80
103
|
h: Heap<Int> := Heap.by(|a, b| a < b)
|
|
81
104
|
_ := 1.to(1000).fold(12345, |x, _|
|
|
@@ -114,3 +137,31 @@ test "byKey orders by a key of any type, and before can make a max-heap"
|
|
|
114
137
|
g.push({i * 37} % 100)
|
|
115
138
|
assert g.peek()! == 99
|
|
116
139
|
assert 0.until(100).fold(true, |ok, i| ok && g.pop()! == 99 - i)
|
|
140
|
+
|
|
141
|
+
test "toSlice, fold and each go in pop order and leave the heap as it was"
|
|
142
|
+
h: Heap<Int> := Heap.by(|a, b| a < b)
|
|
143
|
+
for i := range 5
|
|
144
|
+
h.push({i * 3 + 2} % 5 + 1)
|
|
145
|
+
assert h.toSlice().toStr() == "[1,2,3,4,5]"
|
|
146
|
+
assert h.fold(0, |a, x| a * 10 + x) == 12345
|
|
147
|
+
seen := Slice<Int>()
|
|
148
|
+
h.each(|x| seen.push(x))
|
|
149
|
+
assert seen.toStr() == "[1,2,3,4,5]"
|
|
150
|
+
assert h.length() == 5 && h.peek()! == 1
|
|
151
|
+
e: Heap<Int> := Heap.by(|a, b| a < b)
|
|
152
|
+
assert e.toSlice().length() == 0 && e.fold(7, |a, x| a + x) == 7
|
|
153
|
+
|
|
154
|
+
test "== is the same elements in the same pop order"
|
|
155
|
+
a: Heap<Int> := Heap.by(|a, b| a < b)
|
|
156
|
+
b: Heap<Int> := Heap.by(|a, b| a < b)
|
|
157
|
+
for i := range 3
|
|
158
|
+
a.push({i * 2 + 2} % 3 + 1)
|
|
159
|
+
for i := range 3
|
|
160
|
+
b.push(i + 1)
|
|
161
|
+
assert a == b
|
|
162
|
+
b.push(4)
|
|
163
|
+
assert a != b
|
|
164
|
+
m: Heap<Int> := Heap.by(|a, b| a > b)
|
|
165
|
+
for i := range 3
|
|
166
|
+
m.push(i + 1)
|
|
167
|
+
assert a != m
|