Skip to content

Commit

Permalink
Added recursive merge function for NamedTuple, addresses JuliaLang#29215
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsamaroo committed Sep 18, 2018
1 parent 8dd3326 commit fbe6d28
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions base/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@ merge(a::NamedTuple{()}, b::NamedTuple) = b

merge(a::NamedTuple, b::Iterators.Pairs{<:Any,<:Any,<:Any,<:NamedTuple}) = merge(a, b.data)

"""
merge(a::NamedTuple, b::NamedTuple)
Perform a recursive merge of two or more named tuples.
```jldoctest
julia> merge((a=1, b=2), (b=3, c=(f=1)), (c=(f=2),))
(a = 1, b = 3, c = (d = 2,))
```
"""
merge(a::NamedTuple, b::NamedTuple...) = merge(merge(a, b[1]), b[2:end]...)

"""
merge(a::NamedTuple, iterable)
Expand Down
3 changes: 3 additions & 0 deletions test/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,6 @@ y = map(v -> (a=v.a, b=v.a + v.b), [(a=1, b=missing), (a=1, b=2)])
# Iterator constructor
@test NamedTuple{(:a, :b), Tuple{Int, Float64}}(Any[1.0, 2]) === (a=1, b=2.0)
@test NamedTuple{(:a, :b)}(Any[1.0, 2]) === (a=1.0, b=2)

# Recursive merge, issue #29215
@test merge((a=1, b=2), (b=3, c=(d=1,)), (c=(d=2,),)) === (a=1, b=3, c=(d=2,))

0 comments on commit fbe6d28

Please sign in to comment.