Created
October 1, 2016 09:44
-
-
Save koher/b47ef1f1e90a65faa643d0376eb33758 to your computer and use it in GitHub Desktop.
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 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