Last active
August 29, 2015 14:01
-
-
Save jdegoes/231184fe3f657b706e4a to your computer and use it in GitHub Desktop.
scalaz-stream scan
This file contains hidden or 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
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