Created
January 18, 2013 02:14
-
-
Save meetumaltiar/4561789 to your computer and use it in GitHub Desktop.
Composing Futures
This file contains 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 akka.dispatch.Future | |
import akka.actor.ActorSystem | |
object FutureMonadic extends App { | |
implicit val system = ActorSystem("future") | |
val startTime = System.currentTimeMillis | |
// we construct List[Future[Int]] | |
val futureList = List(1, 2, 3) map { case number => Future(timeTakingIdentityFunction(number)) } | |
val future = Future.sequence(futureList) | |
future onSuccess { | |
case results => | |
val elapsedTime = ((System.currentTimeMillis - startTime) / 1000.0) | |
println("Sum of 1, 2 and 3 is " + results.sum + " calculated in " + elapsedTime + " seconds") | |
} | |
def timeTakingIdentityFunction(number: Int) = { | |
Thread.sleep(3000) | |
number | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment