Created
December 9, 2015 13:48
-
-
Save oreillyross/12a607972f3192e6a754 to your computer and use it in GitHub Desktop.
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 scala.concurrent.{ Await, Future } | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration.DurationInt | |
def f(n: Int): Future[Int] = Future(n + 1) | |
def g(n: Int): Future[Int] = Future(n - 1) | |
def h(n: Int): Future[Int] = Future(n * 2) | |
val n = f(42).flatMap(g) | |
assert(Await.result(n, 1.second) == 42) | |
val m = for { | |
m1 <- f(42) | |
m2 <- g(m1) | |
m3 <- h(m2) | |
} yield m3 | |
assert(Await.result(m, 1.second) == 84) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment