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

rename pop!(vector, idx, [default]) to popat! #36070

Merged
merged 2 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 33 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1153,13 +1153,44 @@ function pop!(a::Vector)
return item
end

function pop!(a::Vector, i::Integer)
"""
popat!(a::Vector, i::Integer, [default])

Remove the item at the given `i` and return it. Subsequent items
are shifted to fill the resulting gap.
When `i` is not a valid index for `a`, return `default`, or throw an error if
`default` is not specified.
See also [`deleteat!`](@ref) and [`splice!`](@ref).
rfourquet marked this conversation as resolved.
Show resolved Hide resolved

!!! compat "Julia 1.5"
This function is available as of Julia 1.5.

# Examples
```jldoctest
julia> a = [4, 3, 2, 1]; popat!(a, 2)
3

julia> a
3-element Array{Int64,1}:
4
2
1

julia> popat!(a, 4, missing)
missing

julia> popat!(a, 4)
ERROR: BoundsError: attempt to access 3-element Array{Int64,1} at index [4]
[...]
```
"""
function popat!(a::Vector, i::Integer)
x = a[i]
_deleteat!(a, i, 1)
x
end

function pop!(a::Vector, i::Integer, default)
function popat!(a::Vector, i::Integer, default)
if 1 <= i <= length(a)
x = @inbounds a[i]
_deleteat!(a, i, 1)
Expand Down
3 changes: 0 additions & 3 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,6 @@ 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
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ export
append!,
insert!,
pop!,
popat!,
prepend!,
push!,
resize!,
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ Partially implemented by:
```@docs
Base.push!
Base.pop!
Base.popat!
Base.pushfirst!
Base.popfirst!
Base.insert!
Expand Down
14 changes: 7 additions & 7 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,17 +467,17 @@ end
@test_throws BoundsError insert!(v, 5, 5)
end

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

Expand Down