Skip to content

Commit ff68092

Browse files
committed
Implement fold
1 parent be16938 commit ff68092

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

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

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6

tests/pos/staged-streams/staged-streams_1.scala renamed to tests/run-with-compiler/staged-streams_1.scala

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
package streams
2-
31
import dotty.tools.dotc.quoted.Toolbox._
42
import scala.quoted._
53

6-
object StagedStreams {
4+
object Test {
75

86
// TODO: remove as it exists in Quoted Lib
97
sealed trait Var[T] {
@@ -40,21 +38,24 @@ object StagedStreams {
4038

4139
trait StagedStream[A]
4240
case class Linear[A](producer: Producer[A]) extends StagedStream[A]
43-
case class Nested[A, B](producer: Producer[A], nestedf: A => StagedStream[B]) extends StagedStream[B]
41+
case class Nested[A, B](producer: Producer[B], nestedf: B => StagedStream[A]) extends StagedStream[A]
4442

4543
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-
Var('{z}) { s =>
48-
~fold_raw((a: Expr[A]) => '{
49-
s.update(f(~s.get, a))
50-
s.get
51-
}, stream)
52-
acc
44+
45+
def fold[W: Type](z: Expr[W], f: ((Expr[W], Expr[A]) => Expr[W])): Expr[W] = {
46+
Var(z) { s: Var[W] => '{
47+
48+
~fold_raw[Expr[A]]((a: Expr[A]) => '{
49+
~s.update(f(s.get, a))
50+
}, stream)
51+
52+
~s.get
53+
}
5354
}
5455
}
5556

56-
def fold_raw[W](consumer: A => Expr[Unit], stream: StagedStream[A]): Expr[Unit] = {
57-
stream match {
57+
def fold_raw[A](consumer: A => Expr[Unit], s: StagedStream[A]): Expr[Unit] = {
58+
s match {
5859
case Linear(producer) => {
5960
producer.card match {
6061
case Many =>
@@ -71,10 +72,10 @@ object StagedStreams {
7172
})
7273
}
7374
}
74-
case _ => ???
75-
// Nested(producer, nestedf) => {
76-
// ??? //consume(((a) => consume(consumer, nestedf(a))), Linear[A](producer))
77-
// }
75+
case nested: Nested[a, bt] => ???
76+
// {
77+
// fold_raw[bt](((e: bt) => fold_raw(consumer, nested.nestedf(e))), Linear(nested.producer))
78+
// }
7879
}
7980
}
8081
}
@@ -88,7 +89,7 @@ object StagedStreams {
8889

8990
def init(k: St => Expr[Unit]): Expr[Unit] = {
9091
Var('{(~arr).length}) { n =>
91-
Var(0.toExpr){i =>
92+
Var(0.toExpr){ i =>
9293
k((i, n, arr))
9394
}
9495
}
@@ -114,6 +115,15 @@ object StagedStreams {
114115
Stream(Linear(prod))
115116
}
116117
}
118+
119+
def test1() = Stream
120+
.of('{Array(1, 2, 3)})
121+
.fold('{0}, ((a: Expr[Int], b : Expr[Int]) => '{ ~a + ~b }))
122+
123+
124+
def main(args: Array[String]): Unit = {
125+
println(test1().run)
126+
}
117127
}
118128

119129

0 commit comments

Comments
 (0)