Skip to content

Instantly share code, notes, and snippets.

@oreillyross
Created December 9, 2015 13:48
Show Gist options
  • Save oreillyross/12a607972f3192e6a754 to your computer and use it in GitHub Desktop.
Save oreillyross/12a607972f3192e6a754 to your computer and use it in GitHub Desktop.
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