Created
June 17, 2013 22:26
-
-
Save anonymous/5801051 to your computer and use it in GitHub Desktop.
:; bin/scala -i util/util-app -i finagle/finagle-core jammer.scala Made some tasty jam: cranapple, banana, cherry, durian
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
import com.twitter.finagle.{Filter, Service} | |
import com.twitter.util.{Await, Future} | |
case class Fruit(flavor: String) | |
case class Jam(flavor: String) | |
object Jammer extends Service[Fruit, Jam] { | |
def apply(fruit: Fruit) = | |
Future value Jam(fruit.flavor) | |
} | |
object AppleToCranapple extends Filter[Fruit, Jam, Fruit, Jam] { | |
def apply(fruit: Fruit, jammer: Service[Fruit, Jam]) = { | |
fruit match { | |
case Fruit("apple") => jammer(Fruit("cranapple")) | |
case fruit => jammer(fruit) | |
} | |
} | |
} | |
val fruits = "apple" :: "banana" :: "cherry" :: "durian" :: Nil | |
val jammer = AppleToCranapple andThen Jammer | |
try { | |
val jams = Await result { | |
Future collect { | |
fruits map { Fruit(_) } map { | |
jammer(_) map { _.flavor } | |
} | |
} | |
} | |
println("Made some tasty jam: %s".format(jams.mkString(", "))) | |
} catch { case e => | |
println("Error: %s".format(e.getMessage)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment