Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created December 28, 2010 16:28
Show Gist options
  • Save debasishg/757389 to your computer and use it in GitHub Desktop.
Save debasishg/757389 to your computer and use it in GitHub Desktop.
Monads .. again
val first = List(1, 2)
val next = List(8, 9)
for {
i <- first
j <- next
}
yield(i * j)
// gets converted to
first flatMap {
f => next map {
n => f * n
}
}
// List is a monad
// The 2 statements in the for comprehension act as generators for the lists
// Each pair generated is sequenced through the flatMap (which is Scala's bind) to generate the product
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment