Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Responder._
- import scalaz._
- object ResponderDemo extends Application {
- import Scalaz._
- // --------------------------------------------------------------------------
- // Uses Responder (a continuation monad) to compose asynchronous functions.
- // --------------------------------------------------------------------------
- // asynchronously concat s1 and s2 and call k with result
- def concat(s1: String, s2: String, k: String => Unit): Unit = new Thread() {
- override def run {
- Thread sleep 1000
- k(s1 + s2)
- }
- }.start
- // concrete responder passing a concatenation result to continuation k
- class ConcatResponder(s1: String, s2: String) extends Responder[String] {
- def respond(k: (String) => Unit) = concat(s1, s2, s => k(s))
- }
- // Creates a Kleisli instance that appends s2 when applied to input String s1
- def append(s2: String, f: String => String = s => s): Kleisli[Responder, String, String] =
- kleisli((s1: String) => new ConcatResponder(s1, s2).map(f))
- // Kleisli composition of asynchronous functions
- val c1 = append("b") >=> append("c") >=> append("d") >=> append("e")
- // ... or usage in a for-comprehension
- def c2(s: String) = for {
- s1 <- append("x")(s)
- s2 <- append("y")(s1)
- s3 <- append("z")(s2)
- } yield s3
- // applications of c1 and c2 run concurrently
- c1("a") respond println // prints "abcde" after approx. 4 seconds
- c2("a") respond println // prints "axyz" after approx. 3 seconds
- }
Add Comment
Please, Sign In to add comment