Skip to content

Commit

Permalink
rename pop!(vector, idx, [default]) to popat! (#36070)
Browse files Browse the repository at this point in the history
* rename pop!(vector, idx, [default]) to popat!

* popat! : explain what `default` is
  • Loading branch information
rfourquet authored Jun 2, 2020
1 parent be2c643 commit 0164101
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
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).
!!! 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

0 comments on commit 0164101

Please sign in to comment.