@@ -184,36 +184,52 @@ function merge!(d::AbstractDict, others::AbstractDict...)
184184end
185185
186186"""
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
188190
189191Update collection with pairs from the other collections.
190192Values 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.
192202
193203# Examples
194204```jldoctest
195205julia> d1 = Dict(1 => 2, 3 => 4);
196206
197207julia> d2 = Dict(1 => 4, 4 => 5);
198208
199- julia> merge !(+, d1, d2);
209+ julia> mergewith !(+, d1, d2);
200210
201211julia> d1
202212Dict{Int64,Int64} with 3 entries:
203213 4 => 5
204214 3 => 4
205215 1 => 6
206216
207- julia> merge !(-, d1, d1);
217+ julia> mergewith !(-, d1, d1);
208218
209219julia> d1
210220Dict{Int64,Int64} with 3 entries:
211221 4 => 0
212222 3 => 0
213223 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
214230```
215231"""
216- function merge ! (combine:: Function , d:: AbstractDict , others:: AbstractDict... )
232+ function mergewith ! (combine, d:: AbstractDict , others:: AbstractDict... )
217233 for other in others
218234 for (k,v) in other
219235 d[k] = haskey (d, k) ? combine (d[k], v) : v
@@ -222,6 +238,10 @@ function merge!(combine::Function, d::AbstractDict, others::AbstractDict...)
222238 return d
223239end
224240
241+ mergewith! (combine) = (args... ) -> mergewith! (combine, args... )
242+
243+ merge! (combine:: Callable , args... ) = mergewith! (combine, args... )
244+
225245"""
226246 keytype(type)
227247
@@ -287,12 +307,21 @@ merge(d::AbstractDict, others::AbstractDict...) =
287307 merge! (_typeddict (d, others... ), others... )
288308
289309"""
310+ mergewith(combine, d::AbstractDict, others::AbstractDict...)
311+ mergewith(combine)
290312 merge(combine, d::AbstractDict, others::AbstractDict...)
291313
292314Construct a merged collection from the given collections. If necessary, the
293315types of the resulting collection will be promoted to accommodate the types of
294316the 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.
296325
297326# Examples
298327```jldoctest
@@ -306,14 +335,20 @@ Dict{String,Int64} with 2 entries:
306335 "bar" => 4711
307336 "baz" => 17
308337
309- julia> merge (+, a, b)
338+ julia> mergewith (+, a, b)
310339Dict{String,Float64} with 3 entries:
311340 "bar" => 4753.0
312341 "baz" => 17.0
313342 "foo" => 0.0
343+
344+ julia> ans == mergewith(+)(a, b)
345+ true
314346```
315347"""
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... ) =
317352 merge! (combine, _typeddict (d, others... ), others... )
318353
319354promoteK (K) = K
0 commit comments