I have some question of map function on Collection
@_inlineable
public func map<T>(
_ transform: (Element) throws -> T
) rethrows -> [T] {
// TODO: swift-3-indexing-model - review the following
let n = self.count
if n == 0 {
return []
}
i see let n = self.count; if n == 0.
this shouldn’t be ‘if self.isEmpty { … }; let n = self.count’?
Nevin
2
You’re talking about here in Collection.swift.
Look a few lines down and you’ll see that n is used as the capacity to reserve in a ContiguousArray. So it is correct as written: the count needs to be found and used.
1 Like
saagarjha
(Saagar Jha)
3
The way @Kwanghoon_Choi wrote their suggested edit, it appears that they already believe that n is needed:
If so, I think their issue is with the use of count, which is O(n), over isEmpty, which is O(1). In this case O(n)≈O(1) since n is zero.
2 Likes
ah thanks to reply. it had simple reason.