// // Array+Uniques.swift // Tusker // // Created by Shadowfacts on 1/6/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import Foundation extension Array { func uniques(by identify: (Element) -> ID) -> [Element] { var uniques = Set>() for elem in self { uniques.insert(Hashed(element: elem, id: identify(elem))) } return uniques.map(\.element) } } extension Array where Element: Hashable { func uniques() -> [Element] { return uniques(by: { $0 }) } } fileprivate struct Hashed: Hashable { let element: Element let id: ID static func ==(lhs: Self, rhs: Self) -> Bool { return lhs.id == rhs.id } func hash(into hasher: inout Hasher) { hasher.combine(id) } }