swift, Array的高阶函数:flatMap 和 compactMap

96
like_sunflower
2019.01.05 16:22 字数 375
image.png

flatMap: 对数组的每一个元素做一次处理,返回处理后的数组。
与map的区别是:

  1. 返回后的数组中不存在nil, 同时也会把Optional解包。
    函数声明:
    @available(swift, deprecated: 4.1, renamed: "compactMap(:)", message: "Please use compactMap(:) for the case where closure returns an optional value")
    public func flatMap(_ transform: (Element) throws -> String?) rethrows -> [String]
    注意:4.1之后如果闭包中返回的值是可选的话,就要使用compactMap代替flatMap了,不然的话就会警告。
        let arrayAny: [Any?] = [1, 2, 3, 4, 5, nil, "a", 8, "9"]
        let arrayInt = arrayAny.flatMap { (obj) -> Any? in
            if obj is Int {
                return obj
            } else {
                return nil
            }
        }
        print("arrayInt: \(arrayInt)")
        // arrayInt: [1, 2, 3, 4, 5, 8]

详解:
我们并没有强制解包,但是返回元素已不是可选类型,而且没有nil。

       // 简化写法
       let arrayInt2 = arrayAny.flatMap { $0 is Int ? $0 : nil }
       print("arrayInt2: \(arrayInt2)")
       // arrayInt2: [1, 2, 3, 4, 5, 8]
 print("-----------------案例二----------------------")
let arrayString = ["Ann", "Bob", "Tom", "Lily", "HanMeiMei", "Jerry"]
//需求一:计算每个元素的长度
        let arrayCount = arrayString.flatMap { (str) -> Int in
            return str.count
        }
        print("arrayCount: \(arrayCount)")
        // arrayCount: [3, 3, 3, 4, 9, 5]
        
        // 简化
        let arrayCount2 = arrayString.flatMap { $0.count }
        print("arrayCount2: \(arrayCount2)")
        // arrayCount2: [3, 3, 3, 4, 9, 5]
        print("-----------------案例三----------------------")
        let arrayThree = ["Apple", "Orange", "Puple", ""]
        let arrayT = arrayThree.flatMap { (str) -> String? in
            return str.count > 0 ? str : nil
        }
        print("arrayT: \(arrayT)")
        // arrayT: ["Apple", "Orange", "Puple"]
        
        // 简化
        let arrayT2 = arrayThree.flatMap { $0.count > 0 ? $0 : nil }
        print("arrayT2: \(arrayT2)")
        // arrayT2: ["Apple", "Orange", "Puple"]

上面的例子中,我们虽然返回了nil,但是结果中,却是没有nil,也不是可选类型。

2.flatMap还能把多维数组变成一维数组。

        print("-----------------案例四----------------------")
        let arrayA = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        let arr1 = arrayA.map { $0 }
        let arr2 = arrayA.flatMap { $0 }
        print("arr1 = \(arr1)")
        print("arr2 = \(arr2)")
        /*
         结果:
         arr1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
         arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
  1. flatMap也能把两个不同的数组合并成一个数组,这个合并的数组元素个数是前面两个数组元素
    个数的乘积。
        print("-----------------案例五----------------------")
        let fruits = ["Apple", "Orange", "Puple", "Pear"]
        let counts = [2, 3, 6, 1]
        let arrayMerge = counts.flatMap { (count) -> [String] in
            fruits.map({ return $0 + ": \(count)"})
        }
        print("arrayMerge = \(arrayMerge)")
        /*
         返回结果:
         arrayMerge = ["Apple: 2", "Orange: 2", "Puple: 2", "Pear: 2", "Apple: 3", "Orange: 3", "Puple: 3", "Pear: 3", "Apple: 6", "Orange: 6", "Puple: 6", "Pear: 6", "Apple: 1", "Orange: 1", "Puple: 1", "Pear: 1"]
         */

image.png

compactMap与flatMap的区别上面也说了,当闭包中的返回结果是可选的时候,使用compactMap代替flatMap,那么当闭包中的返回结果不是可选的时候,依然使用flatMap。

let arrayString = ["Ann", "Bob", "Tom", "Lily", "HanMeiMei", "Jerry"]
        let arrayInt = arrayString.compactMap { (str) -> Int? in
            return str.count
        }
        print("arrayInt: \(arrayInt)")
        // arrayInt: [3, 3, 3, 4, 9, 5]
        
        // 简化
        let arrayInt2 = arrayString.compactMap { $0.count }
        print("arrayInt2: \(arrayInt2)")
        // arrayInt2: [3, 3, 3, 4, 9, 5]
        
        let arrayI = arrayString.compactMap { $0.contains("i") ? $0 : nil }
        print("arrayI: \(arrayI)")
        // arrayI: ["Lily", "HanMeiMei"]

结果可以看出,虽然闭包返回值是可选的,但是真正返回的结果中,并不是可选的,也会过滤掉nil的情况。

swift学习
Web note ad 1