數(shù)組的map、reduce方法
map方法將遍歷序列中每個(gè)元素
[1,2,3,4,5,6].map { (i) -> Int in
return i * 2
}
簡化版
[1,2,3,4,5,6].map({$0 * 2})
reduce: i 這里是一個(gè)累加器(并不表示相加)鸭津,保存了前一次運(yùn)算的結(jié)果彤侍。
[1,2,3,4,5,6].reduce(1) { (i, element) -> Int in
print(i,element)
return i * element
}
上面的reduce方法可以簡化為
[1,2,3,4,5,6].reduce(1, combine: *)
下面來看看flatMap,該函數(shù)會忽略為nil的集合元素。
let persons: [[String: AnyObject]] = [["name": "Carl Saxon", "city": "New York, NY", "age": 44],
["name": "Travis Downing", "city": "El Segundo, CA", "age": 34],
["name": "Liz Parker", "city": "San Francisco, CA", "age": 32],
["name": "John Newden", "city": "New Jersey, NY", "age": 21],
["name": "Hector Simons", "city": "San Diego, CA", "age": 37],
["name": "Brian Neo", "age": 27]] //注意這家伙沒有 city 鍵值
//找到住在CA的居民逆趋。
func peopleFromState(state:String,persons:[[String:AnyObject]]) -> Int{
return persons.flatMap { $0["city"]?.componentsSeparatedByString(", ").last}.filter { $0 == state}.count
}
peopleFromState("CA", persons: persons)
用reduce實(shí)現(xiàn)map盏阶、flatMap
func rmap(element:[Int],transform:(Int)->Int ) ->[Int]{
return element.reduce([Int](), combine: { (var accemulator:[Int], obj:Int) -> [Int] in
print(accemulator,obj)
accemulator.append(transform(obj))
return accemulator
})
}
rmap([1,2,3,4,5,6], transform: {$0 * 2})
func rmap2(element:[Int],transform:(Int)->Int) -> [Int] {
return element.reduce([Int](), combine: {$0 + [transform($1)]})
}
let array = rmap2([1,2,3,4,5,6], transform: {$0 * 2})
print(array)
結(jié)果都是[2, 4, 6, 8, 10, 12]
理解 $0, $1
$0,$1是一個(gè)常量,類型根據(jù)所在函數(shù)的參數(shù)類型決定闻书。
為何它們是這個(gè)類型呢名斟?
其所在函數(shù)是combine,那么$0和$1的類型都是由combine這個(gè)(函數(shù)/閉包/方法)的第二個(gè)參數(shù)類型決定的魄眉。所以我們來看看reduce函數(shù)的定義:
public func reduce<T>(initial: T, @noescape combine: (T, Self.Generator.Element) throws -> T) rethrows
$0代表第一個(gè)參數(shù)砰盐,其類型這里是[Int],$1代表的是combine內(nèi)的第二個(gè)參數(shù),所以其類型就是Int的坑律。
用reduce實(shí)現(xiàn)filter
func rfilter(element:[Int],filter:(Int)->Bool)-> [Int]{
return element.reduce([Int](), combine: { (var acc:[Int], value:Int) -> [Int] in
if filter(value){
acc.append(value)
}
return acc
})
}
let xxc = rfilter([1,2,3,4,5,6], filter: { $0 > 3})
print(xxc)
func rfilter2(element:[Int],filter:(Int)->Bool)-> [Int]{
return element.reduce([Int](), combine: { guard filter($1) else{ return $0}
return $0 + [$1]
})
}
let oos = rfilter2([1,2,3,4,5,6], filter: {$0 > 3 })
print(oos)
再看前面提過的問題:找到住在CA的居民岩梳。并且計(jì)算他們的平均年齡
func peopleFromState(state:String,persons:[[String:AnyObject]]) -> (population:Int,averageAge:CGFloat){
//return persons.flatMap { $0["city"]?.componentsSeparatedByString(", ").last}.filter { $0 == state}.count
typealias Result = (population:Int,averageAge:CGFloat)
let u = persons.reduce((populatoin:0,averageAge:0.0)) { (var result:Result, dic:[String:AnyObject]) -> Result in
guard let locationStr = dic["city"]?.componentsSeparatedByString(", ").last,let personAge = dic["age"] where locationStr == state else{ return result}
return (result.population + 1,result.averageAge + CGFloat(personAge as! NSNumber))
}
return (u.populatoin,u.averageAge / CGFloat(u.populatoin))
}
let result = peopleFromState("CA", persons: persons)
result.population // 3
result.averageAge // 34.333333334