Swift中flatMap和compactMap之间的区别 [英] Difference between flatMap and compactMap in Swift

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

问题描述

Swift 4.1 中似乎已弃用flatMap.但是, Swift 4.1 compactMap中有一种新方法正在执行相同的操作? 使用flatMap,您可以转换集合中的每个对象,然后删除所有为零的项目.
就像flatMap一样

It seems like in Swift 4.1 flatMap is deprecated. However there is a new method in Swift 4.1 compactMap which is doing the same thing? With flatMap you can transform each object in a collection, then remove any items that were nil.
Like flatMap

let array = ["1", "2", nil] 
array.flatMap { $0 } // will return "1", "2"

类似于compactMap

let array = ["1", "2", nil] 
array.compactMap { $0 } // will return "1", "2"

compactMap在做同样的事情.

这两种方法有什么区别?苹果为何决定重命名该方法?

What are the differences between these 2 methods? Why did Apple decide to rename the method?

推荐答案

Swift标准库为flatMap函数定义了3个重载:

The Swift standard library defines 3 overloads for flatMap function:

Sequence.flatMap<S>(_: (Element) -> S) -> [S.Element]  
Optional.flatMap<U>(_: (Wrapped) -> U?) -> U?  
Sequence.flatMap<U>(_: (Element) -> U?) -> [U]  

可以通过两种方式滥用最后一个重载函数:
考虑以下结构和数组:

The last overload function can be misused in two ways:
Consider following struct and array:

struct Person {
  var age: Int
  var name: String
}  

let people = [Person(age: 21, name: "Osame"), Person(age: 17, name: "Masoud"), Person(age: 20, name: "Mehdi")]  

第一种方法:其他包装和展开:
如果需要获取people数组中包含的人员年龄的数组,则可以使用以下两个函数:

First Way: Additional Wrapping and Unwrapping:
If you needed to get an array of ages of persons included in people array you could use two functions :

let flatMappedAges = people.flatMap({$0.age})  // prints: [21, 17, 20]
let mappedAges = people.map({$0.age})  // prints: [21, 17, 20]  

在这种情况下,map函数将完成此任务,并且无需使用flatMap,因为两者都会产生相同的结果.此外,在flatMap用例内部有一个无用的包装和解包过程.(closure参数将其返回值与Optional一起包装,flatMap的实现在返回Optional值之前先对其进行拆包)

In this case the map function will do the job and there is no need to use flatMap, because both produce the same result. Besides, there is a useless wrapping and unwrapping process inside this use case of flatMap.(The closure parameter wraps its returned value with an Optional and the implementation of flatMap unwraps the Optional value before returning it)

第二种方式-符合收集协议的字符串:
认为您需要从people数组中获取人员姓名列表.您可以使用以下行:

Second Way - String conformance to Collection Protocol:
Think you need to get a list of persons' name from people array. You could use the following line :

let names = people.flatMap({$0.name})  

如果您使用的是4.0之前的快速版本,则将获得

If you were using a swift version prior to 4.0 you would get a transformed list of

["Osame", "Masoud", "Mehdi"]  

,但是在较新的版本中,String符合Collection协议,因此,您使用flatMap()会匹配第一个重载函数,而不是第三个重载函数,并且会为您转换后的值提供平坦的结果:

but in newer versions String conforms to Collection protocol, So, your usage of flatMap() would match the first overload function instead of the third one and would give you a flattened result of your transformed values:

["O", "s", "a", "m", "e", "M", "a", "s", "o", "u", "d", "M", "e", "h", "d", "i"]

结论:他们弃用了flatMap()的第三次重载
由于存在这些误用,Swift团队决定将第三个重载弃用到flatMap函数.对于到目前为止您需要处理Optional的情况,他们的解决方案是引入一个名为compactMap()的新函数,它将为您提供预期的结果.

Conclusion: They deprecated third overload of flatMap()
Because of these misuses, swift team has decided to deprecate the third overload to flatMap function. And their solution to the case where you need to to deal with Optionals so far was to introduce a new function called compactMap() which will give you the expected result.

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

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