Skip to content

Commit 7a92776

Browse files
committed
Stream: add 'product'
1 parent e38279e commit 7a92776

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

libraries/common/stream.effekt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,12 @@ def iterate[A](initial: A) { step: A => A }: Unit / emit[A] = {
184184
}
185185

186186

187-
def sum { stream : () => Unit / emit[Int] }: Int =
187+
def sum { stream: () => Unit / emit[Int] }: Int =
188188
returning::sum[Unit]{stream}.second
189189

190+
def product { stream: () => Unit / emit[Int] }: Int =
191+
returning::product[Unit]{stream}.second
192+
190193
def collectList[A] { stream: () => Unit / emit[A] }: List[A] =
191194
returning::collectList[A, Unit]{stream}.second
192195

@@ -471,7 +474,7 @@ def limit[A, R](number: Int) { stream: () => R / emit[A] }: R / { emit[A], stop
471474
}
472475
}
473476

474-
def sum[R] { stream : () => R / emit[Int] }: (R, Int) = {
477+
def sum[R] { stream: () => R / emit[Int] }: (R, Int) = {
475478
var s = 0;
476479
try {
477480
(stream(), s)
@@ -481,6 +484,16 @@ def sum[R] { stream : () => R / emit[Int] }: (R, Int) = {
481484
}
482485
}
483486

487+
def product[R] { stream: () => R / emit[Int] }: (R, Int) = {
488+
var s = 1;
489+
try {
490+
(stream(), s)
491+
} with emit[Int] { v =>
492+
s = s * v;
493+
resume(())
494+
}
495+
}
496+
484497
def collectList[A, R] { stream: () => R / emit[A] }: (R, List[A]) =
485498
try {
486499
(stream(), Nil())

0 commit comments

Comments
 (0)