Created
May 4, 2017 02:38
-
-
Save xuyazhong/6c8b438e0a6d1488443f0a814b45cc76 to your computer and use it in GitHub Desktop.
数组Map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/apple/swift/blob/master/stdlib/public/core/Collection.swift | |
@_inlineable | |
public func map<T>(_ transform: (Iterator.Element) throws -> T) rethrows -> [T] { | |
// TODO: swift-3-indexing-model - review the following | |
let count: Int = numericCast(self.count) | |
if count == 0 { | |
return [] | |
} | |
var result = ContiguousArray<T>() | |
result.reserveCapacity(count) | |
var i = self.startIndex | |
for _ in 0..<count { | |
result.append(try transform(self[i])) | |
formIndex(after: &i) | |
} | |
_expectEnd(of: self, is: i) | |
return Array(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment