Skip to content

Instantly share code, notes, and snippets.

@0xYUANTI
Created June 20, 2017 13:35
Show Gist options
  • Save 0xYUANTI/a14507b277df97574c87d3f36030d8e0 to your computer and use it in GitHub Desktop.
Save 0xYUANTI/a14507b277df97574c87d3f36030d8e0 to your computer and use it in GitHub Desktop.
// 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