Skip to content

Instantly share code, notes, and snippets.

@fxm90
Last active June 14, 2021 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fxm90/5c037048e74e809ab594fe5b88495a71 to your computer and use it in GitHub Desktop.
Save fxm90/5c037048e74e809ab594fe5b88495a71 to your computer and use it in GitHub Desktop.
Combine two dictionaries of same type with custom operator.
extension Dictionary {
/// Combine two dictionaries of same type with `+` operator.
///
/// - Important: When `left` and `right` are having a same key, the value from `right` will override
/// the value from `left`. E.g.
/// ```
/// var left = ["a": 1, "b": 2]
/// let right = ["a": 3, "c": 4]
/// left += right
/// print(left)
/// // ["a": 3, "b": 2, "c": 4,]
/// ```
static func += (left: inout [Key: Value], right: [Key: Value]) {
left.merge(right, uniquingKeysWith: { _, rightVal in rightVal })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment