sort , forEach, filter, first(where:), last(where:) removeAll(where:), allSatisfy, map, reduce
// MARK: sort
func testSort() {
var array1 = ["Animal", "Baby", "Apple", "Google", "Aunt"]
array1.sort()
let array2 = ["Animal", "Baby", "Apple", "Google", "Aunt"]
array2.sorted()
/**
1. 默認(rèn)是升序
2. sorted 返回一個(gè)新的集合
3. sort 直接在原來的集合上操作
*/
//降序
array1.sort() { (str1, str2) in
return str1 > str2 // 9times
}
array2.sorted() { (str1, str2) in
return str1 > str2
}
//降序 簡(jiǎn)寫
array1.sort { return $0 > $1 } // 10times
array2.sorted { return $0 > $1 }
}
// MARK: forEach
func testForEach() {
let array1 = ["Animal", "Baby", "Apple", "Google", "Aunt"]
array1.forEach { str in print(str) }
array1.forEach { print($0) }
}
// MARK: filter & first(where:) & last(where:) & removeAll(where:)
func testFilter() {
var array1 = ["Animal", "Baby", "Apple", "Google", "Aunt"]
/**
篩選里面的閉包必須返回Bool杭跪, 篩選出返回true的元素
*/
array1.filter { str in
str.hasPrefix("A")
}.forEach{ print($0) }
array1.filter { $0.hasPrefix("A") }.forEach{ print($0) }
// 篩選出第一個(gè)符合條件的元素
let ele1 = array1.first { $0.hasPrefix("G") }
ele1
// 篩選出隨后一個(gè)符合條件的元素
let ele2 = array1.last { $0.hasPrefix("B") }
ele2
//刪除符合條件的元素
array1.removeAll { $0.hasPrefix("A") }
array1
}
// MARK: allSatisfy
func testAllSatisfy() {
/**
檢查序列中所有元素是否滿足條件耀态,全部滿足才返回true
*/
let scores = [86, 88, 95, 90]
let passed = scores.allSatisfy {
$0 > 80
}
passed
}
// MARK: map & compactMap & mapValues & compactMapValues
func testMap() {
/**
閉包返回一個(gè)變換后的元素,所有變換后的元素組成一個(gè)新的集合
*/
let newStr = "test".map { "\($0) - new" }
newStr
let array3 = ["array1","array2","array3","array4","array5"]
let array3New = array3.map { "\($0)-new" }
array3 // ["array1", "array2", "array3", "array4", "array5"]
array3New // ["array1-new", "array2-new", "array3-new", "array4-new", "array5-new"]
let dic = ["key1": "value1"]
let dicNew = dic.map { ["\($0.key)-new" : "\($0.value)-new"] }
dic // ["key1": "value1"]
dicNew // [["key1-new": "value1-new"]]
/**
compactMap 可以過濾掉空元素
compactMap(flatMap 被廢棄了) 可以過濾掉空元素,
*/
let array4 = ["array1","array2", nil ,"array4","array5"]
let list1 = array4.map { $0 }
list1 // ["array1", "array2", nil, "array4", "array5"]
let list2 = array4.compactMap { $0 }
list2 // "array1", "array2", "array4", "array5"]
/**
mapValues & compactMapValues 專門用于字典
*/
let dic1 = ["1": "1", "2": "2", "3": "3", "4": "r", "5": "5"]
/**
mapValues類似數(shù)組中的map鸳吸,只是專門針對(duì)字典的values, mapValues 返回的還是一個(gè)字典
如果在字典上用map淋样,返回的是數(shù)組(key/value)
compactMapValues 類似數(shù)組中的compactMap酗失,過濾掉不符合條件的內(nèi)容启具,只是專門針對(duì)字典的values, compactMapValues 返回的還是一個(gè)字典
如果在字典上用compactMap蕊玷,返回的是數(shù)組(key/value)
*/
let result1 = dic1.mapValues { Int($0)}
result1 // ["4": nil, "5": 5, "3": 3, "1": 1, "2": 2]
let result2 = dic1.map { Int($0.value)} // [nil, 5, 3, 1, 2]
result2
let result3 = dic1.compactMapValues { Int($0) } // ["2": 2, "1": 1, "3": 3, "5": 5]
result3
let result4 = dic1.compactMap { Int($0.value) } // [5, 2, 3, 1]
result4
}
// MARK: reduce 把多個(gè)元素的值合并為一個(gè)新的值
func testReduce() {
let sum = [1, 3, 3, 4]
let start = 102
/**
可以把多個(gè)元素的值合并成一個(gè)新的值
start: 設(shè)置初始狀態(tài)
result: 中間結(jié)果
num: 集合中的每個(gè)元素
*/
//直接返回結(jié)果
let total = sum.reduce(start) { result, num -> Int in
print("result \(result); num \(num)")
return result+num
}
total // 113
//不返回結(jié)果
var into = 102
let total2 = sum.reduce(into: into) { result, num in
print("result \(result); num \(num)")
into += num
print("into \(into)")
}
into // 113
total2 // 102
//簡(jiǎn)寫1
let total3 = sum.reduce(start) { $0 + $1}
total3 // 113
//簡(jiǎn)寫2
let total4 = sum.reduce(start, +)
total4 // 113
//字符串 reduce
let total5 = ["a","b","c"].reduce("1", { $0 + ", " + $1})
total5 // "1, a, b, c"
let dic = ["1": "value1", "2": "value2", "3": "value3", "4": "valuer", "5": "value5"]
let reduceResult = dic.reduce("======") { (result, item: (key: String, value: String)) -> String in
result + item.value
}
reduceResult // "======value2value3value5value1valuer"
}
//testReduce()
// MARK: 組合方法
func comminMethod() {
let array1 = ["Animal", "Baby", "Apple", "Google", "Aunt"]
array1
.sorted{ $0 > $1}
.filter { $0.hasPrefix("A")}
.forEach { print($0) }
}
comminMethod()