plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
fa335e7
— Peter John
2026-11-04T21:59:39+05:30
std: delete Seq (generators removed; T4)
- plum-std/Seq.plum +0 -168
plum-std/Seq.plum
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
module std
|
|
2
|
-
|
|
3
|
-
import std/Int
|
|
4
|
-
import std/Float
|
|
5
|
-
import std/Slice
|
|
6
|
-
|
|
7
|
-
# Sequences. `xs.filter()` in a `for` source reads like a method chain but is
|
|
8
|
-
# expanded at the use site into ONE loop: no closure, no `call_indirect`, no
|
|
9
|
-
# intermediate Slice. See `plum-core/src/forms.rs` for the precise rules.
|
|
10
|
-
#
|
|
11
|
-
# * `Seq[T]` is a compiler primitive; `*T` is sugar for it. A Seq never exists
|
|
12
|
-
# at runtime: it can't be stored, returned or captured, only flow into a
|
|
13
|
-
# `for` source or the next method call.
|
|
14
|
-
# * The methods below are declared with `extend Seq[T]`. `xs.m()` where the type
|
|
15
|
-
# of `xs` has no `m` of its own and is iterable (`length()`/`at(i)`) resolves
|
|
16
|
-
# to `Seq.m`, so `units.filter()` works on a Slice, an Array, a Deque, ...
|
|
17
|
-
# * ADAPTERS reshape a sequence and have no result (`take`, `skip`, `stepBy`,
|
|
18
|
-
# `zip`, `reversed`, `chunks`): they may appear anywhere in a chain.
|
|
19
|
-
# * TERMINALS declare a result `-> R` and take the `for` body (`filter`, `map`,
|
|
20
|
-
# `find`, `fold`, `count`, `any`, `all`, `each`, `firstN`, `toList`): a terminal
|
|
21
|
-
# must be the LAST call of the chain.
|
|
22
|
-
# * `yield a, b` runs the `for` body; as an expression (`if yield x`,
|
|
23
|
-
# `acc = yield acc, x`) its value is the body's last expression. A clause
|
|
24
|
-
# `, if cond` that is false skips the element like `continue` of the
|
|
25
|
-
# iterator's own loop; `break`/`continue` in the body act on that loop;
|
|
26
|
-
# `return` returns from the enclosing fn.
|
|
27
|
-
# * `self` of an adapter or terminal that only uses it as a `for` source is the
|
|
28
|
-
# whole upstream chain, which is how a chain fuses. `zip`, `reversed` and
|
|
29
|
-
# `chunks` need random access, so their receiver must be a Slice.
|
|
30
|
-
|
|
31
|
-
primitive Seq[T] =
|
|
32
|
-
# ---- adapters ---------------------------------------------------------------
|
|
33
|
-
|
|
34
|
-
# The first `n` elements.
|
|
35
|
-
fun* take(self, n: Int) *T =
|
|
36
|
-
seen := 0
|
|
37
|
-
for x := range self
|
|
38
|
-
if seen >= n
|
|
39
|
-
break
|
|
40
|
-
seen = seen + 1
|
|
41
|
-
yield x
|
|
42
|
-
|
|
43
|
-
# Everything after the first `n` elements.
|
|
44
|
-
fun* skip(self, n: Int) *T =
|
|
45
|
-
seen := 0
|
|
46
|
-
for x := range self
|
|
47
|
-
before := seen
|
|
48
|
-
seen = seen + 1
|
|
49
|
-
if before >= n
|
|
50
|
-
yield x
|
|
51
|
-
|
|
52
|
-
# Every `step`-th element, starting with the first.
|
|
53
|
-
fun* stepBy(self, step: Int) *T =
|
|
54
|
-
seen := 0
|
|
55
|
-
for x := range self
|
|
56
|
-
before := seen
|
|
57
|
-
seen = seen + 1
|
|
58
|
-
if before % step == 0
|
|
59
|
-
yield x
|
|
60
|
-
|
|
61
|
-
# `for a, b := range xs.zip(ys)`: pairs up to the shorter length.
|
|
62
|
-
fun* zip(self, ys: Slice[B]) *(T, B) =
|
|
63
|
-
n := self.length()
|
|
64
|
-
m := ys.length()
|
|
65
|
-
k := if n < m then n else m
|
|
66
|
-
for i := range k
|
|
67
|
-
yield self.at(i), ys.at(i)
|
|
68
|
-
|
|
69
|
-
# The elements back to front.
|
|
70
|
-
fun* reversed(self) *T =
|
|
71
|
-
n := self.length()
|
|
72
|
-
for i := range n
|
|
73
|
-
yield self.at(n - 1 - i)
|
|
74
|
-
|
|
75
|
-
# Consecutive pieces of `size` elements (the last may be shorter).
|
|
76
|
-
fun* chunks(self, size: Int) *Slice[T] =
|
|
77
|
-
n := self.length()
|
|
78
|
-
start := 0
|
|
79
|
-
while start < n
|
|
80
|
-
stop := if start + size < n then start + size else n
|
|
81
|
-
piece := self.sublist(start, stop)
|
|
82
|
-
start = stop
|
|
83
|
-
yield piece
|
|
84
|
-
|
|
85
|
-
# ---- terminals --------------------------------------------------------------
|
|
86
|
-
|
|
87
|
-
# The elements for which the body is true.
|
|
88
|
-
fun* filter(self) *T -> Slice[T] =
|
|
89
|
-
out := Slice()
|
|
90
|
-
for x := range self
|
|
91
|
-
if yield x
|
|
92
|
-
out.push(x)
|
|
93
|
-
out
|
|
94
|
-
|
|
95
|
-
# The body's value for every element.
|
|
96
|
-
fun* map(self) *T -> Slice[U] =
|
|
97
|
-
out := Slice()
|
|
98
|
-
for x := range self
|
|
99
|
-
v := yield x
|
|
100
|
-
out.push(v)
|
|
101
|
-
out
|
|
102
|
-
|
|
103
|
-
# The first element for which the body is true, or null. Stops there.
|
|
104
|
-
fun* find(self) *T -> T? =
|
|
105
|
-
hit := Slice()
|
|
106
|
-
for x := range self
|
|
107
|
-
if yield x
|
|
108
|
-
hit.push(x)
|
|
109
|
-
break
|
|
110
|
-
hit.head()
|
|
111
|
-
|
|
112
|
-
# `for acc, x := range xs.fold(init)`: the body's value is the next accumulator.
|
|
113
|
-
fun* fold(self, init: A) *(A, T) -> A =
|
|
114
|
-
acc := init
|
|
115
|
-
for x := range self
|
|
116
|
-
acc = yield acc, x
|
|
117
|
-
acc
|
|
118
|
-
|
|
119
|
-
# true when the body is true for some element. Stops at the first.
|
|
120
|
-
fun* any(self) *T -> Bool =
|
|
121
|
-
hits := 0
|
|
122
|
-
for x := range self
|
|
123
|
-
if yield x
|
|
124
|
-
hits = 1
|
|
125
|
-
break
|
|
126
|
-
hits == 1
|
|
127
|
-
|
|
128
|
-
# true when the body is true for every element. Stops at the first false.
|
|
129
|
-
fun* all(self) *T -> Bool =
|
|
130
|
-
misses := 0
|
|
131
|
-
for x := range self
|
|
132
|
-
ok := yield x
|
|
133
|
-
if !ok
|
|
134
|
-
misses = 1
|
|
135
|
-
break
|
|
136
|
-
misses == 0
|
|
137
|
-
|
|
138
|
-
# How many elements the body is true for.
|
|
139
|
-
fun* count(self) *T -> Int =
|
|
140
|
-
n := 0
|
|
141
|
-
for x := range self
|
|
142
|
-
if yield x
|
|
143
|
-
n = n + 1
|
|
144
|
-
n
|
|
145
|
-
|
|
146
|
-
# Runs the body for every element.
|
|
147
|
-
fun* each(self) *T -> Unit =
|
|
148
|
-
for x := range self
|
|
149
|
-
yield x
|
|
150
|
-
|
|
151
|
-
# The first `n` elements for which the body is true. Stops there.
|
|
152
|
-
fun* firstN(self, n: Int) *T -> Slice[T] =
|
|
153
|
-
out := Slice()
|
|
154
|
-
if n > 0
|
|
155
|
-
for x := range self
|
|
156
|
-
if yield x
|
|
157
|
-
out.push(x)
|
|
158
|
-
if out.length() >= n
|
|
159
|
-
break
|
|
160
|
-
out
|
|
161
|
-
|
|
162
|
-
# `xs.take(3).toList()`: the elements as a Slice (no body).
|
|
163
|
-
fun* toList(self) *T -> Slice[T] =
|
|
164
|
-
out := Slice()
|
|
165
|
-
for x := range self
|
|
166
|
-
yield x
|
|
167
|
-
out.push(x)
|
|
168
|
-
out
|