在scala中flatten和flatMap [英] flatten and flatMap in scala

查看:239
本文介绍了在scala中flatten和flatMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我是否正确理解了flatten和flatMap函数.

I'll like to check if I have understood flatten and flatMap functions correctly.

1)我是否正确认为拼合仅在一个集合构成其他集合时才有效.例如,flatten将适用于以下列表

1) Am I correct that flatten works only when a collection constitutes of other collections. Eg flatten would work on following lists

//list of lists
val l1 = List(List(1,1,2,-1,3,1,-4,5), List("a","b"))

//list of a set, list and map
val l2 = List(Set(1,2,3), List(4,5,6), Map('a'->"x",'b'->"y"))

但是拼合不能用于关注

val l3 = List(1,2,3)
val l4 = List(1,2,3,List('a','b'))
val s1 = "hello world"
val m1 = Map('h'->"h", 'e'->"e", 'l'->"l",'o'->"0")

'flatten'方法将通过删除层次结构来创建一个由所有元素组成的新列表.因此,它可以对集合进行扁平化"处理,并将所有元素置于同一水平.

'flatten' method would create a new list consisting of all elements by removing the hierarchy. Thus it sort of 'flattens' the collection and brings all elements at the same level.

l1.flatten
res0: List[Any] = List(1, 1, 2, -1, 3, 1, -4, 5, a, b)

l2.flatten
res1: List[Any] = List(1, 2, 3, 1, 5, 6, (a,x), (b,y))

2)'flatMap'首先将方法应用于列表的元素,然后拉平列表.如上所述,如果列表具有层次结构(包含其他集合),则flatten方法有效.因此,很重要的一点是,我们应用于元素的方法必须返回一个集合,否则flatMap将无法工作

2) 'flatMap' first applies a method to elements of a list and then flattens the list. As we noticed above, the flatten method works if lists have a hierarchy (contain other collections). Thus it is important that the method we apply to the elements returns a collection otherwise flatMap will not work

//we have list of lists
val l1 = List(List(1,1,2,-1,3,1,-4,5), List("a","b"))

l1 flatMap(x=>x.toSet)
res2: List[Any] = List(5, 1, -4, 2, 3, -1, a, b)

val l2 = List(Set(1,2,3), List(1,5,6), Map('a'->"x",'b'->"y"))
l2.flatMap(x=>x.toSet)
res3: List[Any] = List(1, 2, 3, 1, 5, 6, (a,x), (b,y))

val s1 = "hello world"
s1.flatMap(x=>Map(x->x.toString)) 

我们在上面注意到s1.flatten不起作用,但s1.flatMap起作用.这是因为,在s1.flatMap中,我们将String(字符)的元素转换为Map,该Map是一个集合.因此,字符串被转换为Maps的集合,例如(Map('h'->"h"),Map('e'->"e"),Map('l'->"l"),Map( 'l'->"l"),Map('o'->"o")....)这样展平现在就可以使用.请注意,创建的地图不是Map('h'->"h",'e'->"e",'l'->"l",....).

We notice above that s1.flatten didn't work but s1.flatMap did. This is because, in s1.flatMap, we convert elements of a String (characters) into a Map which is a collection. Thus the string got converted into a collection of Maps like (Map('h'->"h"), Map('e'->"e"), Map('l'->"l"),Map ('l'->"l"),Map('o'->"o")....) Thus flatten will work now. Note that the Map created is not Map('h'->"h", 'e'->"e", 'l'->"l",....).

推荐答案

看看flatten的完整签名:

def flatten[B](implicit asTraversable: (A) ⇒ GenTraversableOnce[B]): List[B]

如您所见,flatten采用一个隐式参数.该参数提供了有关如何展平给定集合类型的规则.如果编译器找不到范围内的隐式对象,则可以显式提供它.

As you can see, flatten takes an implicit parameter. That parameter provides the rules for how to flatten the given collection types. If the compiler can't find an implicit in scope then it can be provided explicitly.

flatten几乎可以平整任何东西.

flatten can flatten almost anything as long as you provide the rules to do so.

这篇关于在scala中flatten和flatMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆