Created
July 4, 2016 10:49
-
-
Save kiliankoe/2780dd0084398cae01f3e95ce80baaf4 to your computer and use it in GitHub Desktop.
Dictionary mapValues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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