@@ -184,36 +184,52 @@ function merge!(d::AbstractDict, others::AbstractDict...)
184
184
end
185
185
186
186
"""
187
- merge!(combine, d::AbstractDict, others::AbstractDict...)
187
+ mergewith!(combine, d::AbstractDict, others::AbstractDict...) -> d
188
+ mergewith!(combine)
189
+ merge!(combine, d::AbstractDict, others::AbstractDict...) -> d
188
190
189
191
Update collection with pairs from the other collections.
190
192
Values with the same key will be combined using the
191
- combiner function.
193
+ combiner function. The curried form `mergewith!(combine)` returns the
194
+ function `(args...) -> mergewith!(combine, args...)`.
195
+
196
+ Method `merge!(combine::Union{Function,Type}, args...)` as an alias of
197
+ `mergewith!(combine, args...)` is still available for backward
198
+ compatibility.
199
+
200
+ !!! compat "Julia 1.5"
201
+ `mergewith!` requires Julia 1.5 or later.
192
202
193
203
# Examples
194
204
```jldoctest
195
205
julia> d1 = Dict(1 => 2, 3 => 4);
196
206
197
207
julia> d2 = Dict(1 => 4, 4 => 5);
198
208
199
- julia> merge !(+, d1, d2);
209
+ julia> mergewith !(+, d1, d2);
200
210
201
211
julia> d1
202
212
Dict{Int64,Int64} with 3 entries:
203
213
4 => 5
204
214
3 => 4
205
215
1 => 6
206
216
207
- julia> merge !(-, d1, d1);
217
+ julia> mergewith !(-, d1, d1);
208
218
209
219
julia> d1
210
220
Dict{Int64,Int64} with 3 entries:
211
221
4 => 0
212
222
3 => 0
213
223
1 => 0
224
+
225
+ julia> foldl(mergewith!(+), [d1, d2]; init=Dict{Int64,Int64}())
226
+ Dict{Int64,Int64} with 3 entries:
227
+ 4 => 5
228
+ 3 => 0
229
+ 1 => 4
214
230
```
215
231
"""
216
- function merge ! (combine:: Function , d:: AbstractDict , others:: AbstractDict... )
232
+ function mergewith ! (combine, d:: AbstractDict , others:: AbstractDict... )
217
233
for other in others
218
234
for (k,v) in other
219
235
d[k] = haskey (d, k) ? combine (d[k], v) : v
@@ -222,6 +238,10 @@ function merge!(combine::Function, d::AbstractDict, others::AbstractDict...)
222
238
return d
223
239
end
224
240
241
+ mergewith! (combine) = (args... ) -> mergewith! (combine, args... )
242
+
243
+ merge! (combine:: Callable , args... ) = mergewith! (combine, args... )
244
+
225
245
"""
226
246
keytype(type)
227
247
@@ -287,12 +307,21 @@ merge(d::AbstractDict, others::AbstractDict...) =
287
307
merge! (_typeddict (d, others... ), others... )
288
308
289
309
"""
310
+ mergewith(combine, d::AbstractDict, others::AbstractDict...)
311
+ mergewith(combine)
290
312
merge(combine, d::AbstractDict, others::AbstractDict...)
291
313
292
314
Construct a merged collection from the given collections. If necessary, the
293
315
types of the resulting collection will be promoted to accommodate the types of
294
316
the merged collections. Values with the same key will be combined using the
295
- combiner function.
317
+ combiner function. The curried form `mergewith(combine)` returns the function
318
+ `(args...) -> mergewith(combine, args...)`.
319
+
320
+ Method `merge(combine::Union{Function,Type}, args...)` as an alias of
321
+ `mergewith(combine, args...)` is still available for backward compatibility.
322
+
323
+ !!! compat "Julia 1.5"
324
+ `mergewith` requires Julia 1.5 or later.
296
325
297
326
# Examples
298
327
```jldoctest
@@ -306,14 +335,20 @@ Dict{String,Int64} with 2 entries:
306
335
"bar" => 4711
307
336
"baz" => 17
308
337
309
- julia> merge (+, a, b)
338
+ julia> mergewith (+, a, b)
310
339
Dict{String,Float64} with 3 entries:
311
340
"bar" => 4753.0
312
341
"baz" => 17.0
313
342
"foo" => 0.0
343
+
344
+ julia> ans == mergewith(+)(a, b)
345
+ true
314
346
```
315
347
"""
316
- merge (combine:: Function , d:: AbstractDict , others:: AbstractDict... ) =
348
+ mergewith (combine, d:: AbstractDict , others:: AbstractDict... ) =
349
+ mergewith! (combine, _typeddict (d, others... ), others... )
350
+ mergewith (combine) = (args... ) -> mergewith (combine, args... )
351
+ merge (combine:: Callable , d:: AbstractDict , others:: AbstractDict... ) =
317
352
merge! (combine, _typeddict (d, others... ), others... )
318
353
319
354
promoteK (K) = K
0 commit comments