Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xuyazhong/6c8b438e0a6d1488443f0a814b45cc76 to your computer and use it in GitHub Desktop.
Save xuyazhong/6c8b438e0a6d1488443f0a814b45cc76 to your computer and use it in GitHub Desktop.
数组Map
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