scala Iterable#map vs. Iterable#flatMap
monads, scala, scala-collections
Solution
Here is a pretty good explanation:
http://www.codecommit.com/blog/scala/scala-collections-for-the-easily-bored-part-2
Using list as an example:
Map's signature is:
map [B](f : (A) => B) : List[B]
and flatMap's is
flatMap [B](f : (A) => Iterable[B]) : List[B]
So flatMap takes a type [A] and returns an iterable type [B] and map takes a type [A] and returns a type [B]
This will also give you an idea that flatmap will "flatten" lists.
val l = List(List(1,2,3), List(2,3,4))
println(l.map(_.toString)) // changes type from list to string
// prints List(List(1, 2, 3), List(2, 3, 4))
println(l.flatMap(x => x)) // "changes" type list to iterable
// prints List(1, 2, 3, 2, 3, 4)
Problem
What is the difference between the `map` and `flatMap` functions of `Iterable`?