Skip to content

Instantly share code, notes, and snippets.

@mkhl
Created November 10, 2015 09:51
Show Gist options
  • Save mkhl/a17247fff806348acf44 to your computer and use it in GitHub Desktop.
Save mkhl/a17247fff806348acf44 to your computer and use it in GitHub Desktop.
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