Skip to content

Instantly share code, notes, and snippets.

@koher
Created October 1, 2016 09:44
Show Gist options
  • Save koher/b47ef1f1e90a65faa643d0376eb33758 to your computer and use it in GitHub Desktop.
Save koher/b47ef1f1e90a65faa643d0376eb33758 to your computer and use it in GitHub Desktop.
extension Dictionary {
func filterMap<T>(_ transform: (Value) -> T?) -> [Key: T] {
var transformed = [Key:T]()
for entry in self {
if let newValue = transform(entry.value) {
transformed[entry.key] = newValue
}
}
return transformed
}
}
var d1: [String: Any?] = ["a": 1, "b": nil]
d1.filterMap { $0 } // ["a": 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment