plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
5a570ae
— Peter John
2026-11-10T22:01:10+05:30
List: any and all, as on Slice, Map and Set
- docs/std.md +1 -1
- std/collections/List.plum +14 -0
docs/std.md
CHANGED
|
@@ -75,7 +75,7 @@ fun main() =
|
|
|
75
75
|
|
|
76
76
|
## Sequences
|
|
77
77
|
|
|
78
|
-
**`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
|
+
**`List`** suits recursion: `map`, `filter`, `fold`, `each`, `find`, `any`, `all`, `append`, `reverse`, `length`, `isEmpty`, `get` (an `Option`), `at` (crashes out of range), `join(sep)` (each element's `toStr()` with `sep` between, like `Slice.join`), `toSlice`, `toArray`, `List.fromSlice`, `==`. Each runs in constant stack (a million elements are fine).
|
|
79
79
|
|
|
80
80
|
**`Slice`** is a growable array:
|
|
81
81
|
|
std/collections/List.plum
CHANGED
|
@@ -52,6 +52,14 @@ enum List<T> =
|
|
|
52
52
|
Nil => None
|
|
53
53
|
Cons(h, t) => if keep(h) then Some(h) else t.find(keep)
|
|
54
54
|
|
|
55
|
+
# Whether `keep` holds for some element. Stops at the first hit.
|
|
56
|
+
fun any(self, keep: fn(T) -> Bool) -> Bool = self.find(keep).isSome()
|
|
57
|
+
|
|
58
|
+
# Whether `keep` holds for every element (true when empty). Stops at the first miss.
|
|
59
|
+
fun all(self, keep: fn(T) -> Bool) -> Bool = match self
|
|
60
|
+
Nil => true
|
|
61
|
+
Cons(h, t) => if keep(h) then t.all(keep) else false
|
|
62
|
+
|
|
55
63
|
# Pushes the elements of `self` onto `acc` one by one (so they come out reversed).
|
|
56
64
|
fun revOnto(self, acc: List<T>) -> List<T> = match self
|
|
57
65
|
Nil => acc
|
|
@@ -172,3 +180,9 @@ test "join of 100,000 pieces"
|
|
|
172
180
|
assert s.startsWith("0,1,2,")
|
|
173
181
|
assert s.endsWith(",99998,99999")
|
|
174
182
|
assert s.length() == 588889
|
|
183
|
+
|
|
184
|
+
test "any and all stop early, a million elements in constant stack"
|
|
185
|
+
nil: List<Int> := Nil
|
|
186
|
+
xs := 1.to(1000000).fold(nil, |acc, i| Cons(head: i, tail: acc))
|
|
187
|
+
assert xs.any(|x| x == 1) && !xs.any(|x| x < 0) && xs.all(|x| x > 0) && !xs.all(|x| x > 1)
|
|
188
|
+
assert !nil.any(|x| true) && nil.all(|x| false)
|