@@ -54,7 +54,7 @@ object Test {
54
54
}
55
55
}
56
56
57
- def fold_raw [A ](consumer : A => Expr [Unit ], s : StagedStream [A ]): Expr [Unit ] = {
57
+ private def fold_raw [A ](consumer : A => Expr [Unit ], s : StagedStream [A ]): Expr [Unit ] = {
58
58
s match {
59
59
case Linear (producer) => {
60
60
producer.card match {
@@ -82,7 +82,7 @@ object Test {
82
82
Stream (mapRaw[Expr [A ], Expr [B ]](a => k => ' { ~ k(f(a)) }, stream))
83
83
}
84
84
85
- def mapRaw [A , B ](f : (A => (B => Expr [Unit ]) => Expr [Unit ]), s : StagedStream [A ]): StagedStream [B ] = {
85
+ private def mapRaw [A , B ](f : (A => (B => Expr [Unit ]) => Expr [Unit ]), s : StagedStream [A ]): StagedStream [B ] = {
86
86
s match {
87
87
case Linear (producer) => {
88
88
val prod = new Producer [B ] {
@@ -116,7 +116,7 @@ object Test {
116
116
Stream (flatMapRaw[Expr [A ], Expr [B ]]((a => { val Stream (nested) = f(a); nested }), stream))
117
117
}
118
118
119
- def flatMapRaw [A , B ](f : (A => StagedStream [B ]), stream : StagedStream [A ]): StagedStream [B ] = {
119
+ private def flatMapRaw [A , B ](f : (A => StagedStream [B ]), stream : StagedStream [A ]): StagedStream [B ] = {
120
120
stream match {
121
121
case Linear (producer) => Nested (producer, f)
122
122
case nested : Nested [a, bt] =>
@@ -142,6 +142,79 @@ object Test {
142
142
143
143
Stream (flatMapRaw[Expr [A ], Expr [A ]]((a => { Linear (filterStream(a)) }), stream))
144
144
}
145
+
146
+ // def moreTermination[A](f: Rep[Boolean] => Rep[Boolean], stream: StagedStream[A]): StagedStream[A] = {
147
+ // def addToProducer[A](f: Rep[Boolean] => Rep[Boolean], producer: Producer[A]): Producer[A] = {
148
+ // producer.card match {
149
+ // case Many =>
150
+ // new Producer[A] {
151
+ // type St = producer.St
152
+
153
+ // val card = producer.card
154
+ // def init(k: St => Rep[Unit]): Rep[Unit] =
155
+ // producer.init(k)
156
+ // def step(st: St, k: (A => Rep[Unit])): Rep[Unit] =
157
+ // producer.step(st, el => k(el))
158
+ // def hasNext(st: St): Rep[Boolean] =
159
+ // f(producer.hasNext(st))
160
+ // }
161
+ // case AtMost1 => producer
162
+ // }
163
+ // }
164
+ // stream match {
165
+ // case Linear(producer) => Linear(addToProducer(f, producer))
166
+ // case Nested(producer, nestedf) =>
167
+ // Nested(addToProducer(f, producer), (a: Id[_]) => moreTermination(f, nestedf(a)))
168
+ // }
169
+ // }
170
+
171
+ // private def addCounter[A](n: Expr[Int], producer: Producer[A]): Producer[(Expr[Int], A)] =
172
+ // new Producer[(Var[Int], A)] {
173
+ // type St = (Var[Int], producer.St)
174
+
175
+ // val card = producer.card
176
+ // def init(k: St => Rep[Unit]): Rep[Unit] = {
177
+ // producer.init(st => {
178
+ // var counter: Var[Int] = n
179
+ // k(counter, st)
180
+ // })
181
+ // }
182
+ // def step(st: St, k: (((Var[Int], A)) => Rep[Unit])): Rep[Unit] = {
183
+ // val (counter, nst) = st
184
+ // producer.step(nst, el => {
185
+ // k((counter, el))
186
+ // })
187
+ // }
188
+ // def hasNext(st: St): Rep[Boolean] = {
189
+ // val (counter, nst) = st
190
+ // producer.card match {
191
+ // case Many => counter > 0 && producer.hasNext(nst)
192
+ // case AtMost1 => producer.hasNext(nst)
193
+ // }
194
+ // }
195
+ // }
196
+
197
+ // def takeRaw[A](n: Rep[Int], stream: StagedStream[A]): StagedStream[A] = {
198
+ // stream match {
199
+ // case Linear(producer) => {
200
+ // mapRaw[(Var[Int], A), A]((t => k => {
201
+ // t._1 = t._1 - 1
202
+ // k(t._2)
203
+ // }), Linear(addCounter(n, producer)))
204
+ // }
205
+ // case Nested(producer, nestedf) => {
206
+ // Nested(addCounter(n, producer), (t: (Var[Int], Id[_])) => {
207
+ // mapRaw[A, A]((el => k => {
208
+ // t._1 = t._1 - 1
209
+ // k(el)
210
+ // }), moreTermination(b => t._1 > 0 && b, nestedf(t._2)))
211
+ // })
212
+ // }
213
+ // }
214
+ // }
215
+
216
+ // def take(n: Rep[Int]): Stream[A] = Stream(takeRaw(n, stream))
217
+
145
218
}
146
219
147
220
object Stream {
0 commit comments