Dictionary.swift 475 B

12345678910111213141516171819
  1. //
  2. // Dictionary.swift
  3. // LoopKitUI
  4. //
  5. // Created by Michael Pangburn on 7/7/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. extension Dictionary {
  9. func compactMapValuesWithKeys<NewValue>(_ transform: (Element) throws -> NewValue?) rethrows -> [Key: NewValue] {
  10. try reduce(into: [:]) { result, element in
  11. if let newValue = try transform(element) {
  12. result[element.key] = newValue
  13. }
  14. }
  15. }
  16. }