-
-
Save s3cur3/63aa3ea34ef36534c1b011ac75fc56ea to your computer and use it in GitHub Desktop.
Generic versions of transforming Dictionary keys and values, inspired by https://twitter.com/ChristianSelig/status/1372961695346352130
This file contains 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 mapKeys<NewKey>(_ transform: (Key) -> NewKey) -> [NewKey: Value] { | |
reduce(into: [NewKey: Value]()) { result, item in | |
result[transform(item.key)] = item.value | |
} | |
} | |
} | |
/// 🥤 | |
extension Dictionary where Key: RawRepresentable { | |
func rawConversion<NewKey>() -> [NewKey: Value] where NewKey == Key.RawValue { | |
mapKeys(\.rawValue) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to match the naming scheme of the existing
Dictionary.mapValues
.