Julia recursive deep copy and merge for nested dicts
Julia
function deep_clone(x)
x isa AbstractDict && return Dict(k => deep_clone(v) for (k, v) in x)
x isa AbstractVector && return [deep_clone(v) for v in x]
return x
end
function deep_merge(base, patch)
out = deep_clone(base)
for (k, v) in patch
if haskey(out, k) && out[k] isa AbstractDict && v isa AbstractDict
out[k] = deep_merge(out[k], v)
else
out[k] = deep_clone(v)
end
end
return out
end
base = Dict("db" => Dict("host" => "localhost", "port" => 5432))
println(deep_merge(base, Dict("db" => Dict("port" => 6543))))