Skip to content

Instantly share code, notes, and snippets.

@trane
Created September 3, 2015 22:43
Show Gist options
  • Save trane/248bde43d840776c53a4 to your computer and use it in GitHub Desktop.
Save trane/248bde43d840776c53a4 to your computer and use it in GitHub Desktop.
val a = Some(1)
val b = Some(2)
val c = Some(3)
for {
x <- a
y <- b
z <- c
} yield x + y + z // Some(6)
a.flatMap(x => // A => Option[B]
b.flatMap(y => // B => Option[C]
c.map(z => // C => D
x + y + z
)
)
) // Option[B] Some(6)
a.map(x => // A => B A => Option[_]
b.map(y =>
c.map(z =>
x + y + z)
)
) // Option[Option[Option[B]]] Some(Some(Some(6)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment