CollectionExtensions.swift 638 B

123456789101112131415161718192021222324252627282930
  1. //
  2. // CollectionExtensions.swift
  3. // MiaomiaoClientUI
  4. //
  5. // Created by Bjørn Inge Berg on 26/03/2019.
  6. // Copyright © 2019 Bjørn Inge Berg. All rights reserved.
  7. //
  8. import Foundation
  9. extension Collection {
  10. subscript(safe index: Index) -> Element? {
  11. indices.contains(index) ? self[index] : nil
  12. }
  13. }
  14. extension Array where Element: Hashable {
  15. func removingDuplicates() -> [Element] {
  16. var addedDict = [Element: Bool]()
  17. return filter {
  18. addedDict.updateValue(true, forKey: $0) == nil
  19. }
  20. }
  21. mutating func removeDuplicates() {
  22. self = self.removingDuplicates()
  23. }
  24. }