Skip to content

Instantly share code, notes, and snippets.

@radianttap
Created April 12, 2019 14:12
Show Gist options
  • Save radianttap/dccb96ade1fd66e93626bfaa97f2b980 to your computer and use it in GitHub Desktop.
Save radianttap/dccb96ade1fd66e93626bfaa97f2b980 to your computer and use it in GitHub Desktop.
Dictionary, mapKeys (counterpart to mapValues)
public extension Dictionary {
func mapKeys<T>(_ transform: (Key) throws -> T) rethrows -> [T : Value] where T: Hashable {
var dict: [T: Value] = [:]
for (key, value) in self {
let newKey = try transform(key)
dict[newKey] = value
}
return dict
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment