@@ -1730,13 +1730,13 @@ public struct Dictionary<Key : Hashable, Value> :
1730
1730
/// print(wordToValue)
1731
1731
/// // Prints "["three": 3, "four": 4, "five": 5, "one": 1, "two": 2]"
1732
1732
///
1733
- /// - Parameter keysAndValues: A sequence of `(Key, Value)` tuples to use for
1733
+ /// - Parameter keysAndValues: A sequence of key-value pairs to use for
1734
1734
/// the new dictionary. Every key in `keysAndValues` must be unique.
1735
1735
/// - Returns: A new dictionary initialized with the elements of
1736
1736
/// `keysAndValues`.
1737
1737
public init<S: Sequence>(
1738
1738
uniqueKeysWithValues keysAndValues: S
1739
- ) where S.Iterator. Element == (Key, Value) {
1739
+ ) where S.Element == (Key, Value) {
1740
1740
if let d = keysAndValues as? Dictionary<Key, Value> {
1741
1741
self = d
1742
1742
} else {
@@ -1773,15 +1773,15 @@ public struct Dictionary<Key : Hashable, Value> :
1773
1773
/// // ["b": 4, "a": 3]
1774
1774
///
1775
1775
/// - Parameters:
1776
- /// - keysAndValues: A sequence of `(Key, Value)` tuples to use for the new
1776
+ /// - keysAndValues: A sequence of key-value pairs to use for the new
1777
1777
/// dictionary.
1778
1778
/// - combine: A closure that is called with the values for any duplicate
1779
1779
/// keys that are encountered. The closure returns the desired value for
1780
1780
/// the final dictionary.
1781
1781
public init<S: Sequence>(
1782
1782
_ keysAndValues: S,
1783
1783
uniquingKeysWith combine: (Value, Value) throws -> Value
1784
- ) rethrows where S.Iterator. Element == (Key, Value) {
1784
+ ) rethrows where S.Element == (Key, Value) {
1785
1785
self = Dictionary(minimumCapacity: keysAndValues.underestimatedCount)
1786
1786
try _variantBuffer.merge(keysAndValues, uniquingKeysWith: combine)
1787
1787
}
@@ -1810,8 +1810,8 @@ public struct Dictionary<Key : Hashable, Value> :
1810
1810
/// `values`.
1811
1811
public init<S: Sequence>(
1812
1812
grouping values: S,
1813
- by keyForValue: (S.Iterator. Element) throws -> Key
1814
- ) rethrows where Value == [S.Iterator. Element] {
1813
+ by keyForValue: (S.Element) throws -> Key
1814
+ ) rethrows where Value == [S.Element] {
1815
1815
self = [:]
1816
1816
for value in values {
1817
1817
self[try keyForValue(value), default: []].append(value)
@@ -2093,27 +2093,60 @@ public struct Dictionary<Key : Hashable, Value> :
2093
2093
/// var dictionary = ["a": 1, "b": 2]
2094
2094
///
2095
2095
/// // Keeping existing value for key "a":
2096
- /// dictionary.merge(["a": 3, "c": 4])
2097
- /// { (current, _) in current }
2096
+ /// dictionary.merge(zip(["a", "c"], [3, 4])) { (current, _) in current }
2098
2097
/// // ["b": 2, "a": 1, "c": 4]
2099
2098
///
2100
2099
/// // Taking the new value for key "a":
2101
- /// dictionary.merge(["a": 5, "d": 6])
2102
- /// { (_, new) in new }
2100
+ /// dictionary.merge(zip(["a", "d"], [5, 6])) { (_, new) in new }
2103
2101
/// // ["b": 2, "a": 5, "c": 4, "d": 6]
2104
2102
///
2105
2103
/// - Parameters:
2106
- /// - other: A sequence of `(Key, Value)` tuples .
2104
+ /// - other: A sequence of key-value pairs .
2107
2105
/// - combine: A closure that takes the current and new values for any
2108
2106
/// duplicate keys. The closure returns the desired value for the final
2109
2107
/// dictionary.
2110
2108
public mutating func merge<S: Sequence>(
2111
2109
_ other: S,
2112
2110
uniquingKeysWith combine: (Value, Value) throws -> Value
2113
- ) rethrows where S.Iterator. Element == (Key, Value) {
2111
+ ) rethrows where S.Element == (Key, Value) {
2114
2112
try _variantBuffer.merge(other, uniquingKeysWith: combine)
2115
2113
}
2116
2114
2115
+ /// Merges the given dictionary into this dictionary, using a combining
2116
+ /// closure to determine the value for any duplicate keys.
2117
+ ///
2118
+ /// Use the `combine` closure to select which value to use in the updated
2119
+ /// dictionary, or to combine existing and new values. As the key-values
2120
+ /// pairs in `other` are merged with this dictionary, the `combine` closure
2121
+ /// is called with the current and new values for any duplicate keys that
2122
+ /// are encountered.
2123
+ ///
2124
+ /// This example shows how to choose the current or new values for any
2125
+ /// duplicate keys:
2126
+ ///
2127
+ /// var dictionary = ["a": 1, "b": 2]
2128
+ ///
2129
+ /// // Keeping existing value for key "a":
2130
+ /// dictionary.merge(["a": 3, "c": 4]) { (current, _) in current }
2131
+ /// // ["b": 2, "a": 1, "c": 4]
2132
+ ///
2133
+ /// // Taking the new value for key "a":
2134
+ /// dictionary.merge(["a": 5, "d": 6]) { (_, new) in new }
2135
+ /// // ["b": 2, "a": 5, "c": 4, "d": 6]
2136
+ ///
2137
+ /// - Parameters:
2138
+ /// - other: A dictionary to merge.
2139
+ /// - combine: A closure that takes the current and new values for any
2140
+ /// duplicate keys. The closure returns the desired value for the final
2141
+ /// dictionary.
2142
+ public mutating func merge(
2143
+ _ other: [Key: Value],
2144
+ uniquingKeysWith combine: (Value, Value) throws -> Value) rethrows
2145
+ {
2146
+ try _variantBuffer.merge(
2147
+ other.lazy.map { ($0, $1) }, uniquingKeysWith: combine)
2148
+ }
2149
+
2117
2150
/// Returns a new dictionary created by merging the key-value pairs in the
2118
2151
/// given sequence into the dictionary, using a combining closure to
2119
2152
/// determine the value for any duplicate keys.
@@ -2128,7 +2161,45 @@ public struct Dictionary<Key : Hashable, Value> :
2128
2161
/// duplicate keys:
2129
2162
///
2130
2163
/// let dictionary = ["a": 1, "b": 2]
2164
+ /// let newKeyValues = zip(["a", "b"], [3, 4])
2165
+ ///
2166
+ /// let keepingCurrent = dictionary.merging(newKeyValues) { (current, _) in current }
2167
+ /// // ["b": 2, "a": 1]
2168
+ /// let replacingCurrent = dictionary.merging(newKeyValues) { (_, new) in new }
2169
+ /// // ["b": 4, "a": 3]
2170
+ ///
2171
+ /// - Parameters:
2172
+ /// - other: A sequence of key-value pairs.
2173
+ /// - combine: A closure that takes the current and new values for any
2174
+ /// duplicate keys. The closure returns the desired value for the final
2175
+ /// dictionary.
2176
+ /// - Returns: A new dictionary with the combined keys and values of this
2177
+ /// dictionary and `other`.
2178
+ public func merging<S: Sequence>(
2179
+ _ other: S,
2180
+ uniquingKeysWith combine: (Value, Value) throws -> Value
2181
+ ) rethrows -> [Key: Value] where S.Element == (Key, Value) {
2182
+ var result = self
2183
+ try result._variantBuffer.merge(other, uniquingKeysWith: combine)
2184
+ return result
2185
+ }
2186
+
2187
+ /// Returns a new dictionary created by merging the given dictionary into
2188
+ /// this dictionary, using a combining closure to determine the value for
2189
+ /// any duplicate keys.
2190
+ ///
2191
+ /// Use the `combine` closure to select which value to use in the returned
2192
+ /// dictionary, or to combine existing and new values. As the key-value
2193
+ /// pairs in `other` are merged with this dictionary, the `combine` closure
2194
+ /// is called with the current and new values for any duplicate keys that
2195
+ /// are encountered.
2196
+ ///
2197
+ /// This example shows how to choose the current or new values for any
2198
+ /// duplicate keys:
2199
+ ///
2200
+ /// let dictionary = ["a": 1, "b": 2]
2131
2201
/// let otherDictionary = ["a": 3, "b": 4]
2202
+ ///
2132
2203
/// let keepingCurrent = dictionary.merging(otherDictionary)
2133
2204
/// { (current, _) in current }
2134
2205
/// // ["b": 2, "a": 1]
@@ -2137,16 +2208,16 @@ public struct Dictionary<Key : Hashable, Value> :
2137
2208
/// // ["b": 4, "a": 3]
2138
2209
///
2139
2210
/// - Parameters:
2140
- /// - other: A sequence of `(Key, Value)` tuples .
2211
+ /// - other: A dictionary to merge .
2141
2212
/// - combine: A closure that takes the current and new values for any
2142
2213
/// duplicate keys. The closure returns the desired value for the final
2143
2214
/// dictionary.
2144
2215
/// - Returns: A new dictionary with the combined keys and values of this
2145
2216
/// dictionary and `other`.
2146
- public func merging<S: Sequence> (
2147
- _ other: S ,
2217
+ public func merging(
2218
+ _ other: [Key: Value] ,
2148
2219
uniquingKeysWith combine: (Value, Value) throws -> Value
2149
- ) rethrows -> [Key: Value] where S.Iterator.Element == (Key, Value) {
2220
+ ) rethrows -> [Key: Value] {
2150
2221
var result = self
2151
2222
try result.merge(other, uniquingKeysWith: combine)
2152
2223
return result
@@ -4938,7 +5009,7 @@ internal enum _Variant${Self}Buffer<${TypeParametersDecl}> : _HashBuffer {
4938
5009
internal mutating func nativeMerge<S: Sequence>(
4939
5010
_ keysAndValues: S,
4940
5011
uniquingKeysWith combine: (Value, Value) throws -> Value
4941
- ) rethrows where S.Iterator. Element == (Key, Value) {
5012
+ ) rethrows where S.Element == (Key, Value) {
4942
5013
for (key, value) in keysAndValues {
4943
5014
var (i, found) = asNative._find(key, startBucket: asNative._bucket(key))
4944
5015
@@ -4969,7 +5040,7 @@ internal enum _Variant${Self}Buffer<${TypeParametersDecl}> : _HashBuffer {
4969
5040
internal mutating func merge<S: Sequence>(
4970
5041
_ keysAndValues: S,
4971
5042
uniquingKeysWith combine: (Value, Value) throws -> Value
4972
- ) rethrows where S.Iterator. Element == (Key, Value) {
5043
+ ) rethrows where S.Element == (Key, Value) {
4973
5044
if _fastPath(guaranteedNative) {
4974
5045
try nativeMerge(keysAndValues, uniquingKeysWith: combine)
4975
5046
return
0 commit comments