Guest User

Untitled

a guest
Jul 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. val first = List(1, 2)
  2. val next = List(8, 9)
  3.  
  4. for {
  5. i <- first
  6. j <- next
  7. }
  8. yield(i * j)
  9.  
  10. // gets converted to
  11.  
  12. first flatMap {
  13. f => next map {
  14. n => f * n
  15. }
  16. }
  17.  
  18. // List is a monad
  19. // The 2 statements in the for comprehension act as generators for the lists
  20. // Each pair generated is sequenced through the flatMap (which is Scala's bind) to generate the product
Add Comment
Please, Sign In to add comment