1.filter
filter: 过滤,可以对数组中的元素按照某种规则进行一次过滤。
函数声明:
public func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> [Element]
从定义上看,filter函数的闭包是 (Element) throws -> Bool, 有一个参数,返回Boo值,
整个函数返回值是符合筛选条件的一个子数组。所以,这里,我们在闭包中一定是一个Bool表达式。
使用示例:
let array = [1,2,3,4,5,6,7,2,3,5,9,7,3,2,2,1,4,5,7,4,3,2,2,4,5]
// 找出大于5的元素
let array5 = array.filter { (item) -> Bool in
return item > 5
}
print("array5: \(array5)")
// array5: [6, 7, 9, 7, 7]
// 简化
let arr5 = array.filter { $0 > 5 }
print("arr5: \(arr5)")
// arr5: [6, 7, 9, 7, 7]
// 找出小于4的元素
let arr4 = array.filter { $0 < 4 }
print("arr4: \(arr4)")
// arr4: [1, 2, 3, 2, 3, 3, 2, 2, 1, 3, 2, 2]
let arrayString = ["Objective-C", "Swift", "HTML", "CSS", "Java", "C++", "JavaScript"]
// 筛选字符个数小于10的元素
let arrayLess10 = arrayString.filter { $0.count < 10 }
print("arrayLess10: \(arrayLess10)")
// arrayLess10: ["Swift", "HTML", "CSS", "Java", "C++"]
// 筛选包含S的元素
let arrayS = arrayString.filter { $0.contains("S") }
print("arrayS: \(arrayS)")
// arrayS: ["Swift", "CSS", "JavaScript"]
小结:
filter与之前学习过的Map和flatMap、compactMap还是有很大区别的,虽然有时候实现一些过滤的
需求,这些方法都可以实现。filter更像是从原来数组中寻找合适的,符合条件的元素,而不可能
创造出新的元素来,闭包中返回的也是Bool值。Map和flatMap、compactMap是可以创造出新的元素的,
它们是对元素做处理,至于返回什么,是可以自己决定的。
2.reduce
reduce:计算,可以对数组的元素进行计算。
public func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> Result
第一个参数表示起始位置,第二个参数表示数组元素
使用示例:
let festivals = ["NewYear",
"SpringFestival",
"LabourDay",
"Mid-AutumnFestival",
"NationalDay"]
// 将数组中的每个字符串用”、“拼接
let str = festivals.reduce("$") { (str1, str2) -> String in
return str1 == "$" ? str2 : str1 + "、" + str2
}
print("str: \(str)")
// str: NewYear、SpringFestival、LabourDay、Mid-AutumnFestival、NationalDay
// 返回结果是拼接起来的字符串
// 我们打印一下str1和str2
let strL = festivals.reduce("$") { (str1, str2) -> String in
print("str1: \(str1)")
print("str2: \(str2)")
return str1 == "$" ? str2 : str1 + "、" + str2
}
print("strL: \(strL)")
/*
结果:
str1: $
str2: NewYear
str1: NewYear
str2: SpringFestival
str1: NewYear、SpringFestival
str2: LabourDay
str1: NewYear、SpringFestival、LabourDay
str2: Mid-AutumnFestival
str1: NewYear、SpringFestival、LabourDay、Mid-AutumnFestival
str2: NationalDay
strL: NewYear、SpringFestival、LabourDay、Mid-AutumnFestival、NationalDay
*/
// 案例二
/*
问题描述:
有一份人员名单,要从这些名单中找出来自”北京“的年龄总和.
*/
let persons = [
["姓名": "李二", "性别":"男", "年龄":"23", "籍贯":"山西", "职业":"程序员"],
["姓名": "张倩", "性别":"女", "年龄":"20", "籍贯":"北京", "职业":"体操运动员"],
["姓名": "王淼", "性别":"男", "年龄":"30", "籍贯":"四川", "职业":"饭店老板"],
["姓名": "曾无", "性别":"男", "年龄":"52", "籍贯":"北京", "职业":"公务员"],
["姓名": "梅秉", "性别":"女", "年龄":"35", "籍贯":"北京", "职业":"医生"]
]
let age = persons.reduce(0) { (average, dict) -> Int in
print("average: \(average)")
print("dict: \(dict)")
var ages = average
if dict["籍贯"] == "北京" {
if let str = dict["年龄"],
let tempAge: Int = Int(str) {
ages = ages + tempAge
}
}
return ages
}
print("age: \(age)")
/*
打印结果:
average: 0
dict: ["年龄": "23", "籍贯": "山西", "职业": "程序员", "性别": "男", "姓名": "李二"]
average: 0
dict: ["年龄": "20", "籍贯": "北京", "职业": "体操运动员", "性别": "女", "姓名": "张倩"]
average: 20
dict: ["年龄": "30", "籍贯": "四川", "职业": "饭店老板", "性别": "男", "姓名": "王淼"]
average: 20
dict: ["年龄": "52", "籍贯": "北京", "职业": "公务员", "性别": "男", "姓名": "曾无"]
average: 72
dict: ["年龄": "35", "籍贯": "北京", "职业": "医生", "性别": "女", "姓名": "梅秉"]
age: 107
*/
经过这几次练习,对这几个高阶函数熟悉多了,使用起来也熟练多了,练习的目的算是达到了呢。
始终相信,自己就是一只蜗牛,不聪明,但是也要脚踏实地的慢慢地爬。