Skip to content

Instantly share code, notes, and snippets.

@maizy
Last active April 24, 2017 16:17
Show Gist options
  • Save maizy/1d10545cc599a25043a0254987848faa to your computer and use it in GitHub Desktop.
Save maizy/1d10545cc599a25043a0254987848faa to your computer and use it in GitHub Desktop.
import scala.annotation.tailrec
@tailrec
def nextStep(
iter: Iterator[Int], someAcc: List[Int]): (Stream[Int], Iterator[Int], List[Int]) = {
if (iter.hasNext) {
val i = iter.next()
// emulate some window function computation
val newAcc = (i / 2) :: someAcc
val windowValue = newAcc.sum
// skip some items before a window function match some condition
if (windowValue > 10000) {
(Stream(i), iter, List.empty)
} else {
nextStep(iter, newAcc)
}
} else {
(Stream.empty, iter, someAcc)
}
}
def mkStream(seq: Iterator[Int], someAcc: List[Int]): Stream[Int] = {
val (res, tail, newAcc) = nextStep(seq, someAcc)
res append mkStream(tail, newAcc)
}
val iterator = (1 to 10000000).toIterator
println(mkStream(iterator, List.empty).take(100).toList.mkString(","))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment