Skip to content

Instantly share code, notes, and snippets.

@tjheslin1
Last active November 18, 2017 12:30
Show Gist options
  • Save tjheslin1/147c5eab62d88c8184cd661b77a48858 to your computer and use it in GitHub Desktop.
Save tjheslin1/147c5eab62d88c8184cd661b77a48858 to your computer and use it in GitHub Desktop.
// Taken from Essential Scala by Noel Welsh and Dave Gurnell
for {
a <- getFirstNumber // getFirstNumber returns Option[Int]
b <- getSecondNumber // getSecondNumber returns Option[Int]
} yield a + b
// The final result is an Option[Int]---the result of
// applying `+` to `a` and `b` if both values are present
for {
a <- getFirstNumbers // getFirstNumbers returns Seq[Int]
b <- getSecondNumbers // getSecondNumbers returns Seq[Int]
} yield a + b
// The final result is a Seq[Int]---the results of
// applying `+` to all combinations of `a` and `b`
for {
a <- getFirstNumber // getFirstNumber returns Future[Int]
b <- getSecondNumber // getSecondNumber returns Future[Int]
} yield a + b
// The final result is a Future[Int]---a data structure
// that will eventually allow us to access the result of
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment