Created
September 23, 2019 06:06
-
-
Save paradoxeth/1e6f74aa4e8ffd4cf877bacfffdc5b15 to your computer and use it in GitHub Desktop.
UniquieMap
This file contains 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
public func uniqueMap<T: Equatable>(_ transform: (Iterator.Element) throws -> T) rethrows -> [T] { | |
let count: Int = numericCast(self.count) | |
if isEmpty { | |
return [] | |
} | |
var result = ContiguousArray<T>() | |
result.reserveCapacity(count) | |
var i = self.startIndex | |
for _ in 0..<count { | |
let transformed = try transform(self[i]) | |
if !result.contains(where: { $0 == transformed }) { | |
result.append(transformed) | |
} | |
formIndex(after: &i) | |
} | |
return Array(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment