Skip to content

Instantly share code, notes, and snippets.

@jdegoes
Last active August 29, 2015 14:01
Show Gist options
  • Save jdegoes/231184fe3f657b706e4a to your computer and use it in GitHub Desktop.
Save jdegoes/231184fe3f657b706e4a to your computer and use it in GitHub Desktop.
scalaz-stream scan
def scan[F[_], A, B](b: B, p: Process[F, A])(f: (B, A) => B): Process[F, B] = {
import Process._
def scan0(b: B, p: Process[F, A]) = scan(b, p)(f)
p match {
case h @ Halt(e) => h
case await : Await[F, Any, A] =>
Process.await[F, Any, B](await.req)(
(a: Any) => scan0(b, await.recv(a)),
scan0(b, await.fallback1),
scan0(b, await.cleanup1)
)
case emit : Emit[F, A] =>
val b2 = emit.head.scanLeft(b)(f)
Emit(b2, scan0(b2.last, emit.tail))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment