Skip to content

Instantly share code, notes, and snippets.

@krasserm
Created December 29, 2010 17:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krasserm/758761 to your computer and use it in GitHub Desktop.
Save krasserm/758761 to your computer and use it in GitHub Desktop.
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment