-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Method for dropping an element of NamedTuple #27574
Comments
Current workaround: julia> d = (solver = 2, b = 3, c = 4)
(solver = 2, b = 3, c = 4)
julia> inter = [p for p in pairs(d) if p[1] != :solver]
2-element Array{Pair{Symbol,Int64},1}:
:b => 3
:c => 4
julia> f = (; inter...)
(b = 3, c = 4) (answered on Slack, I think by araslan) |
julia> function dropnames(namedtuple::NamedTuple, names::Tuple{Vararg{Symbol}})
keepnames = Base.diff_names(Base._nt_names(namedtuple), names)
return NamedTuple{keepnames}(namedtuple)
end
dropnames (generic function with 1 method)
julia> d = (solver = 2, b = 3, c = 4)
(solver = 2, b = 3, c = 4)
julia> dropnames(d, (:solver,))
(b = 3,)
julia> dropnames(d, (:b,))
(solver = 2,)
julia> dropnames(d, (:b,:solver))
NamedTuple()
julia> dropnames(d, (:oops,))
(solver = 2, b = 3)
|
Very nice! Can it also become part of Base or I am being too greedy? |
Base has my permission. |
Is the inference failure OK (everything becomes I would imagine that most use-cases could get away with |
There are some utilities for implementing this in base/namedtuple.jl. If |
|
@JeffreySarnoff Will you do a PR for this? (I really hope that this is in the next beta :P ) |
I don't know what to do about this: julia> function dropnames(namedtuple::NamedTuple, names::Tuple{Vararg{Symbol}})
keepnames = Base.diff_names(Base._nt_names(namedtuple), names)
return NamedTuple{keepnames}(namedtuple)
end
julia> code_warntype(dropnames, (NamedTuple, Tuple{Vararg{Symbol}}))
Body::Any
1 1 ─ %1 = Base.diff_names::typeof(Base.diff_names) │
│ %2 = Base._nt_names::typeof(Base._nt_names) │
│ %3 = %2(%%namedtuple)::Any │
│ %4 = %1(%3, %%names)::Tuple{Vararg{Symbol,N} where N} │
2 │ %5 = Core.apply_type(Main.NamedTuple, %4)::Type{NamedTuple{_1,T} where T<:Tuple} where _1
│ %6 = %5(%%namedtuple)::Any │
└── return %6 │
|
That happens because |
submitted PR #27725 |
Looks like the PR that implemented this got approved after some debate about naming, but then never merged |
+1 reopening and utilizing that PR |
So in the end it seems the decision was not to merge #27725 (we should use NamedTupleTools.delete instead). If that's the consensus, this issue should be closed. Otherwise, the PR should be reopened (I'd vote for this one!). |
Adding triage label, see #27725 (comment) |
Triage discussed this (summary: yes) #27725 (comment) |
closed by #55270 |
I am trying to do:
The JuliaData/NamedTuples has a
delete
method, so one can do something likef = delete(d, :solver)
. It would be nice to have it forCore.NamedTuple
as well, if possible.The text was updated successfully, but these errors were encountered: