-
-
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
add pop!(vector, idx, [default]) #35513
Conversation
base/array.jl
Outdated
_deleteat!(a, i, 1); | ||
x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
_deleteat!(a, i, 1); | |
x | |
_deleteat!(a, i, 1) | |
return x |
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will make a compromise and remove the useless ;
while not adding return
;-)
Adding return
in long function form is not yet officially recommended and Base
is full of cases without return
, so I will contribute to support this lovely style here :)
base/array.jl
Outdated
x | ||
end | ||
|
||
pop!(a::Vector, i::Integer, default) = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boring comment but this looks like it should be a full length function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
* origin/master: (833 commits) Improve typesubtract for tuples (#35600) Make searchsorted*/findnext/findprev return values of keytype (#32978) fix buggy rand(RandomDevice(), Bool) (#35590) remove `Ref` allocation on task switch (#35606) Revert "partr: fix multiqueue resorting stability" (#35589) exclude types with free variables from `type_morespecific` (#35555) fix small typo in NEWS.md (#35611) enable inline allocation of structs with pointers (#34126) SparseArrays: Speed up right-division by diagonal matrices (#35533) Allow hashing 1D OffsetArrays NEWS item for introspection macros (#35594) Special case empty covec-diagonal-vec product (#35557) [GCChecker] fix a few tests by looking through casts Use norm instead of abs in generic lu factorization (#34575) [GCChecker,NFC] run clang-format -style=llvm [GCChecker] fix tests and add Makefile Add introspection macros support for dot syntax (#35522) Specialize `union` for `OneTo` (#35577) add pop!(vector, idx, [default]) (#35513) bump Pkg version (#35584) ...
I'm having after-thougths on this one, because of the Also, I remembered that |
It's an interesting case. (I'm actually not completely convinced that distinction is essential, but that's our API at least until 1.x.) |
Note that there is a separate docstring for
It can easily be interpreted as working for |
This is one of those features that arguably should not exist in Python. The performance is linear where many coders expect constant time for a pop operation. |
I find this totally subjective. There are situtation when you need an O(n) operation, and this is such a case. I'm not against finding another name for this operation, e.g. like I suggested |
Am I right that it is O(1) if popping from the end? It is subjective as you say. I was just reporting my experience teaching python coding. |
Yes fortunately :) And indeed, I agree that it can be surprising to novice programmers, and I also know that you are not alone in thinking O(n) |
That is what causes the confusion. Lots of python students at least just can't imagine how these things are implemented. To be fair to them, I don't know if it is O(1) time if popping the second last element without reading the source. It could be O(1) if removing the first element too but I assume not. |
From triage: we're not that concerned about the performance issue but we're pretty concerned about the semantic distinction between what |
Given that this is on a release branch already, probably best to rename it to |
This removes and return an element at an arbitrary position of a vector, and seems to be compatible with the docstring for
pop!(collection, key[, default])
.Python has it, and I needed it today, which seemed good enough arguments for me to add this :)