Scala Iterable#map vs. Iterable#flatMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1059776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:29:34  来源:igfitidea点击:

scala Iterable#map vs. Iterable#flatMap

scalamonadsscala-collections

提问by Landon Kuhn

What is the difference between the mapand flatMapfunctions of Iterable?

map和 的flatMap功能有Iterable什么区别?

采纳答案by agilefall

Here is a pretty good explanation:

这是一个很好的解释:

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

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 是

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]

所以 flatMap 接受一个类型 [A] 并返回一个可迭代类型 [B] 并且 map 接受一个类型 [A] 并返回一个类型 [B]

This will also give you an idea that flatmap will "flatten" lists.

这也会让您知道 flatmap 将“展平”列表。

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)

回答by kikibobo

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

以上都是正确的,但还有一件很方便的事情: flatMap将 aList[Option[A]]变成List[A],并删除任何Option向下钻取到 的None。这是超越使用null.

回答by Daniel C. Sobral

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.

for 中的每个迭代器都将被转换为“flatMap”,除了最后一个被转换为“map”。这样,您将返回一个平面集合,而不是返回嵌套集合(一个包含 blah、blah、blah 缓冲区数组的列表)。由被产生的元素形成的集合——在这种情况下是整数列表。

回答by skaffman

From scaladoc:

Scaladoc

  • map
  • 地图

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

返回将给定函数 f 应用于此可迭代对象的每个元素所产生的可迭代对象。

  • flatMap
  • 平面图

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

将给定的函数 f 应用于此可迭代对象的每个元素,然后连接结果。

回答by skaffman

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

看这里: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).

“搜索 flatMap” - 那里有一个很好的解释。(基本上它是“扁平化”和“映射”的组合——来自其他语言的特征)。