Skip to content

Commit

Permalink
rename pop!(vector, idx, [default]) to popat!
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed May 29, 2020
1 parent 7c980c6 commit 16765b6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
29 changes: 27 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1153,13 +1153,38 @@ 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.
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
5-element Array{Int64,1}:
4
2
1
julia> popat!(a, 4, missing)
missing
```
"""
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 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 16765b6

Please sign in to comment.