plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
9f3cdb1
— Peter John
2026-11-06T19:53:31+05:30
Simpler language: small std files off return and while, and fold, each, find
- std/Array.plum +5 -9
- std/Buffer.plum +6 -9
- std/Float.plum +77 -61
- std/Grid.plum +4 -4
- std/Int.plum +111 -34
- std/Io.plum +1 -1
- std/List.plum +32 -0
- std/Pair.plum +1 -1
- std/Set.plum +6 -8
std/Array.plum
CHANGED
|
@@ -68,13 +68,13 @@ primitive Array[T] =
|
|
|
68
68
|
fun with(self, i: Int, v: T) -> Array[T] =
|
|
69
69
|
c := self.resize(self.length())
|
|
70
70
|
c.set(i, v)
|
|
71
|
-
|
|
71
|
+
c
|
|
72
72
|
|
|
73
73
|
# The in-place `with`, picked by the compiler alone: the checker rejects a call written in
|
|
74
74
|
# source, so no program can write through an array without the proof.
|
|
75
75
|
fun withUnique(self, i: Int, v: T) -> Array[T] =
|
|
76
76
|
self.set(i, v)
|
|
77
|
-
|
|
77
|
+
self
|
|
78
78
|
|
|
79
79
|
# Returns a NEW array, one longer than `self`, with `v` appended at the end
|
|
80
80
|
# — `self` itself is left untouched (this allocates and copies, it does not
|
|
@@ -95,11 +95,9 @@ primitive Array[T] =
|
|
|
95
95
|
# in a native `array (mut i64)` / `(mut f64)` / `(mut i32)`.
|
|
96
96
|
fun filled(n: Int, v: T) -> Array[T] =
|
|
97
97
|
a := Array[T]().resize(n)
|
|
98
|
-
i := 0
|
|
99
|
-
|
|
98
|
+
for i := range n
|
|
100
99
|
a.set(i, v)
|
|
101
|
-
i = i + 1
|
|
102
|
-
|
|
100
|
+
a
|
|
103
101
|
|
|
104
102
|
# Copies `n` elements of `src` from `srcStart` into `dst` at `dstStart`
|
|
105
103
|
# (`array.copy`: overlapping ranges are fine). Traps if a range is out of bounds.
|
|
@@ -121,10 +119,8 @@ test "Array is a primitive with working generic methods"
|
|
|
121
119
|
|
|
122
120
|
test "Array[Int] holds a million elements unboxed"
|
|
123
121
|
a := Array.filled(1000000, 0)
|
|
124
|
-
i := 0
|
|
125
|
-
|
|
122
|
+
for i := range 1000000
|
|
126
123
|
a.set(i, i * 2)
|
|
127
|
-
i = i + 1
|
|
128
124
|
assert a.length() == 1000000
|
|
129
125
|
assert a.get(999999) == 1999998
|
|
130
126
|
assert a.get(0) == 0
|
std/Buffer.plum
CHANGED
|
@@ -64,12 +64,11 @@ struct Buffer(mut data: Array[Byte], mut len: Int) =
|
|
|
64
64
|
# allocation or copy at all.
|
|
65
65
|
fun ensureCapacity(self, needed: Int) -> Unit =
|
|
66
66
|
cap := self.data.length()
|
|
67
|
-
if needed
|
|
67
|
+
if needed > cap
|
|
68
|
-
return
|
|
69
|
-
|
|
68
|
+
doubled := if cap == 0 then 16 else cap * 2
|
|
70
|
-
|
|
69
|
+
new_cap := if needed > doubled then needed else doubled
|
|
71
|
-
|
|
70
|
+
new_data := self.data.resize(new_cap)
|
|
72
|
-
|
|
71
|
+
self.data = new_data
|
|
73
72
|
|
|
74
73
|
test "Buffer() with no args runs init() and works immediately"
|
|
75
74
|
b := Buffer()
|
|
@@ -79,10 +78,8 @@ test "Buffer() with no args runs init() and works immediately"
|
|
|
79
78
|
|
|
80
79
|
test "toStr length equals bytes written, even after capacity doubling"
|
|
81
80
|
b := Buffer()
|
|
82
|
-
i := 0
|
|
83
|
-
|
|
81
|
+
for i := range 100
|
|
84
82
|
b.write("ab")
|
|
85
|
-
i = i + 1
|
|
86
83
|
b.writeByte(Byte(33))
|
|
87
84
|
s := b.toStr()
|
|
88
85
|
assert s.length() == 201
|
std/Float.plum
CHANGED
|
@@ -32,37 +32,46 @@ primitive Float =
|
|
|
32
32
|
fun parse(s: Str) -> Result[Float, NumError] =
|
|
33
33
|
len := s.length()
|
|
34
34
|
if len == 0
|
|
35
|
-
|
|
35
|
+
Err(BadNumStr)
|
|
36
|
+
else
|
|
36
|
-
|
|
37
|
+
neg := s.byteAt(0) == 45
|
|
37
|
-
|
|
38
|
+
start := if neg || s.byteAt(0) == 43 then 1 else 0
|
|
38
|
-
|
|
39
|
+
if start >= len
|
|
39
|
-
|
|
40
|
+
Err(BadNumStr)
|
|
40
|
-
|
|
41
|
+
else
|
|
41
|
-
|
|
42
|
+
parseWhole(s, start, len, 0.0f, false, neg)
|
|
43
|
+
|
|
42
|
-
|
|
44
|
+
# `Float.parse`'s integer-part loop: digits of `s` from `i` up to the `.` (or the end).
|
|
45
|
+
fun parseWhole(s: Str, i: Int, len: Int, whole: Float, sawDigit: Bool, neg: Bool) -> Result[Float, NumError] =
|
|
43
|
-
|
|
46
|
+
if i < len && s.byteAt(i) != 46
|
|
44
47
|
b := s.byteAt(i)
|
|
45
48
|
if b < 48 || b > 57
|
|
46
|
-
|
|
49
|
+
Err(BadNumStr)
|
|
50
|
+
else
|
|
47
|
-
|
|
51
|
+
parseWhole(s, i + 1, len, whole * 10.0f + Float(b - 48), true, neg)
|
|
48
|
-
|
|
52
|
+
else if i < len
|
|
49
|
-
i = i + 1
|
|
50
|
-
frac_part := 0.0f
|
|
51
|
-
frac_scale := 1.0f
|
|
52
|
-
|
|
53
|
+
parseFrac(s, i + 1, len, whole, 0.0f, 1.0f, sawDigit, neg)
|
|
53
|
-
|
|
54
|
+
else
|
|
55
|
+
parseDone(whole, 0.0f, sawDigit, neg)
|
|
56
|
+
|
|
57
|
+
# `Float.parse`'s fractional loop: digits after the `.`, `scale` the current place value.
|
|
58
|
+
fun parseFrac(s: Str, i: Int, len: Int, whole: Float, frac: Float, scale: Float, sawDigit: Bool, neg: Bool) -> Result[Float, NumError] =
|
|
54
|
-
|
|
59
|
+
if i >= len
|
|
60
|
+
parseDone(whole, frac, sawDigit, neg)
|
|
61
|
+
else
|
|
55
|
-
|
|
62
|
+
b := s.byteAt(i)
|
|
56
|
-
|
|
63
|
+
if b < 48 || b > 57
|
|
57
|
-
|
|
64
|
+
Err(BadNumStr)
|
|
65
|
+
else
|
|
58
|
-
|
|
66
|
+
nextScale := scale / 10.0f
|
|
59
|
-
|
|
67
|
+
parseFrac(s, i + 1, len, whole, frac + Float(b - 48) * nextScale, nextScale, true, neg)
|
|
68
|
+
|
|
60
|
-
|
|
69
|
+
fun parseDone(whole: Float, frac: Float, sawDigit: Bool, neg: Bool) -> Result[Float, NumError] =
|
|
61
|
-
i = i + 1
|
|
62
|
-
if !
|
|
70
|
+
if !sawDigit
|
|
63
|
-
|
|
71
|
+
Err(BadNumStr)
|
|
72
|
+
else
|
|
64
|
-
|
|
73
|
+
value := whole + frac
|
|
65
|
-
|
|
74
|
+
Ok(if neg then -value else value)
|
|
66
75
|
|
|
67
76
|
fun abs(self) -> Float =
|
|
68
77
|
if self < 0.0f then -self else self
|
|
@@ -71,10 +80,11 @@ primitive Float =
|
|
|
71
80
|
# `Copysign`/`sign` conventions loosely.
|
|
72
81
|
fun sign(self) -> Float =
|
|
73
82
|
if self > 0.0f
|
|
74
|
-
|
|
83
|
+
1.0f
|
|
75
|
-
if self < 0.0f
|
|
84
|
+
else if self < 0.0f
|
|
76
|
-
|
|
85
|
+
-1.0f
|
|
86
|
+
else
|
|
77
|
-
|
|
87
|
+
self
|
|
78
88
|
|
|
79
89
|
# A `Float` hashes via a truncating conversion (no bit-level float hashing
|
|
80
90
|
# attempted).
|
|
@@ -89,14 +99,16 @@ primitive Float =
|
|
|
89
99
|
fun floor(self) -> Float =
|
|
90
100
|
t := Float(Int(self))
|
|
91
101
|
if self < 0.0f && t != self
|
|
92
|
-
|
|
102
|
+
t - 1.0f
|
|
93
|
-
|
|
103
|
+
else
|
|
104
|
+
t
|
|
94
105
|
|
|
95
106
|
fun ceil(self) -> Float =
|
|
96
107
|
t := Float(Int(self))
|
|
97
108
|
if self > 0.0f && t != self
|
|
98
|
-
|
|
109
|
+
t + 1.0f
|
|
99
|
-
|
|
110
|
+
else
|
|
111
|
+
t
|
|
100
112
|
|
|
101
113
|
# Round-half-away-from-zero (matching Go's `math.Round`), not
|
|
102
114
|
# round-half-to-even.
|
|
@@ -104,7 +116,7 @@ primitive Float =
|
|
|
104
116
|
neg := self < 0.0f
|
|
105
117
|
v := if neg then -self else self
|
|
106
118
|
r := {v + 0.5f}.floor()
|
|
107
|
-
|
|
119
|
+
if neg then -r else r
|
|
108
120
|
|
|
109
121
|
fun log(self) -> Float =
|
|
110
122
|
math.ln(self)
|
|
@@ -118,8 +130,9 @@ primitive Float =
|
|
|
118
130
|
# floor(log2(|self|)).
|
|
119
131
|
fun logb(self) -> Float =
|
|
120
132
|
if self == 0.0f
|
|
121
|
-
|
|
133
|
+
-1.0f / 0.0f
|
|
134
|
+
else
|
|
122
|
-
|
|
135
|
+
{math.ln(if self < 0.0f then -self else self) * Float.Log2E}.floor()
|
|
123
136
|
|
|
124
137
|
fun sqrt(self) -> Float =
|
|
125
138
|
math.sqrt(self)
|
|
@@ -149,42 +162,45 @@ primitive Float =
|
|
|
149
162
|
# Inverse hyperbolic cosine, via acosh(x) = ln(x + sqrt(x^2 - 1)), x >= 1.
|
|
150
163
|
fun acosh(self) -> Float =
|
|
151
164
|
if self < 1.0f
|
|
152
|
-
|
|
165
|
+
0.0f / 0.0f
|
|
166
|
+
else
|
|
153
|
-
|
|
167
|
+
math.ln(self + math.sqrt(self * self - 1.0f))
|
|
154
168
|
|
|
155
169
|
fun sinh(self) -> Float =
|
|
156
|
-
|
|
170
|
+
{math.exp(self) - math.exp(-self)} / 2.0f
|
|
157
171
|
|
|
158
172
|
fun cosh(self) -> Float =
|
|
159
|
-
|
|
173
|
+
{math.exp(self) + math.exp(-self)} / 2.0f
|
|
160
174
|
|
|
161
175
|
fun tanh(self) -> Float =
|
|
162
|
-
|
|
176
|
+
self.sinh() / self.cosh()
|
|
163
177
|
|
|
164
178
|
# Inverse hyperbolic sine: asinh(x) = ln(x + sqrt(x^2 + 1)).
|
|
165
179
|
fun asinh(self) -> Float =
|
|
166
|
-
|
|
180
|
+
math.ln(self + math.sqrt(self * self + 1.0f))
|
|
167
181
|
|
|
168
182
|
# Inverse hyperbolic tangent: atanh(x) = 0.5*ln((1+x)/(1-x)), |x| < 1.
|
|
169
183
|
fun atanh(self) -> Float =
|
|
170
184
|
if self <= -1.0f || self >= 1.0f
|
|
171
|
-
|
|
185
|
+
0.0f / 0.0f
|
|
186
|
+
else
|
|
172
|
-
|
|
187
|
+
0.5f * math.ln({1.0f + self} / {1.0f - self})
|
|
173
188
|
|
|
174
189
|
# Rounded to 6 fractional digits, with trailing zeros trimmed (but always
|
|
175
190
|
# at least one digit after the point, e.g. `3.0` not `3.`).
|
|
176
191
|
fun toStr(self) -> Str =
|
|
177
192
|
if self != self
|
|
178
|
-
|
|
193
|
+
"NaN"
|
|
179
|
-
if self > Float.Max || self < -Float.Max
|
|
194
|
+
else if self > Float.Max || self < -Float.Max
|
|
180
|
-
|
|
195
|
+
if self < 0.0f then "-Infinity" else "Infinity"
|
|
196
|
+
else
|
|
181
|
-
|
|
197
|
+
neg := self < 0.0f
|
|
182
|
-
|
|
198
|
+
abs_val := if neg then -self else self
|
|
183
|
-
|
|
199
|
+
int_part := Int(abs_val)
|
|
184
|
-
|
|
200
|
+
frac_val := abs_val - Float(int_part)
|
|
185
|
-
|
|
201
|
+
scaled := Int(frac_val * 1000000.0f + 0.5f)
|
|
186
|
-
|
|
202
|
+
carry := scaled >= 1000000
|
|
187
|
-
|
|
203
|
+
whole_part := if carry then int_part + 1 else int_part
|
|
188
|
-
|
|
204
|
+
frac := if carry then 0 else scaled
|
|
189
|
-
|
|
205
|
+
s := Int.intToStr(whole_part) + "." + Int.trimmedFracDigits(frac, 6)
|
|
190
|
-
|
|
206
|
+
if neg then "-" + s else s
|
std/Grid.plum
CHANGED
|
@@ -15,17 +15,17 @@ struct Grid[T](w: Int, h: Int, data: Array[T]) =
|
|
|
15
15
|
# `Grid.filled(w, h, v)` — a `w` x `h` grid with every cell set to `v`
|
|
16
16
|
# (`T` is inferred from `v`).
|
|
17
17
|
fun filled(w: Int, h: Int, v: T) -> Grid[T] =
|
|
18
|
-
|
|
18
|
+
Grid(w: w, h: h, data: Array.filled(w * h, v))
|
|
19
19
|
|
|
20
20
|
# Flat index of cell `(x, y)`.
|
|
21
21
|
fun index(self, x: Int, y: Int) -> Int =
|
|
22
|
-
|
|
22
|
+
y * self.w + x
|
|
23
23
|
|
|
24
24
|
fun inBounds(self, x: Int, y: Int) -> Bool =
|
|
25
|
-
|
|
25
|
+
x >= 0 && y >= 0 && x < self.w && y < self.h
|
|
26
26
|
|
|
27
27
|
fun get(self, x: Int, y: Int) -> T =
|
|
28
|
-
|
|
28
|
+
self.data.get(y * self.w + x)
|
|
29
29
|
|
|
30
30
|
fun set(self, x: Int, y: Int, v: T) -> Unit =
|
|
31
31
|
self.data.set(y * self.w + x, v)
|
std/Int.plum
CHANGED
|
@@ -6,6 +6,7 @@ import std/math
|
|
|
6
6
|
import std/Pair
|
|
7
7
|
import std/Option
|
|
8
8
|
import std/Result
|
|
9
|
+
import std/Array
|
|
9
10
|
|
|
10
11
|
# Why `Int.parse` / `Float.parse` failed.
|
|
11
12
|
enum NumError = BadNumStr
|
|
@@ -21,20 +22,25 @@ primitive Int =
|
|
|
21
22
|
fun parse(s: Str) -> Result[Int, NumError] =
|
|
22
23
|
len := s.length()
|
|
23
24
|
if len == 0
|
|
24
|
-
|
|
25
|
+
Err(BadNumStr)
|
|
26
|
+
else
|
|
25
|
-
|
|
27
|
+
neg := s.byteAt(0) == 45
|
|
26
|
-
|
|
28
|
+
start := if neg || s.byteAt(0) == 43 then 1 else 0
|
|
27
|
-
|
|
29
|
+
if start >= len
|
|
28
|
-
|
|
30
|
+
Err(BadNumStr)
|
|
29
|
-
|
|
31
|
+
else
|
|
30
|
-
|
|
32
|
+
parseDigits(s, start, len, 0, neg)
|
|
33
|
+
|
|
34
|
+
# `Int.parse`'s loop: the digits of `s` from `i` on, `value` so far.
|
|
35
|
+
fun parseDigits(s: Str, i: Int, len: Int, value: Int, neg: Bool) -> Result[Int, NumError] =
|
|
31
|
-
|
|
36
|
+
if i >= len
|
|
37
|
+
Ok(if neg then -value else value)
|
|
38
|
+
else
|
|
32
39
|
b := s.byteAt(i)
|
|
33
40
|
if b < 48 || b > 57
|
|
34
|
-
|
|
41
|
+
Err(BadNumStr)
|
|
42
|
+
else
|
|
35
|
-
|
|
43
|
+
parseDigits(s, i + 1, len, value * 10 + b - 48, neg)
|
|
36
|
-
i = i + 1
|
|
37
|
-
return Ok(if neg then -value else value)
|
|
38
44
|
|
|
39
45
|
fun abs(self) -> Int =
|
|
40
46
|
if self < 0 then -self else self
|
|
@@ -74,13 +80,17 @@ primitive Int =
|
|
|
74
80
|
# through Float rounding error.
|
|
75
81
|
fun logb(self) -> Float =
|
|
76
82
|
if self == 0
|
|
77
|
-
|
|
83
|
+
-1.0f / 0.0f
|
|
84
|
+
else
|
|
78
|
-
|
|
85
|
+
n := if self < 0 then -self else self
|
|
79
|
-
|
|
86
|
+
Float(log2Floor(n, 0))
|
|
87
|
+
|
|
88
|
+
# floor(log2(n)) for n > 0, counting halvings in `k`.
|
|
89
|
+
fun log2Floor(n: Int, k: Int) -> Int =
|
|
80
|
-
|
|
90
|
+
if n >= 2
|
|
81
|
-
n
|
|
91
|
+
log2Floor(n / 2, k + 1)
|
|
82
|
-
k = k + 1
|
|
83
|
-
|
|
92
|
+
else
|
|
93
|
+
k
|
|
84
94
|
|
|
85
95
|
fun sqrt(self) -> Float =
|
|
86
96
|
math.sqrt(Float(self))
|
|
@@ -147,11 +157,12 @@ primitive Int =
|
|
|
147
157
|
# re-dispatching back through `Float.toStr` itself.
|
|
148
158
|
fun intToStr(n: Int) -> Str =
|
|
149
159
|
if n == 0
|
|
150
|
-
|
|
160
|
+
"0"
|
|
161
|
+
else
|
|
151
|
-
|
|
162
|
+
neg := n < 0
|
|
152
|
-
|
|
163
|
+
v := if neg then -n else n
|
|
153
|
-
|
|
164
|
+
s := digitsToStr(v)
|
|
154
|
-
|
|
165
|
+
if neg then "-" + s else s
|
|
155
166
|
|
|
156
167
|
# Builds the decimal digits of a positive Int, most significant first, by
|
|
157
168
|
# peeling off one digit at a time from the least significant end (so the
|
|
@@ -159,8 +170,9 @@ primitive Int =
|
|
|
159
170
|
# digit is appended last).
|
|
160
171
|
fun digitsToStr(n: Int) -> Str =
|
|
161
172
|
if n == 0
|
|
162
|
-
|
|
173
|
+
""
|
|
174
|
+
else
|
|
163
|
-
|
|
175
|
+
digitsToStr(n / 10) + digitChar(n % 10)
|
|
164
176
|
|
|
165
177
|
fun digitChar(d: Int) -> Str =
|
|
166
178
|
match d
|
|
@@ -181,22 +193,25 @@ primitive Int =
|
|
|
181
193
|
# render a fractional part at a fixed width regardless of its own leading zeros.
|
|
182
194
|
fun fixedFracDigits(n: Int, place_value: Int) -> Str =
|
|
183
195
|
if place_value == 0
|
|
184
|
-
|
|
196
|
+
""
|
|
197
|
+
else
|
|
185
|
-
|
|
198
|
+
digitChar(n / place_value % 10) + fixedFracDigits(n, place_value / 10)
|
|
186
199
|
|
|
187
200
|
fun pow10(n: Int) -> Int =
|
|
188
201
|
if n <= 0
|
|
202
|
+
1
|
|
189
|
-
|
|
203
|
+
else
|
|
190
|
-
|
|
204
|
+
10 * pow10(n - 1)
|
|
191
205
|
|
|
192
206
|
# `fixedFracDigits(n, pow10(digits - 1))`, minus however many trailing-zero
|
|
193
207
|
# digits `n` has — but always at least one digit ("0" if `n` is 0 outright).
|
|
194
208
|
fun trimmedFracDigits(n: Int, digits: Int) -> Str =
|
|
195
209
|
if n == 0 || digits <= 1
|
|
196
|
-
|
|
210
|
+
digitChar(n % 10)
|
|
197
|
-
if n % 10 == 0
|
|
211
|
+
else if n % 10 == 0
|
|
198
|
-
|
|
212
|
+
trimmedFracDigits(n / 10, digits - 1)
|
|
213
|
+
else
|
|
199
|
-
|
|
214
|
+
fixedFracDigits(n, pow10(digits - 1))
|
|
200
215
|
|
|
201
216
|
# The ints `start`, `start + step`, ... up to (excluding) `stop`; counting down
|
|
202
217
|
# when `step` is negative. A `for x := range v` source via `next`.
|
|
@@ -206,6 +221,68 @@ struct Range(start: Int, stop: Int, step: Int) =
|
|
|
206
221
|
more := if self.step > 0 then self.start < self.stop else self.start > self.stop
|
|
207
222
|
if more then Some(Pair(first: self.start, second: Range(start: self.start + self.step, stop: self.stop, step: self.step))) else None
|
|
208
223
|
|
|
224
|
+
# Left fold over the range: `f(f(f(acc, a), b), c)`. Constant stack.
|
|
225
|
+
fun fold[A](self, acc: A, f: fn(A, Int) -> A) -> A =
|
|
226
|
+
more := if self.step > 0 then self.start < self.stop else self.start > self.stop
|
|
227
|
+
if more
|
|
228
|
+
Range(start: self.start + self.step, stop: self.stop, step: self.step).fold(f(acc, self.start), f)
|
|
229
|
+
else
|
|
230
|
+
acc
|
|
231
|
+
|
|
232
|
+
# Calls `f` on each element, in order. Constant stack.
|
|
233
|
+
fun each(self, f: fn(Int) -> Unit) -> Unit =
|
|
234
|
+
more := if self.step > 0 then self.start < self.stop else self.start > self.stop
|
|
235
|
+
if more
|
|
236
|
+
f(self.start)
|
|
237
|
+
Range(start: self.start + self.step, stop: self.stop, step: self.step).each(f)
|
|
238
|
+
|
|
239
|
+
# The first element for which `keep` holds, or None. Stops at the first hit.
|
|
240
|
+
fun find(self, keep: fn(Int) -> Bool) -> Option[Int] =
|
|
241
|
+
more := if self.step > 0 then self.start < self.stop else self.start > self.stop
|
|
242
|
+
if more
|
|
243
|
+
if keep(self.start)
|
|
244
|
+
Some(self.start)
|
|
245
|
+
else
|
|
246
|
+
Range(start: self.start + self.step, stop: self.stop, step: self.step).find(keep)
|
|
247
|
+
else
|
|
248
|
+
None
|
|
249
|
+
|
|
209
250
|
# Every `by`-th element, starting with the first (`by` > 0).
|
|
210
251
|
fun stepBy(self, by: Int) -> Range =
|
|
211
252
|
Range(start: self.start, stop: self.stop, step: if self.step > 0 then by else -by)
|
|
253
|
+
|
|
254
|
+
# `step` applied to `s` until `done(s)` holds; returns that state. Constant stack.
|
|
255
|
+
fun repeatUntil[S](s: S, done: fn(S) -> Bool, step: fn(S) -> S) -> S =
|
|
256
|
+
if done(s)
|
|
257
|
+
s
|
|
258
|
+
else
|
|
259
|
+
repeatUntil(step(s), done, step)
|
|
260
|
+
|
|
261
|
+
# Test helper: counts its calls in `calls[0]`, then compares.
|
|
262
|
+
fun countedIs(calls: Array[Int], x: Int, want: Int) -> Bool =
|
|
263
|
+
calls.set(0, calls.get(0) + 1)
|
|
264
|
+
x == want
|
|
265
|
+
|
|
266
|
+
fun bump(calls: Array[Int], x: Int) -> Unit =
|
|
267
|
+
calls.set(0, calls.get(0) + 1)
|
|
268
|
+
|
|
269
|
+
test "range fold, each, find"
|
|
270
|
+
assert 1.to(10).fold(0, |a, x| a + x) == 55
|
|
271
|
+
assert 5.until(5).fold(7, |a, x| a + x) == 7
|
|
272
|
+
assert 3.downTo(1).fold(0, |a, x| a * 10 + x) == 321
|
|
273
|
+
assert 1.to(10).find(|x| x * x > 20).unwrapOr(-1) == 5
|
|
274
|
+
assert 1.to(3).find(|x| x > 9).isNone()
|
|
275
|
+
|
|
276
|
+
test "range fold, each, find, repeatUntil run a million iterations"
|
|
277
|
+
assert 0.until(1000000).fold(0, |a, x| a + 1) == 1000000
|
|
278
|
+
assert 0.until(1000000).find(|x| x == 999999).unwrapOr(-1) == 999999
|
|
279
|
+
calls := Array.filled(1, 0)
|
|
280
|
+
0.until(1000000).each(|x| bump(calls, x))
|
|
281
|
+
assert calls.get(0) == 1000000
|
|
282
|
+
assert repeatUntil(0, |n| n >= 1000000, |n| n + 1) == 1000000
|
|
283
|
+
|
|
284
|
+
test "range find exits early"
|
|
285
|
+
calls := Array.filled(1, 0)
|
|
286
|
+
r := 0.until(1000000).find(|x| countedIs(calls, x, 3))
|
|
287
|
+
assert r.unwrapOr(-1) == 3
|
|
288
|
+
assert calls.get(0) == 4
|
std/Io.plum
CHANGED
|
@@ -39,4 +39,4 @@ primitive Io =
|
|
|
39
39
|
fun randomSeed(self) -> Int =
|
|
40
40
|
hi := self.rawRandomHalf()
|
|
41
41
|
lo := self.rawRandomHalf() & 0xFFFFFFFF
|
|
42
|
-
|
|
42
|
+
{ hi << 32 } | lo
|
std/List.plum
CHANGED
|
@@ -41,6 +41,17 @@ enum List[T] =
|
|
|
41
41
|
Nil => acc
|
|
42
42
|
Cons(h, t) => t.fold(f(acc, h), f)
|
|
43
43
|
|
|
44
|
+
# Calls `f` on each element, in order. Constant stack.
|
|
45
|
+
fun each(self, f: fn(T) -> Unit) -> Unit =
|
|
46
|
+
if Cons(h, t) := self
|
|
47
|
+
f(h)
|
|
48
|
+
t.each(f)
|
|
49
|
+
|
|
50
|
+
# The first element for which `keep` holds, or None. Stops at the first hit.
|
|
51
|
+
fun find(self, keep: fn(T) -> Bool) -> Option[T] = match self
|
|
52
|
+
Nil => None
|
|
53
|
+
Cons(h, t) => if keep(h) then Some(h) else t.find(keep)
|
|
54
|
+
|
|
44
55
|
# Pushes the elements of `self` onto `acc` one by one (so they come out reversed).
|
|
45
56
|
fun revOnto(self, acc: List[T]) -> List[T] = match self
|
|
46
57
|
Nil => acc
|
|
@@ -118,6 +129,14 @@ fun mapCells[A, B](xs: List[A], f: fn(A) -> B) -> List[B] = match xs
|
|
|
118
129
|
Nil => Nil
|
|
119
130
|
Cons(h, t) => Cons(head: f(h), tail: mapCells(t, f))
|
|
120
131
|
|
|
132
|
+
# Test helper: counts its calls in `calls[0]`, then compares.
|
|
133
|
+
fun countedIs(calls: Array[Int], x: Int, want: Int) -> Bool =
|
|
134
|
+
calls.set(0, calls.get(0) + 1)
|
|
135
|
+
x == want
|
|
136
|
+
|
|
137
|
+
fun bump(calls: Array[Int], x: Int) -> Unit =
|
|
138
|
+
calls.set(0, calls.get(0) + 1)
|
|
139
|
+
|
|
121
140
|
test "filter_million_constant_stack"
|
|
122
141
|
xs: List[Int] := Nil
|
|
123
142
|
i := 1000000
|
|
@@ -126,3 +145,16 @@ test "filter_million_constant_stack"
|
|
|
126
145
|
i = i - 1
|
|
127
146
|
evens := xs.filter(|x| x % 2 == 0)
|
|
128
147
|
assert evens.length() == 500000
|
|
148
|
+
|
|
149
|
+
test "list fold, each, find: a million elements, find exits early"
|
|
150
|
+
nil: List[Int] := Nil
|
|
151
|
+
xs := 1.to(1000000).fold(nil, |acc, i| Cons(head: i, tail: acc))
|
|
152
|
+
assert xs.fold(0, |a, x| a + 1) == 1000000
|
|
153
|
+
calls := Array.filled(1, 0)
|
|
154
|
+
xs.each(|x| bump(calls, x))
|
|
155
|
+
assert calls.get(0) == 1000000
|
|
156
|
+
assert xs.find(|x| x == 1).unwrapOr(-1) == 1
|
|
157
|
+
assert xs.find(|x| x < 0).isNone()
|
|
158
|
+
hits := Array.filled(1, 0)
|
|
159
|
+
assert xs.find(|x| countedIs(hits, x, 999998)).unwrapOr(-1) == 999998
|
|
160
|
+
assert hits.get(0) == 3
|
std/Pair.plum
CHANGED
|
@@ -10,4 +10,4 @@ import std/Option
|
|
|
10
10
|
struct Pair[A, B](first: A, second: B) =
|
|
11
11
|
|
|
12
12
|
fun toStr(self) -> Str =
|
|
13
|
-
|
|
13
|
+
"{self.first.toStr()}: {self.second.toStr()}"
|
std/Set.plum
CHANGED
|
@@ -11,23 +11,23 @@ import std/Float
|
|
|
11
11
|
struct Set[T](map: Map[T, Bool]) =
|
|
12
12
|
|
|
13
13
|
fun init() -> Set[T] =
|
|
14
|
-
|
|
14
|
+
Set(map: Map.empty())
|
|
15
15
|
|
|
16
16
|
# Adds `v`; adding a value already present changes nothing.
|
|
17
17
|
fun add(self, v: T) -> Unit =
|
|
18
18
|
self.map.set(v, true)
|
|
19
19
|
|
|
20
20
|
fun has(self, v: T) -> Bool =
|
|
21
|
-
|
|
21
|
+
self.map.has(v)
|
|
22
22
|
|
|
23
23
|
fun remove(self, v: T) -> Unit =
|
|
24
24
|
self.map.remove(v)
|
|
25
25
|
|
|
26
26
|
fun length(self) -> Int =
|
|
27
|
-
|
|
27
|
+
self.map.length()
|
|
28
28
|
|
|
29
29
|
fun isEmpty(self) -> Bool =
|
|
30
|
-
|
|
30
|
+
self.map.isEmpty()
|
|
31
31
|
|
|
32
32
|
fun clear(self) -> Unit =
|
|
33
33
|
self.map.clear()
|
|
@@ -37,14 +37,12 @@ struct Set[T](map: Map[T, Bool]) =
|
|
|
37
37
|
result := Slice[T]()
|
|
38
38
|
for k := range self.map.keys()
|
|
39
39
|
result.push(k)
|
|
40
|
-
|
|
40
|
+
result
|
|
41
41
|
|
|
42
42
|
test "set_dedup: adding duplicates keeps one copy"
|
|
43
43
|
s := Set[Int]()
|
|
44
|
-
i := 0
|
|
45
|
-
|
|
44
|
+
for i := range 100
|
|
46
45
|
s.add(i % 10)
|
|
47
|
-
i = i + 1
|
|
48
46
|
assert s.length() == 10
|
|
49
47
|
assert s.has(3)
|
|
50
48
|
assert s.has(10) == false
|