Skip to content

Instantly share code, notes, and snippets.

@kiliankoe
Created July 4, 2016 10:49
Show Gist options
  • Save kiliankoe/2780dd0084398cae01f3e95ce80baaf4 to your computer and use it in GitHub Desktop.
Save kiliankoe/2780dd0084398cae01f3e95ce80baaf4 to your computer and use it in GitHub Desktop.
Dictionary mapValues
extension Dictionary {
func mapValues<TransformedValue: Sequence where Value: Sequence>(transform: (Value) -> TransformedValue) -> Dictionary<Key, TransformedValue> {
var newDict: [Key: TransformedValue] = [:]
for (k, v) in self {
newDict[k] = transform(v)
}
return newDict
}
}
let dict = [
1: ["foo", "bar"],
2: ["baz", "bar"]
]
let newDict = dict.mapValues {
$0.sorted()
}
print(newDict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment