Last active
June 14, 2021 09:53
-
-
Save fxm90/5c037048e74e809ab594fe5b88495a71 to your computer and use it in GitHub Desktop.
Combine two dictionaries of same type with custom operator.
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 { | |
/// 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