JeffreySarnoff/NamedTupleTools.jl

getkeys

cscherrer opened this issue · 9 comments

Here's one I've found useful - is this already implemented somehow or would you like a PR?

julia> function getkeys(nt::NamedTuple, ks)
           namedtuple(ks)((nt[k] for k in ks)...)
       end
getkeys (generic function with 1 method)

julia> nt = (a=1,b=2,c=3,d=4)
(a = 1, b = 2, c = 3, d = 4)

julia> getkeys(nt, (:a,:c))
(a = 1, c = 3)

ahh get a subset of the keys -- sure PR it

thank you, could not find it easily, so do PR this

call it getfromkeys or bykeys or ?? because getkeys is misleading

getkeys is misleading

Yeah I wasn't sure what to call it, so this was analolgous to getindex. But getkey returns a key so yeah that's confusing.

I think the "right" way to do this is with getindex, so you could write nt[[:a,:c]]. But, type piracy and all... :)

yes -- I already ran into piracy and had to rewrite much of this -- so do not go down that path
.. I had a way to do this ..

here it is (just the opposite logic)


julia> nt
(a = 5, b = 6, c = 7, d = 8)

julia> delete(nt, (:a, :c))
(b = 6, d = 8)

select(nt, (:b, :d)) == delete(nt, (:a, :c))

and there is setdiff so ? copy the nt do the setdiff on the keys and delete those keys (shorthand)

(that does seem a long way around)

Are you suggesting calling it select? That sounds good to me