scala> List(List(1), List(2), List(3), List(4))
res18: List[List[Int]] = List(List(1), List(2), List(3), List(4))
scala> res18.flatten
res19: List[Int] = List
Conceptually, there is no difference. Practically, flatten
is more efficient, and conveys a clearer intent.
Generally, you don't use identity
directly. It's more there for situations like it getting passed in as a parameter, or being set as a default. It's possible for the compiler to optimize it out, but you're risking a superfluous function call for every element.
You would use flatMap
when you need to do a map
(with a function other than identity
) immediately followed by a flatten
.