Skip to content

Commit 99071e0

Browse files
committed
Implement map
1 parent ff68092 commit 99071e0

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
6
1+
6
2+
3+
12

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,43 @@ object Test {
7878
// }
7979
}
8080
}
81+
82+
83+
def map[B : Type](f: (Expr[A] => Expr[B])): Stream[B] = {
84+
Stream(mapRaw[Expr[A], Expr[B]](a => k => '{ ~k(f(a)) }, stream))
85+
}
86+
87+
def mapRaw[A, B](f: (A => (B => Expr[Unit]) => Expr[Unit]), s: StagedStream[A]): StagedStream[B] = {
88+
s match {
89+
case Linear(producer) => {
90+
val prod = new Producer[B] {
91+
92+
type St = producer.St
93+
94+
val card = producer.card
95+
96+
def init(k: St => Expr[Unit]): Expr[Unit] = {
97+
producer.init(k)
98+
}
99+
100+
def step(st: St, k: (B => Expr[Unit])): Expr[Unit] = {
101+
producer.step(st, el => f(el)(k))
102+
}
103+
104+
def hasNext(st: St): Expr[Boolean] = {
105+
producer.hasNext(st)
106+
}
107+
}
108+
109+
Linear(prod)
110+
}
111+
case nested: Nested[a, bt] => ???
112+
// {
113+
// Nested(nested.producer, (a: bt) => mapRaw[A, B](f, nested.nestedf(a)))
114+
// }
115+
}
116+
}
117+
81118
}
82119

83120
object Stream {
@@ -120,9 +157,15 @@ object Test {
120157
.of('{Array(1, 2, 3)})
121158
.fold('{0}, ((a: Expr[Int], b : Expr[Int]) => '{ ~a + ~b }))
122159

160+
def test2() = Stream
161+
.of('{Array(1, 2, 3)})
162+
.map((a: Expr[Int]) => '{ ~a * 2 })
163+
.fold('{0}, ((a: Expr[Int], b : Expr[Int]) => '{ ~a + ~b }))
123164

124165
def main(args: Array[String]): Unit = {
125166
println(test1().run)
167+
println
168+
println(test2().run)
126169
}
127170
}
128171

0 commit comments

Comments
 (0)