forked from strymonas/strymonas-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStream.scala
227 lines (179 loc) · 6.06 KB
/
Stream.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package strymonas
import scala.quoted._
import scala.reflect.ClassTag
class Cooked[A: Type](val shape: (raw: Raw) ?=> raw.Stream[raw.Cde[A]]) {
def flatMap[B: Type](f: Cde[A] => Cooked[B])(using Quotes): Cooked[B] = {
def newShape(using raw: Raw) =
import raw._
import raw.code._
flatMapRaw[A, Cde[B]](x => f(x).shape, shape)
Cooked(newShape)
}
def map[B: Type](f: Cde[A] => Cde[B])(using Quotes): Cooked[B] = {
def newShape(using raw: Raw) =
import raw._
import raw.code._
mapRaw[Cde[A], Cde[B]](a => letl(f(a)), shape)
Cooked[B](newShape)
}
def filter(f: Cde[A] => Cde[Boolean])(using Quotes): Cooked[A] = {
def newShape(using raw: Raw) =
import raw._
import raw.code._
filterRaw[Cde[A]](f, shape)
Cooked[A](newShape)
}
def zipWith[B: Type, C: Type](str2: Cooked[B], f: (Cde[A], Cde[B]) => Cde[C])(using Quotes): Cooked[C] = {
def newShape(using raw: Raw) =
import raw._
import raw.code._
mapRaw_Direct[(Cde[A], Cde[B]), Cde[C]](p => f(p._1, p._2), zipRaw[Cde[A], Cde[B]](shape, str2.shape))
Cooked[C](newShape)
}
def take(n: Cde[Int])(using Quotes): Cooked[A] = {
def newShape(using raw: Raw) =
import raw._
import raw.code.{_, given}
mkInit(
n - int(1),
i => {
var vsSt: Stream[Cde[Unit]] =
mkPullArray[Cde[Unit]](i, i => k => k(unit))
val zipSt: Stream[(Cde[Unit], Cde[A])] = zipRaw(vsSt, shape)
mapRaw_Direct[(Cde[Unit], Cde[A]), Cde[A]](_._2, zipSt)
}
)
Cooked[A](newShape)
}
def takeWhile(f: (Cde[A] => Cde[Boolean]))(using Quotes): Cooked[A] = {
def newShape(using raw: Raw): raw.Stream[raw.Cde[A]] =
import raw._
import raw.Goon._
import raw.code._
mkInitVar(
bool(true),
zr => mapRaw((e: Cde[A]) => k => if_(f(e), k(e), zr := bool(false)), guard(GRef(zr), shape))
)
Cooked[A](newShape)
}
def mapAccum[Z: Type, B: Type](z: Cde[Z], tr: (Cde[Z] => Cde[A] => (Cde[Z] => Cde[B] => Cde[Unit]) => Cde[Unit]))(
using Quotes
): Cooked[B] = {
def newShape(using raw: Raw): raw.Stream[raw.Cde[B]] =
import raw._
import raw.code._
mkInitVar(
z,
zr =>
mapRaw(
(a: Cde[A]) => k => letl(dref(zr))(z => tr(z)(a)((z2: Cde[Z]) => (b: Cde[B]) => seq(zr := z2, k(b)))),
shape
)
)
Cooked[B](newShape)
}
def drop(n: Cde[Int])(using Quotes): Cooked[A] = {
def newShape(using raw: Raw): raw.Stream[raw.Cde[A]] =
import raw._
import raw.code.{_, given}
mkInitVar(n, z => filterRaw(e => (dref(z) <= int(0)) || seq(decr(z), bool(false)), shape))
Cooked[A](newShape)
}
def dropWhile(f: (Cde[A] => Cde[Boolean]))(using Quotes): Cooked[A] = {
def newShape(using raw: Raw): raw.Stream[raw.Cde[A]] =
import raw._
import raw.code.{_, given}
mkInitVar(bool(false), z => filterRaw((e: Cde[A]) => dref(z) || seq(z := not(f(e)), dref(z)), shape))
Cooked[A](newShape)
}
def fold[W: Type](using raw: Raw)(z: Cde[W], f: ((Cde[W], Cde[A]) => Cde[W]))(using Quotes): Cde[W] = {
import raw._
import raw.code._
letVar(z) { s =>
seq(foldRaw[Cde[A]]((a: Cde[A]) => s := f(dref(s), a), shape(using raw)), dref(s))
}
}
def collect(using raw: Raw)()(using Quotes): Cde[List[A]] = {
import raw._
import raw.code._
this.fold(nil(), (xs, x) => cons(x, xs))
}
}
object Cooked {
// val raw = Raw(Code)
def of[A: Type](arr: Cde[Array[A]])(using Quotes): Cooked[A] = {
def shape(using raw: Raw) =
import raw._
import raw.code.{_, given}
mkInit(
arr,
(arr: Cde[Array[A]]) =>
mkInit(array_len(arr) - int(1), (len: Cde[Int]) => mkPullArray[Cde[A]](len, array_get(arr)))
)
Cooked(shape)
}
def of_static[A: Type: ClassTag](arr: Array[Cde[A]])(using Quotes): Cooked[A] = {
val len = arr.length
def shape(using raw: Raw): raw.Stream[raw.Cde[A]] =
import raw._
import raw.code._
mkInitArr(arr, (arr: Cde[Array[A]]) => mkPullArray[Cde[A]](int(len - 1), array_get(arr)))
Cooked(shape)
}
def of_int_array(using raw: Raw)(arr: Array[Int])(using Quotes): Cooked[Int] = {
import raw.code._
of_static(arr.map(e => int(e)))
}
def of_long_array(using raw: Raw)(arr: Array[Long])(using Quotes): Cooked[Long] = {
import raw.code._
of_static(arr.map(e => long(e)))
}
def iota(n: Cde[Int])(using Quotes): Cooked[Int] = {
def shape(using raw: Raw) =
import raw._
import raw.code.{_, given}
mkInitVar(
n,
z => {
infinite[Cde[Int]]((k: Cde[Int] => Cde[Unit]) => {
letl(dref(z))((v: Cde[Int]) => { seq(z := dref(z) + int(1), k(v)) })
})
}
)
Cooked[Int](shape)
}
def fromTo(a: Cde[Int], b: Cde[Int], step: Int = 1)(using Quotes): Cooked[Int] = {
def shape(using raw: Raw): raw.Stream[raw.Cde[Int]] =
import raw._
import raw.code.{_, given}
if step == 1
then mkPullArray[Cde[Int]](b - a, e => (k: Cde[Int] => Cde[Unit]) => letl(e + a)(k))
else
mkInitVar[Int, Cde[Int]](
a,
z =>
guard[Cde[Int]](
Goon.GExp(if step >= 0 then dref(z) <= b else dref(z) >= b),
infinite(k =>
letl(dref(z))(v =>
seq(
if step == 1 then incr(z)
else if step == -1 then decr(z)
else z := dref(z) + int(step),
k(v)
)
)
)
)
)
Cooked[Int](shape)
}
def zipWith[A: Type, B: Type, C: Type](str1: Cooked[A], str2: Cooked[B], f: (Cde[A], Cde[B]) => Cde[C])(using
Quotes
): Cooked[C] = {
def newShape(using raw: Raw): raw.Stream[raw.Cde[C]] =
import raw._
mapRaw_Direct[(Cde[A], Cde[B]), Cde[C]](p => f(p._1, p._2), zipRaw[Cde[A], Cde[B]](str1.shape, str2.shape))
Cooked[C](newShape)
}
}