The following for-expression
seems intuitive to me. Take each item in List(1)
, then map over List("a")
, and then return a List[(Int, String)]
.
scala> val x = for {
| a <- List(1)
| b <- List("a")
| } yield (a,b)
x: List[(Int, String)] = List((1,a))
Now, converting it to a flatMap
, it seems less clear to me. If I understand correctly, I need to call flatMap
first since I'm taking the initial List(1)
, and then applying a function to convert from A => List[B]
.
scala> List(1).flatMap(a => List("a").map(b => (a,b) ))
res0: List[(Int, String)] = List((1,a))
After using the flatMap, it seemed necessary to use a map
since I needed to go from A => B
.
But, as the number of items increases in the for-expression
(say 2 to 3 items), how do I know whether to use a map
or flatMap
when converting from for-expression
to flatMap
?