原文链接
Swift是支持一门函数式编程的语言,拥有Map,FlatMap,Filter,Reduce针对集合类型的操作.本文主要根据官方文档举例了解Swift中的Map,FlatMap,Filter,Reduce
首先我们来看一下map
在Swift
中的的定义,我们看到它可以用在 Optionals 和 SequenceType 上(如:数组、词典等)
public enum Optional : _Reflectable, NilLiteralConvertible {
/// If `self == nil`, returns `nil`. Otherwise, returns `f(self!)`.
@warn_unused_result
public func map(@noescape f: (Wrapped) throws -> U) rethrows -> U?
}
extension CollectionType {
/// Returns an `Array` containing the results of mapping `transform`
/// over `self`.
///
/// - Complexity: O(N).
@warn_unused_result
public func map (@noescape transform: (Self.Generator.Element) throws -> T) rethrows -> [T]
}
我们可以用For-in完成类似的操作
var values = [1,3,5,7]
var results = [Int]()
for var value in values {
value *= 2
results.append(value)
}
//"[2, 6, 10, 14]"
print(results)
Map操作
var values = [1,3,5,7]
let results = values.map (){ $0 * 2 }
//"[2, 6, 10, 14]"
与map一样,它可以用在 Optionals和 SequenceType 上(如:数组、词典等)。
public enum Optional : _Reflectable, NilLiteralConvertible {
/// Returns `nil` if `self` is `nil`, `f(self!)` otherwise.
@warn_unused_result
public func flatMap(@noescape f: (Wrapped) throws -> U?) rethrows -> U?
}
extension SequenceType {
/// 返回一个将变换结果连接起来的数组
/// `transform` over `self`.
/// s.flatMap(transform)
/// is equivalent to
/// Array(s.map(transform).flatten())
@warn_unused_result
public func flatMap(transform: (Self.Generator.Element) throws -> S) rethrows -> [S.Generator.Element]
}
extension SequenceType {
/// 返回一个包含非空值的映射变换结果
@warn_unused_result
public func flatMap(@noescape transform: (Self.Generator.Element) throws -> T?) rethrows -> [T]
}
一:空值过滤
var values:[Int?] = [1,3,5,7,9,nil]
let flattenResult = values.flatMap{ $0 }
/// [1, 3, 5, 7, 9]
二:压平,(降维)
var values = [[1,3,5,7],[9]]
let flattenResult = values.flatMap{ $0 }
/// [1, 3, 5, 7, 9]
同样,我先来看看Swift中的定义:
extension SequenceType {
/// 返回包含原数组中符合条件的元素的数组
/// Returns an `Array` containing the elements of `self`,
/// in order, that satisfy the predicate `includeElement`.
@warn_unused_result
public func filter(@noescape includeElement: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.Generator.Element]
}
eg: 我们向flatMap传入了一个闭包,筛选出了能被3整除的数据
var values = [1,3,5,7,9]
let flattenResults = values.filter{ $0 % 3 == 0}
//[3, 9]
我们先来看下Swift中的定义:
extension SequenceType {
/// Returns the result of repeatedly calling `combine` with an
/// accumulated value initialized to `initial` and each element of
/// `self`, in turn, i.e. return
/// `combine(combine(...combine(combine(initial, self[0]),
/// self[1]),...self[count-2]), self[count-1])`.
@warn_unused_result
public func reduce(initial: T, @noescape combine: (T, Self.Generator.Element) throws -> T) rethrows -> T
}
eg
let fiveArray = [1,2,3,4,5]
let sum = fiveArray.reduce(0) { $0 + $1 } //求数组元素的和
let sum1 = fiveArray.reduce(0, +) // 简写
let sum2 = fiveArray.filter{ $0 > 2 }.reduce(0, +) // 数组中元素大于2 的数据求和
print(sum,sum1,sum2)
// 15 15 12