Skip to content
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

delete for NamedTuples #27725

Closed
wants to merge 36 commits into from
Closed

delete for NamedTuples #27725

wants to merge 36 commits into from

Conversation

JeffreySarnoff
Copy link
Contributor

@JeffreySarnoff JeffreySarnoff commented Jun 22, 2018

Given a named tuple and a tuple of field names to omit, provides the redacted named tuple.
see #27574 (comment)

@fredrikekre
Copy link
Member

Shouldn't this be a method of deleteat?

@JeffreySarnoff
Copy link
Contributor Author

JeffreySarnoff commented Jun 22, 2018

deleteat! exists in Base, deleteat does not. deleteat! seems inappropriate because there is no inplace activity. Do you think it is worth introducing deleteat? Is deletefields or deletenames better than dropnames?

@JeffreySarnoff
Copy link
Contributor Author

the appveyor build ran out of time or memory

@andyferris
Copy link
Member

+1 for delete (a named tuple is more like a dict than array) or deleteat existing.

@JeffreySarnoff
Copy link
Contributor Author

JeffreySarnoff commented Jun 22, 2018

I like delete, too.
I forked this on github, and have no local copy. If I edit the fork and do nothing else, does this PR update automatically, or is there something more required?

@Datseris
Copy link

Yes.

I think an export statement would also be nice?

@JeffreySarnoff
Copy link
Contributor Author

JeffreySarnoff commented Jun 22, 2018

name changed to delete
docstring revised accordingly
tests revised to test delete
delete added to exports.jl (under collections)

@@ -280,6 +280,16 @@ get(f::Callable, nt::NamedTuple, key::Union{Integer, Symbol}) = haskey(nt, key)
(names...,)
end

"""
delete(nt::NamedTuple{an}, omitnames::Tuple{Vararg{Symbol}}) where {an}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JeffreySarnoff do you think it is really necessary to give the parametric type in the named tuple? I don't think the {an} where {an} adds something and makes the docstring longer and less readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the form used in another NamedTuple function. The an captures the names directly, rather than requiring a separate function call.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am referring to the documentation string. Should the user care about this implementation detail? Isn't it anyway clear that a named tuple is parametricaly defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really, though for the docstring could be given without that and still be valid -- just not matching
I have no idea if that is frowned upon.

"""
    delete(nt::NamedTuple, omitnames::Tuple{Varag{Symbol}})
"""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will shorten the doc string -- and if there are voices to put it back -- I will do that.

@JeffBezanson
Copy link
Sponsor Member

I think there should be a method of this that accepts a single symbol, which is more compatible with existing delete! methods.

@JeffreySarnoff
Copy link
Contributor Author

What if it took one or more symbols? That is the way I had first thought of it.

@JeffreySarnoff
Copy link
Contributor Author

There gets to be a time cost with needing to call this twice or more to remove multiple named values.

@JeffreySarnoff
Copy link
Contributor Author

oh, misread -- addressed

@fredrikekre fredrikekre changed the title dropnames for NamedTuples delete for NamedTuples Jun 22, 2018
@JeffreySarnoff
Copy link
Contributor Author

ci/circleci has been pending for ~6 hours "waiting for jobs [your running builds] to finish", there do not appear to be any running builds for these checks.

@JeffreySarnoff
Copy link
Contributor Author

It says circleci checks failed -- does this have something to do with the changes I suggest?
Here is the end of the log.


Sysimage built. Summary:
Total ─────── 433.265285 seconds 
Base: ───────  98.153646 seconds 22.6544%
Stdlibs: ──── 294.759869 seconds 68.0322%
Precompile: ─  40.347553 seconds 9.31244%
Killed
*** This error is usually fixed by running `make clean`. If the error persists, try `make cleanall`. ***
Makefile:201: recipe for target '/home/circleci/project/usr/lib/julia/sys-o.a' failed
make[1]: *** [/home/circleci/project/usr/lib/julia/sys-o.a] Error 1
Makefile:78: recipe for target 'julia-sysimg-release' failed
make: *** [julia-sysimg-release] Error 2
make: *** Waiting for unfinished jobs....
    LINK usr/lib/julia/sys-debug.so
Exited with code 2

"""
delete(nt::NamedTuple, omitnames::Tuple{Vararg{Symbol}})

Construct a copy of a named tuple `nt`, omitting the fields named in `omitnames`.
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps copy doesn't have to be mentioned since tuples are immutable?

@@ -201,6 +201,15 @@ end
abstr_nt_22194_3()
@test Base.return_types(abstr_nt_22194_3, ()) == Any[Any]

@test Base.delete((a=1, b=2), (:b,)) == (a=1,)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I read that the result here should be inferrable? If so, should these test also check that?

Copy link
Contributor Author

@JeffreySarnoff JeffreySarnoff Jun 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I test that? Guessed this for that.
@test typeof(delete((a=1.0, b="two"), :b)) == NamedTuple{(:a,), Tuple{Float64}}

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@inferred should work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also Base. isn't needed, right?

@JeffreySarnoff
Copy link
Contributor Author

@KristofferC thought to test @inferred is on point. The named tuple functions themselves are inferrable, but wrapped inside a function that goes away.

 julia> using Test

julia> nt = (a=1, b="2", c=3.0)
(a = 1, b = "2", c = 3.0)

julia> keepnames = (:a, :c)
(:a, :c)

julia> NamedTuple{keepnames}(nt)
(a = 1, c = 3.0)

julia> @inferred NamedTuple{keepnames}(nt)
(a = 1, c = 3.0)

julia> function wrap(keepnames::NTuple{N,Symbol}, nt::NamedTuple{nms}) where {N, nms}
           NamedTuple{keepnames}(nt)
       end
wrap (generic function with 2 methods)

julia> wrap(keepnames, nt)
(a = 1, c = 3.0)

julia> @inferred wrap(keepnames, nt)
ERROR: return type NamedTuple{(:a, :c),Tuple{Int64,Float64}} does not match inferred return type Any
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] top-level scope at none:0

The same thing happens with structdiff.

@JeffreySarnoff
Copy link
Contributor Author

fyi there is delete for NamedTuples in (NamedTupleTools.jl)[https://github.com/JeffreySarnoff/NamedTupleTools.jl]

@pierzy
Copy link

pierzy commented Mar 7, 2019

Thanks! I will use the suggested solution (NamedTupleTools.jl). Will it ever become part of Base?

@JeffreySarnoff
Copy link
Contributor Author

not my call

@arnavs
Copy link
Contributor

arnavs commented Apr 25, 2019

FWIW, I just added NamedTupleTools to a project for precisely this function.

@JeffreySarnoff thanks for struggling valiantly with this PR. @StefanKarpinski - it looks like Jeff has complied with triage's decision. From an outside perspective, it looks worth re-opening.

@pablosanjose
Copy link
Contributor

One more vote towards reopening and reconsidering this for Base

@rafaqz
Copy link
Contributor

rafaqz commented Mar 17, 2022

It would be good to have this in Base to also have the delete method to extend for any immutable objects.

@rafaqz
Copy link
Contributor

rafaqz commented Mar 17, 2022

Can we reopen this? Now that getindex also accepts a Tuple of Symbol, that shouldn't be controversial to do here.

@KronosTheLate
Copy link
Contributor

+1 on reopening this.

@JeffreySarnoff
Copy link
Contributor Author

+𝟐 on this early gift for the New Year

@tomchor
Copy link
Contributor

tomchor commented Feb 2, 2023

+1 for reopening this

@3f6a
Copy link

3f6a commented Aug 23, 2023

reopen this?

@brenhinkeller
Copy link
Sponsor Contributor

Hmm, not clear if the discussion ever went back to triage after Jeffrey's revisions. I'll add a triage label here and then hopefully it can either be discussed again – or if it already was someone can report back here (or in #27574).

Note though:
Screenshot 2023-08-23 at 6 35 07 PM

@brenhinkeller brenhinkeller added the status:triage This should be discussed on a triage call label Aug 23, 2023
@JeffreySarnoff
Copy link
Contributor Author

do not read anything into the fact that my submitting branch was deleted

@KronosTheLate
Copy link
Contributor

I am yet to see any reason why this issue is closed, and there is clearly a demand for this functionality. Would whoever makes the calls enlighten users as for why this has not been added?

@LilithHafner LilithHafner removed the status:triage This should be discussed on a triage call label Oct 27, 2023
@LilithHafner
Copy link
Member

#51098 is already tagged for triage

@JeffBezanson
Copy link
Sponsor Member

I don't know why this was closed; I approve this PR.

@LilithHafner
Copy link
Member

Triage says merge this PR.

Copy the diff to a new PR if GitHub won't let us merge this.

@3f6a
Copy link

3f6a commented Nov 28, 2023

Anyone taking this up in a new PR? Or I can just do it (copying the diff here as suggested by Lilith)

@JeffreySarnoff
Copy link
Contributor Author

JeffreySarnoff commented Nov 28, 2023 via email

@LilithHafner
Copy link
Member

Triage specifically approved the diff in this PR (and not the second PR #51098 (comment))

Yes, @3f6a, you can do it!

@CedricTravelletti
Copy link

+1 for reopening

@LilithHafner
Copy link
Member

This PR cannot be reopened right not for technical reasons, but if you would like to see this happen you may open a new PR with the same content.

JeffBezanson added a commit that referenced this pull request Jul 26, 2024
from #27725

Co-authored-by: Jeffrey Sarnoff <[email protected]>
JeffBezanson added a commit that referenced this pull request Jul 26, 2024
from #27725

Co-authored-by: Jeffrey Sarnoff <[email protected]>
JeffBezanson added a commit that referenced this pull request Jul 31, 2024
from #27725

Co-authored-by: Jeffrey Sarnoff <[email protected]>
lazarusA pushed a commit to lazarusA/julia that referenced this pull request Aug 17, 2024
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.