深入浅出Swift高阶函数-Map、Filter、Reduce

在 Swift 中,函数是做为一等公民的存在,因此学习高阶函数仍是很是有必要的,它可使你的代码扩展性更高,代码更 Swift 范。

初探高阶函数

高阶函数一共有下面几个:

  • map:对给定数组每一个元素,执行闭包中的映射,将映射结果放置在数组中返回。
  • flatMap:对给定数组的每一个元素,执行闭包中的映射,对映射结果进行合并操做,而后将合并操做后的结果放置在数组中返回。
  • compactMap:对给定数组的每一个元素,执行闭包中的映射,将非空的映射结果放置在数组中返回。
  • filter:对给定数组的每一个元素,执行闭包中的操做,将符合条件的元素放在数组中返回。
  • reduce:对给定数组的每一个元素,执行闭包中的操做对元素进行合并,并将合并结果返回。

1、map

map方法获取一个闭包表达式作为其唯一参数。

数组中的每一个元素调用一次该闭包函数,并返回该元素所映射的值。

对于 map 函数,使用场景就是将数组的类型映射为别的类型。好比,咱们有一个模型数组,模型的 id 字段咱们从服务器拿的是 String 类型,在某种场景下咱们须要转为 Int 类型,这时候咱们就能够经过 map 函数来实现该需求。bash

struct Student {
    let id: String
    let name: String
    let age: Int
}

let stu1 = Student(id: "1001", name: "stu1", age: 12)
let stu2 = Student(id: "1002", name: "stu2", age: 14)
let stu3 = Student(id: "1003", name: "stu3", age: 16)
let stu4 = Student(id: "1004", name: "stu4", age: 20)
let stus = [stu1, stu2, stu3, stu4]

let intIds = stus.map { (stu) in
    Int(stu.id)
}

print(intIds) //map得到的是一个可选类型的数组 [Optional(1001), Optional(1002), Optional(1003), Optional(1004)]

上述代码将 id 字段从 String 映射为了 Int? 类型,这并非 Int 类型。若是访问元素的话还得须要解包,那么如何既能将元素映射又能自动筛选 nil 的值呢?这时,就轮到 compactmap 出马了。

optional 也可使用map函数闭包

var num: Int? = 2
let result = num.map {
    $0 * 2
}

print(result) // Optional(4)

// ----而不是使用下面的代码
var result: Int?

if let n = num {
    result = n * 2
} else {
    result = nil
}

2、compactMap

扫描二维码关注公众号,回复: 16284254 查看本文章

咱们将上面的代码替换为:app

let intIds = stus.compactMap { (stu) in
    Int(stu.id)
}

这时再打印 intIds 就会发现它已经为 Int 类型了

compactMapValues

对于 Set 和 Array ,你可使用 compactMap 来得到非空的集合,可是对于 Dictionary 来讲,这个函数是不起做用的。函数

let dict = ["key1": 10, "key2": nil]
let result = dict.compactMap { $0 }
print(result) //[(key: "key1", value: Optional(10)), (key: "key2", value: nil)]

这时候,需要使用 compactMapValues 函数来得到非空的字典。学习

let dict = ["key1": 10, "key2": nil]
let result = dict.compactMapValues { $0 }
print(result) //["key1": 10]

3、flatMap

对于 flatMap,主要的应用场景就是你想得到一个单层集合的数组。经过下面的代码来看一下 map 和 flapMap 的区别。

let scoresByName = ["Henk": [0, 5, 8], "John": [2, 5, 8]]

let mapped = scoresByName.map { $0.value }
// [[2, 5, 8], [0, 5, 8]]
print(mapped)

let flatMapped = scoresByName.flatMap { $0.value }
// [2, 5, 8, 0, 5, 8]

map 会直接将元素放在数组中,而 flatMap 会将元素平铺在一个数组中。实际上,s.flatMap(transform) 等同于s.map(transform).joined()

4、filter

这个函数就如同单词的意思:查找。将符合条件的元素查找出来放置在数组中返回。好比想查找年龄大于18岁的全部学生。

let adults = stus.filter { (stu) -> Bool in
    stu.age >= 18
}
print(adults) // 数组中只包含stu4 学生
 统计集合中某个元素的个数、
let array = ["A", "A", "B", "A", "C"]

// 1.
var count = 0
for value in array {
    if value == "A" {
        count += 1
    }
}

// 2.
count = 0
for value in array where value == "A" {
    count += 1
}

// 3.
count = array.filter { $0 == "A" }.count

我们知道获取集合的总个数用.count就可以。为了项目中代码更加统一和可维护性,与其有上面那么多种写法,不如也定义个类似的扩展:count(where:)

extension Sequence where Element: Equatable {
    func count(where isIncluded: (Element) -> Bool) -> Int {
        self.filter(isIncluded).count
    }
}

我们直接给 Sequence 添加扩展,这样格局就又打开了,像 ArraySlice,也支持。

["A", "A", "B"].count(where: { $0 == "A" }) // 2

["B", "A", "B"].dropLast(1) // --> ArraySlice<String>
.count(where: { $0 == "B" }) // 1
5、reduce

对于 reduce,使用场景就是对数组中的元素进行组合运算,好比咱们想计算全部学生的年龄加载一块儿是多少。

let totalAges = stus.reduce(0) { (result, stu) in
    return result + stu.age
}

print(totalAges) // 62

该函数的第一个参数为初始值,后面元组中的第一个参数为每次计算的结果,第二个参数为每次遍历的元素。最后将计算的结果返回。

6、组合使用

对于使用高阶函数最大的好处就是能够进行函数式编程了。下面咱们经过几个小栗子来对这几个高阶函数进行组合使用。

将 String 类型映射为 Int 类型,并查找id大于1002的全部学生

let adults = stus.compactMap { (stu) in
    Int(stu.id)
    }.filter { (id) -> Bool in
        id > 1002
}

print(adults) //[1003, 1004]

计算年龄大于12的全部学生年龄总和

let totalAge = stus.filter { (stu) -> Bool in
    stu.age > 12
    }.reduce(0) { (result, stu) in
        return result + stu.age
}

print(totalAge) // 50

7、去除集合中指定的重复的对象

日常开发中,我想会遇到这样的需求,从网络请求数据得到一个数组,现在需要删除其中重复的元素,同时保证原有的顺序。先看第一种方法:

  • 第一种,内部元素是可哈希的
extension Sequence where Element: Hashable {
    func uniqued() -> [Element] {
        var seen = Set<Element>()
        return filter { seen.insert($0).inserted }
    }
}

[ 1, 2, 3, 1, 2 ].uniqued()   // [ 1, 2, 3 ]

内部使用了 Set 进行 contains 查找,比数组的 contains (_:) 方法时间复杂度要低。

  • 第二种,内部元素不是可哈希的

如果数组内部的元素是解析的 model,而且没有遵守 Hashable,上面的方法就用不了了。

extension Sequence {
    func uniqued(comparator: (Element, Element) -> Bool) -> [Element] {
        var result: [Element] = []
        for element in self {
            if result.contains(where: {comparator(element, $0)}) {
                continue
            }
            result.append(element)
        }
        return result
    }
}

let article1 = ArticleModel(title: "111", content: "aa", articleID: "11111", comments: [])
let article2 = ArticleModel(title: "222", content: "aaa", articleID: "22222", comments: [])
let article3 = ArticleModel(title: "111", content: "aaaa", articleID: "33333", comments: [])
let article4 = ArticleModel(title: "333", content: "aaaaa", articleID: "44444", comments: [])
let articles = [article1, article2, article3, article4]

let newarticles = articles.uniqued(comparator: {$0.title == $1.title})
print(newarticles)    //结果 article3 会被删除
  • 第三种,keypath 版本

最后再来个 keypath 版本,不过是对第二种的一层封装,内部依然调用的是第二种

extension Sequence {
    func uniqued<T: Equatable>(_ keyPath: KeyPath<Element, T>) -> [Element] {
        uniqued { $0[keyPath: keyPath] == $1[keyPath: keyPath] }
    }
}

let newarticles = articles.uniqued(\.title)

总结

  • 使用函数式编程不只能减小代码的行数,还可以使用链式结构构建复杂的逻辑。
  • 当你须要映射一个数组,而且不须要改变返回数组的层级结构的时候,使用 map ,反之,则使用 flatMap 。
  • 当返回数组中的值必须非空的时候,使用 compactMap ;当返回字典中的键值对中的value 必须为非空的时候,使用 compactMapValues 。
  • 当你须要查询的时候,使用 filter 。
  • 当你须要将数组进行某种计算并返回一个值得时候,使用 reduce 。

猜你喜欢

转载自blog.csdn.net/RreamigOfGirls/article/details/127072873
今日推荐