Created
November 10, 2015 09:51
-
-
Save mkhl/a17247fff806348acf44 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 { | |
mutating func merge(other: [Key: Value], by combine: (Value, Value) -> Value) { | |
for (key, newValue) in other { | |
if let oldValue = self[key] { | |
self[key] = combine(oldValue, newValue) | |
} else { | |
self[key] = newValue | |
} | |
} | |
} | |
func merged(other: [Key: Value], by combine: (Value, Value) -> Value) -> [Key: Value] { | |
var copy = self | |
copy.merge(other, by: combine) | |
return copy | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment