Swift 常用高阶函数

Swift 常用高阶函数

  • map函数
    • 对每一个元素进行运算
    • 计算每一个元素的count
    • 对元素进行大小写变换
    • 转换类型
  • sorted函数
    • 从小到大排序
    • 从大到小排序
  • flatMap函数
    • 降维
    • 过滤元素类型
  • filter函数
    • 挑选count > 3的元素
    • 挑选出大于0的元素
    • 组合判断,挑选出大于0的元素,并排序
  • reduce函数
    • 拼接元素
    • 计算元素字符个数
    • 求和

map函数

对每一个元素进行运算

var values = [1, 2, 3]
values = values.map({ element in
    element + 10
})

print(values) //[11, 12, 13]

计算每一个元素的count

let strings = ["one", "two", "three"]
let count = strings.map({ element in
    element.count
})

print(count) //[3, 3, 5]

对元素进行大小写变换

var words = ["C", "D", "F", "A"]
words = words.map({ element in
    //小写
    element.lowercased()
})

print(words) //["c", "d", "f", "a"]

words = words.map({ element in
    //大写
    element.uppercased()
})

print(words) //["C", "D", "F", "A"]

转换类型

let values2 = [1, 2, 3]
let strings2 = values2.map {
    //转为字符串
    String($0)
}

print(strings2) //["1", "2", "3"]

let intArr = strings2.map({
    //转为Int
    Int($0)
})

print(intArr) //[Optional(1), Optional(2), Optional(3)]

let floatArr = strings2.map({
    //转为Float
    Float($0)
})

print(floatArr) //[Optional(1.0), Optional(2.0), Optional(3.0)]

sorted函数

从小到大排序

let strings3 = [1, 10, 12, 5, 7, -5]
//从小到大排序
let value1 = strings3.sorted(by: <)
print(value1) //[-5, 1, 5, 7, 10, 12]

从大到小排序

let value2 = strings3.sorted(by: >)
print(value2) //[12, 10, 7, 5, 1, -5]

flatMap函数

降维

let arr1 = [1, 2, 3]; let arr2 = [4, 5, 6]; let arr3 = [7, 8, 9]
let value = [arr1, arr2, arr3]
print(value) //[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

let flatMapArr = value.flatMap( {$0} )
print(flatMapArr) //[1, 2, 3, 4, 5, 6, 7, 8, 9]

过滤元素类型

let varietyTypeArr = ["str", "4", "8", "str2"]
let strArr = varietyTypeArr.flatMap { element in
    return Int(element)
}
print(strArr) //[4, 8]
//注意 'flatMap' is deprecated: Please use compactMap(_:) for the case where closure returns an optional value
let compactMapArr = varietyTypeArr.compactMap { element in
    return Int(element)
}
print(compactMapArr) //[4, 8]

filter函数

挑选count > 3的元素

let strs = ["one", "A", "C", "two", "three", "four"]
let ret1 = strs.filter( {$0.count > 3} )
print(ret1) //["three", "four"]
//挑选出大于0的元素
let values = [1, 10, 12, 5, 7, -5]
let ret2 = values.filter( {$0 > 0} )
print(ret2) //[1, 10, 12, 5, 7]
//组合判断,挑选出大于0的元素,并排序
let ret3 = values.filter( {$0 > 0} ).sorted(by: >)
print(ret3) //[12, 10, 7, 5, 1]

挑选出大于0的元素

let values = [1, 10, 12, 5, 7, -5]
let ret2 = values.filter( {$0 > 0} )
print(ret2) //[1, 10, 12, 5, 7]

组合判断,挑选出大于0的元素,并排序

let ret3 = values.filter( {$0 > 0} ).sorted(by: >)
print(ret3) //[12, 10, 7, 5, 1]

reduce函数

拼接元素

let strs = ["one", "two", "three"]
let ret = strs.reduce("to") { (result, element) -> String in
    //拼接到前边
//            return result + element
    //拼接到后边
    return element + result
}
print(ret) //toonetwothree threetwooneto

计算元素字符个数

let count = strs.reduce(0) { partialResult, element in
    return partialResult + element.count
}
print(count) //11

//计算字符串中相同字符的个数
let letters = "aabbbccccddddddeeed"
let letterCount = letters.reduce(into: [:]) { counts, letter in
    counts[letter, default: 0] += 1
}
print(letterCount) //["a": 2, "c": 4, "d": 7, "e": 3, "b": 3]

求和

let values = [1, 10, 12, 5, 7, -5]
let sums = values.reduce(0, +)
print(sums) //30

你可能感兴趣的:(Swift,swift,高阶函数)