美文网首页
swift开发--函数map、flatMap、filter、re

swift开发--函数map、flatMap、filter、re

作者: 又是一个程序猿 | 来源:发表于2018-07-18 11:44 被阅读0次

    Swift相比于Objective-C又一个重要的优点,它对函数式编程提供了很好的支持,Swift提供了map、filter、reduce这三个高阶函数作为对容器的支持。

    1 map:可以对数组中的每一个元素做一次处理
    let stringArray = ["Objective-C", "Swift", "HTML", "CSS", "JavaScript"]
            //1.先定义返回字符串个数的方法,再map
            func stringCount(string: String) -> Int {
                return string.count
            }
            let count = stringArray.map(stringCount)
            Tprint(messsage: count)//[11, 5, 4, 3, 10]
            
            //2.直接使用map方法
            let count1 = stringArray.map{ (string) -> Int in
                return string.count
            }
            Tprint(messsage: count1)//[11, 5, 4, 3, 10]
    
            // 3.$0代表数组中的每一个元素
            let count2 = stringArray.map{
                return $0.count 
            }
            Tprint(messsage: count2)//[11, 5, 4, 3, 10]
    
    2 flatMap与map不同之处:

    (1)flatMap返回后的数组中不存在nil,同时它会把Optional解包

            let count3 = stringArray.map { (str) -> Int? in
                return str.count
            }
             Tprint(messsage: count3)//[Optional(11), Optional(5), Optional(4), Optional(3), Optional(10)]
            
            let count4 = stringArray.flatMap { (str) -> Int? in
                return str.count
            }
            Tprint(messsage: count4)//[11, 5, 4, 3, 10]
    

    (2)flatMap还能把数组中存有数组的数组(二维数组、N维数组)一同打开变成一个新的数组

    let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    
    let arr1 = array.map{ $0 }
    arr1 // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    
    let arr2 = array.flatMap{ $0 }
    arr2 // [1, 2, 3, 4, 5, 6, 7, 8, 9]
    

    (3)flatMap也能把两个不同的数组合并成一个数组,这个合并的数组元素个数是前面两个数组元素个数的乘积

    let fruits = ["Apple", "Orange", "Puple"]
    let counts = [2, 3, 5]
    let array = counts.flatMap { count in
        fruits.map ({ fruit in
             return fruit + "  \(count)"            
        })   
    }
    array // ["Apple 2", "Orange 2", "Puple 2", "Apple 3", "Orange 3", "Puple 3", "Apple 5", "Orange 5", "Puple 5"]
    
    3 filter:过滤,可以对数组中的元素按照某种规则进行一次过滤
    let stringArray = ["Objective-C", "Swift", "HTML", "CSS", "JavaScript"]
       // 筛选出字符串的长度小于10的字符串
            func stringCountLessThan10(str :String) -> Bool{
                return str.count < 10
            }
            let count5 = stringArray.filter(stringCountLessThan10(str:))
             Tprint(messsage: count5)
            
            let count6 = stringArray.filter { (str) -> Bool in
                return str.count < 10
            }
            Tprint(messsage: count6)
            
            let count7 = stringArray.filter { $0.count < 10}
            Tprint(messsage: count7)
    
    4 reduce:计算,可以对数组的元素进行计算
    // 将数组中的每个字符串用‘、’拼接
    let stringArray = ["Objective-C", "Swift", "HTML", "CSS", "JavaScript"]
    
    func appendString(string1: String, string2: String) -> String {
        return string1 == "" ? string2 : string1 + "、" + string2
    }
    // reduce方法中的第一个参数是初始值
    stringArray.reduce("", appendString)
    
    stringArray.reduce("", {(string1, string2) -> String in
        return string1 == "" ? string2 : string1 + "、" + string2
    })
    
    // $0表示计算后的结果, $1表示数组中的每一个元素
    stringArray.reduce("", {
        return $0 == "" ? $1 : $0 + "、" + $1
    })
    

    好啦,介绍完了愿能与君共勉。

    相关文章

      网友评论

          本文标题:swift开发--函数map、flatMap、filter、re

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