Last active
April 24, 2017 16:17
-
-
Save maizy/1d10545cc599a25043a0254987848faa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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