Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ New library features
* `x::Signed % Unsigned` and `x::Unsigned % Signed` are supported for integer bitstypes.
* `signed(unsigned_type)` is supported for integer bitstypes, `unsigned(signed_type)` has been supported.
* `accumulate`, `cumsum`, and `cumprod` now support `Tuple` ([#34654]) and arbitrary iterators ([#34656]).
* `pop!(collection, key, [default])` now has a method for `Vector` to remove an element at an arbitrary index ([#35513]).
* In `splice!` with no replacement, values to be removed can now be specified with an
arbitrary iterable (instead of a `UnitRange`) ([#34524]).

Expand Down
15 changes: 15 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,21 @@ function pop!(a::Vector)
return item
end

function pop!(a::Vector, i::Integer)
x = a[i]
_deleteat!(a, i, 1);
x
Copy link
Member

Choose a reason for hiding this comment

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

Maybe

Suggested change
_deleteat!(a, i, 1);
x
_deleteat!(a, i, 1)
return x

?

Copy link
Member Author

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 :)

end

pop!(a::Vector, i::Integer, default) =
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok

if 1 <= i <= length(a)
x = @inbounds a[i]
_deleteat!(a, i, 1)
x
else
default
end

"""
pushfirst!(collection, items...) -> collection

Expand Down
3 changes: 3 additions & 0 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ end
Delete and return the mapping for `key` if it exists in `collection`, otherwise return
`default`, or throw an error if `default` is not specified.

!!! compat "Julia 1.5"
For `collection::Vector`, this method requires at least Julia 1.5.

# Examples
```jldoctest
julia> d = Dict("a"=>1, "b"=>2, "c"=>3);
Expand Down
15 changes: 15 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,21 @@ end
end
@test_throws BoundsError insert!(v, 5, 5)
end

@testset "pop!(::Vector, i, [default])" begin
a = [1, 2, 3, 4]
@test_throws BoundsError pop!(a, 0)
@test pop!(a, 0, "default") == "default"
@test a == 1:4
@test_throws BoundsError pop!(a, 5)
@test pop!(a, 1) == 1
@test a == [2, 3, 4]
@test pop!(a, 2) == 3
@test a == [2, 4]
badpop() = @inbounds pop!([1], 2)
@test_throws BoundsError badpop()
end

@testset "concatenation" begin
@test isequal([fill(1.,2,2) fill(2.,2,1)], [1. 1 2; 1 1 2])
@test isequal([fill(1.,2,2); fill(2.,1,2)], [1. 1; 1 1; 2 2])
Expand Down