Created
June 20, 2017 13:35
-
-
Save 0xYUANTI/a14507b277df97574c87d3f36030d8e0 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
// this seems like a useful FS2 Stream function, is it already built in? | |
// Folds a function into a stream and gives you a stream of the intermediate | |
// accumulator values, i.e.: | |
// > val ones = Stream.iterate(1)(x => x) | |
// > ones.foldIncremental(0)(_ + _) take 3 toList | |
// ==> List[Int] = List(1, 2, 3) | |
implicit class StreamOps[F[_], O](private val self: Stream[F, O]) { | |
def foldIncremental[O2](z: O2)(f: (O2, O) => O2): Stream[F, O2] = | |
self.mapAccumulate(z) { | |
case (z, o) => | |
val o2 = f(z, o) | |
(o2, o2) | |
} map { _._1 } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment