美文网首页
Swift数组高阶函数

Swift数组高阶函数

作者: 肆点壹陆 | 来源:发表于2019-03-02 23:22 被阅读0次

    开发语言:Swift 4.2
    开发环境:Xcode 10.1

    Swift 提供了以下几种高阶函数,用以进行数组转换。

    1、Sequence.map

    遍历原数组,并使用闭包提供的方法对集合的子项进行转换,生成新的数组,例如

    1.1、[Int]转换为[String]

     let test = [1,2,3,4,5]
     let result = test.map({ (value) -> String in
                return value.string
            })
     print(result)
    

    输出:["1", "2", "3", "4", "5"]

    1.2、[Int]转换为[[String]]

    let test = [1,2,3,4,5]
    let result = test.map({ (value) -> [String] in
                return [value.string, (value * 5).string]
            })
    print(result)
    

    输出:[["1", "5"], ["2", "10"], ["3", "15"], ["4", "20"], ["5", "25"]]

    2、Sequence.flatMap

    遍历原数组,生成二维数组,最后降维 或 遍历原数组,过滤非空

    2.1、[Int]转换为[String](将1.2的二维数组降维成一维数组)

    let test = [1,2,3,4,5]
    let result = test.flatMap({ (value) -> [String] in
                return [value.string, (value * 5).string]
            })
    print(result)
    

    输出:["1", "5", "2", "10", "3", "15", "4", "20", "5", "25"]

    2.2、[Int?]转换为[String](过滤非空)

    let test = [1,2,3,4,5,nil]
    let result = test.flatMap({ (value) -> String? in
                guard let noNil = value else {
                    return nil
                }
                return noNil.string
            })
    print(result)
    

    输出:["1", "2", "3", "4", "5"]

    2.3、 问题:如果想同时降维与过滤非空会怎样?

    let test = [1,2,3,4,5,nil]
    let result = test.flatMap({ (value) -> [String]? in
                guard let noNil = value else {
                    return nil
                }
                return [noNil.string, (noNil * 5).string]
            })
    print(result)
    

    输出:[["1", "5"], ["2", "10"], ["3", "15"], ["4", "20"], ["5", "25"]]

    • 此时并没有执行任何降维的操作,事实上在同时有降维和过滤非空的情况下,flatMap只会执行过滤非空的操作
    • 在Swift4.1中,为了不让使用者混淆这两种flatMap的使用情况,引入了新的函数compactMap

    3、Sequence.compactMap

    遍历原数组,过滤非空

    3.1、[Int?]转换为[String](过滤非空)

    let test = [1,2,3,4,5,nil]
    let result = test.compactMap({ (value) -> String? in
                guard let noNil = value else {
                    return nil
                }
                return noNil.string
            })
    print(result)
    

    输出:["1", "2", "3", "4", "5"]

    3.2、[Int]转换为[[String]](无法降维)

    let test = [1,2,3,4,5]
    let result = test.compactMap({ (value) -> [String] in
                return [value.string, (value * 5).string]
            })
    print(result)
    

    输出:[["1", "5"], ["2", "10"], ["3", "15"], ["4", "20"], ["5", "25"]]

    4、Sequence.reduce

    指定一个初始值,遍历原数组,通过闭包的计算将数组元素叠加到初始值上

    let test = [1,2,3,4,5]
    let result = test.reduce("begin") { (res, value) -> String in
                return res + " " + value.string
            }
    print(result)
    

    输出:begin 1 2 3 4 5

    5、Sequence.filter

    遍历原数组,过滤掉不符合闭包的元素

    let test = [1,2,3,4,5]
    let result = test.filter { (value) -> Bool in
                return value % 2 == 0
            }
    print(result)
    

    输出:[2, 4]

    6、Sequence.contains

    遍历原数组,判断是否包含符合闭包条件的元素

    let test = [1,2,3,4,5]
    let result = test.contains { (value) -> Bool in
                return value == 5
            }
    print(result)
    

    输出:true

    7、Sequence.first

    遍历原数组,返回第一个符合闭包条件的元素可选值

    let test = [2,4,6,8,10]
    let result = test.first { (value) -> Bool in
                return value == 6
            }
    print(result)
    

    输出:Optional(6)

    8、Sequence.firstIndex

    遍历原数组,返回第一个符合闭包条件的元素的下标可选值

    let test = [2,4,6,8,10]
    let result = test.firstIndex { (value) -> Bool in
                return value == 6
            }
    print(result)
    

    输出:Optional(2)

    相关文章

      网友评论

          本文标题:Swift数组高阶函数

          本文链接:https://www.haomeiwen.com/subject/bzduuqtx.html