in Ruby there’s a method useful for navigating nested dictionaries called dig()
nested = Dict( :a => Dict( :b => "b", :c => Dict( :d => "d" ) ) ) dig(nested, :a, :b) #=> "b" dig(nested, :a, :c, :d) #=> "d" dig(nested, :a, :e) #=> nothing
I have written this function. The problem is that
args... will produce a Tuple. When called recursively, i will have a Tuple of Tuples and I don’t know how to flatten it or to avoid this. I can fix it by making
args::Tuple and calling like this:
dig(nested, (:a, :b)), but I find this somewhat uglier.
function dig(dict::Dict, args...) if haskey(dict, args) aux = dict[args] if length(args[2:end]) > 0 dig(access, args[2:end]) else aux end end end
How can I “splat” args when maing the recursive call or flatten the Tuple?