Skip to content

Commit be16938

Browse files
committed
Enable fold
1 parent f75c214 commit be16938

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

tests/pos/staged-streams/staged-streams_1.scala

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,40 @@ object StagedStreams {
4343
case class Nested[A, B](producer: Producer[A], nestedf: A => StagedStream[B]) extends StagedStream[B]
4444

4545
case class Stream[A](stream: StagedStream[Expr[A]]) {
46-
// def fold[W](z: Expr[W], f: (Expr[W] => Expr[A] => Expr[W])): Expr[W] = ???
47-
48-
// def fold_raw[W](z: Expr[W], update_acc: Expr[W] => Expr[Unit], f: (Expr[W] => Expr[A] => Expr[W])): Expr[Unit] = {
49-
// def consume[A](consumer: A => Expr[Unit], stream: StagedStream[A]): Expr[Unit] = {
50-
// stream match {
51-
// case Linear(producer) => {
52-
// producer.card match {
53-
// case Many =>
54-
// producer.init(sp => '{
55-
// while(~producer.hasNext(sp)) {
56-
// ~producer.step(sp, consumer)
57-
// }
58-
// })
59-
// case AtMost1 =>
60-
// producer.init(sp => '{
61-
// if (~producer.hasNext(sp)) {
62-
// ~producer.step(sp, consumer)
63-
// }
64-
// })
65-
// }
66-
// }
67-
// case Nested(producer, nestedf) => {
68-
// ??? //consume(((a) => consume(consumer, nestedf(a))), Linear[A](producer))
69-
// }
70-
// }
71-
// }
72-
73-
// ??? // consume((a: Expr[A]) => '{ ~update_acc(f(z)(a)) }, stream)
74-
// }
46+
def fold[W](z: Expr[W], f: ((Expr[W], Expr[A]) => Expr[W])): Expr[W] = {
47+
Var('{z}) { s =>
48+
~fold_raw((a: Expr[A]) => '{
49+
s.update(f(~s.get, a))
50+
s.get
51+
}, stream)
52+
acc
53+
}
54+
}
55+
56+
def fold_raw[W](consumer: A => Expr[Unit], stream: StagedStream[A]): Expr[Unit] = {
57+
stream match {
58+
case Linear(producer) => {
59+
producer.card match {
60+
case Many =>
61+
producer.init(sp => '{
62+
while(~producer.hasNext(sp)) {
63+
~producer.step(sp, consumer)
64+
}
65+
})
66+
case AtMost1 =>
67+
producer.init(sp => '{
68+
if (~producer.hasNext(sp)) {
69+
~producer.step(sp, consumer)
70+
}
71+
})
72+
}
73+
}
74+
case _ => ???
75+
// Nested(producer, nestedf) => {
76+
// ??? //consume(((a) => consume(consumer, nestedf(a))), Linear[A](producer))
77+
// }
78+
}
79+
}
7580
}
7681

7782
object Stream {

tests/pos/staged-streams/staged-streams_2.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,13 @@ import scala.quoted._
55

66
object Test {
77

8-
def test1() = StagedStreams.Stream.of('{Array(1,2,3)})
8+
9+
def test1() = StagedStreams.Stream
10+
.of('{Array(1,2,3)})
11+
.fold('{0}, ((a: Expr[Int])=> (b : Expr[Int]) => '{ ~a + ~b }))
12+
13+
14+
def main(args: Array[String]): Unit = {
15+
println(test1().show)
16+
}
917
}

0 commit comments

Comments
 (0)