利用max方法,可直接遍歷出數組中最大值和最小值數組
var arr = [1, 2, 3, 4] //// 返回數組中的最大值 和 最小值 //@inlinable public func max(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> Element? // 根據返回值true和false返回數組中的最大值和最小值,是一個可選值 var maxElement = arr.max { (temp1, temp2) -> Bool in return temp1 < temp2 }
map方法可遍歷整個數組,並對數組個每一個元素進行操做,返回處理後的數組閉包
/// Returns an array containing the results of mapping the given closure /// over the sequence's elements. /// /// In this example, `map` is used first to convert the names in the array /// to lowercase strings and then to count their characters. /// /// let cast = ["Vivien", "Marlon", "Kim", "Karl"] /// let lowercaseNames = cast.map { $0.lowercased() } /// // 'lowercaseNames' == ["vivien", "marlon", "kim", "karl"] /// let letterCounts = cast.map { $0.count } /// // 'letterCounts' == [6, 6, 3, 4] /// /// - Parameter transform: A mapping closure. `transform` accepts an /// element of this sequence as its parameter and returns a transformed /// value of the same or of a different type. /// - Returns: An array containing the transformed elements of this /// sequence. //@inlinable public func map<T>(_ transform: (Element) throws -> T) rethrows -> [T] var arr3 = arr.map { (temp) -> Int in return temp * 2 } print(arr3) //[2, 4, 6, 8]
filter方法可遍歷整個數組,並依據設置的條件,對數組篩選並返回新的數組app
// 根據條件進行過濾 var arr4 = arr.filter { (temp) -> Bool in return temp % 2 == 0 } print(arr4) // [2, 4]
reduce設置初始參數,根據傳入的閉包操做,返回結果。例如相乘或者相加ui
/// Returns the result of combining the elements of the sequence using the /// given closure. // @inlinable public func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> Result var arr5 = arr.reduce(5) { (result, temp) -> Int in // 5 * 1 * 2 * 3 * 4 return result * temp } //簡便寫法 var arr6 = arr.reduce(5, *) print(arr5) // 120
// @inlinable public func map<T>(_ transform: (Element) throws -> T) rethrows -> [T] var arr7 = arr.map { Array.init(repeating: $0, count: $0) } print(arr7) /// Returns an array containing the concatenated results of calling the /// given transformation with each element of this sequence. /// In fact, `s.flatMap(transform)` is equivalent to /// `Array(s.map(transform).joined())`. //@inlinable public func flatMap<SegmentOfResult>(_ transform: (Element) throws -> SegmentOfResult) rethrows -> [SegmentOfResult.Element] where SegmentOfResult : Sequence var arr8 = arr.flatMap { Array.init(repeating: $0, count: $0) } print(arr8) //[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]] //[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
二者相比較,正如文檔註釋一下flatMap會拼接成一個數組this
var stringArr = ["123", "test", "haha", "-234"] var arr2 = stringArr.map { Int($0) } print(arr2) /// Returns an array containing the non-`nil` results of calling the given /// transformation with each element of this sequence. var arr3 = stringArr.compactMap { Int($0) } print(arr3) //[Optional(123), nil, nil, Optional(-234)] //[123, -234]
map會完成所傳入的閉包操做,如果失敗會賦值爲空,成功會返回一個可選值spa
compactMap根據閉包的操做,如果失敗則被移除,返回的解包後的值code
let result = arr.map { (temp) -> Int in print("map \(temp)") return temp * 2 } print("begin--") print("test", result[0]) print("test", result[1]) print("test", result[2]) print("end")
數組的遍歷未使用lazy時候,打印結果以下:orm
map 1 map 2 map 3 map 4 begin-- test 2 test 4 test 6 end
會先按照順序執行,執行完map遍歷,再執行下面的打印blog
而對於利用lazy遍從來說,代碼以下:element
let result = arr.lazy.map { (temp) -> Int in print("map \(temp)") return temp * 2 } print("begin--") print("test", result[0]) print("test", result[1]) print("test", result[2]) print("end")
打印結果以下:
begin-- map 1 test 2 map 2 test 4 map 3 test 6 end
會發現數組裏面的打印會在該數組的元素被使用的時候纔會調用,這個方法適用於當數組元素的數量比較大時候