scala Iterable#map vs. Iterable#flatMap

ScalaMonadsScala Collections

Scala Problem Overview


What is the difference between the map and flatMap functions of Iterable?

Scala Solutions


Solution 1 - Scala

The above is all true, but there is one more thing that is handy: flatMap turns a List[Option[A]] into List[A], with any Option that drills down to None, removed. This is a key conceptual breakthrough for getting beyond using null.

Solution 2 - Scala

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)

Solution 3 - Scala

From scaladoc:

  • map

> Returns the iterable resulting > from applying the given function f to > each element of this iterable.

  • flatMap

> Applies the given function f > to each element of this iterable, then > concatenates the results.

Solution 4 - Scala

lines.map(line => line split "\\W+") // will return a list of arrays of words
lines.flatMap(line => line split "\\W+") // will return a list of words

You can see this better in for comprehensions:

for {line <- lines
     word <- line split "\\W+"}
yield word.length

this translates into:

lines.flatMap(line => line.split("\\W+").map(word => word.length))

Each iterator inside for will be translated into a "flatMap", except the last one, which gets translated into a "map". This way, instead of returning nested collections (a list of an array of a buffer of blah, blah, blah), you return a flat collection. A collection formed by the elements being yield'ed -- a list of Integers, in this case.

Solution 5 - Scala

Look here: http://www.codecommit.com/blog/scala/scala-collections-for-the-easily-bored-part-2

"Search for flatMap" - there is a really good explanation of it there. (Basically it is a combination of "flatten" and "map" -- features from other languages).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLandon KuhnView Question on Stackoverflow
Solution 1 - ScalakikiboboView Answer on Stackoverflow
Solution 2 - ScalaagilefallView Answer on Stackoverflow
Solution 3 - ScalaskaffmanView Answer on Stackoverflow
Solution 4 - ScalaDaniel C. SobralView Answer on Stackoverflow
Solution 5 - ScalapuppetluvaView Answer on Stackoverflow