Skip to content

Commit 83f8ed2

Browse files
committed
Implement filter
1 parent 58e1726 commit 83f8ed2

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

tests/run-with-compiler/staged-streams_1.check

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
12
44

5-
36
5+
36
6+
7+
2

tests/run-with-compiler/staged-streams_1.scala

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ object Test {
7878
}
7979
}
8080

81-
8281
def map[B : Type](f: (Expr[A] => Expr[B])): Stream[B] = {
8382
Stream(mapRaw[Expr[A], Expr[B]](a => k => '{ ~k(f(a)) }, stream))
8483
}
@@ -124,6 +123,25 @@ object Test {
124123
Nested(nested.producer, (a: bt) => flatMapRaw[A, B](f, nested.nestedf(a)))
125124
}
126125
}
126+
127+
def filter(f: (Expr[A] => Expr[Boolean])): Stream[A] = {
128+
val filterStream = (a: Expr[A]) =>
129+
new Producer[Expr[A]] {
130+
type St = Expr[A]
131+
val card = AtMost1
132+
133+
def init(k: St => Expr[Unit]): Expr[Unit] =
134+
k(a)
135+
136+
def step(st: St, k: (Expr[A] => Expr[Unit])): Expr[Unit] =
137+
k(st)
138+
139+
def hasNext(st: St): Expr[Boolean] =
140+
f(st)
141+
}
142+
143+
Stream(flatMapRaw[Expr[A], Expr[A]]((a => { Linear(filterStream(a)) }), stream))
144+
}
127145
}
128146

129147
object Stream {
@@ -176,12 +194,19 @@ object Test {
176194
.flatMap((d: Expr[Int]) => Stream.of('{Array(1, 2, 3)}).map((dp: Expr[Int]) => '{ ~d * ~dp }))
177195
.fold('{0}, ((a: Expr[Int], b : Expr[Int]) => '{ ~a + ~b }))
178196

197+
def test4() = Stream
198+
.of('{Array(1, 2, 3)})
199+
.filter((d: Expr[Int]) => '{ ~d % 2 == 0 })
200+
.fold('{0}, ((a: Expr[Int], b : Expr[Int]) => '{ ~a + ~b }))
201+
179202
def main(args: Array[String]): Unit = {
180203
println(test1().run)
181204
println
182205
println(test2().run)
183206
println
184207
println(test3().run)
208+
println
209+
println(test4().run)
185210
}
186211
}
187212

0 commit comments

Comments
 (0)