18 lines
547 B
Swift
18 lines
547 B
Swift
import Foundation
|
|
|
|
public extension Dictionary {
|
|
func mapKeys<T>(_ transform: (Key) throws -> T) rethrows -> [T: Value] {
|
|
try reduce(into: [T: Value]()) { result, element in
|
|
try result[transform(element.key)] = element.value
|
|
}
|
|
}
|
|
|
|
func compactMapKeys<T>(_ transform: (Key) throws -> T?) rethrows -> [T: Value] {
|
|
try reduce(into: [T: Value]()) { result, element in
|
|
if let newKey = try transform(element.key) {
|
|
result[newKey] = element.value
|
|
}
|
|
}
|
|
}
|
|
}
|