Skip to content

Commit afab896

Browse files
committed
rename pop!(vector, idx, [default]) to popat!
1 parent 7c980c6 commit afab896

5 files changed

Lines changed: 36 additions & 12 deletions

File tree

base/array.jl

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,13 +1153,38 @@ function pop!(a::Vector)
11531153
return item
11541154
end
11551155

1156-
function pop!(a::Vector, i::Integer)
1156+
"""
1157+
popat!(a::Vector, i::Integer, [default])
1158+
1159+
Remove the item at the given `i` and return it. Subsequent items
1160+
are shifted to fill the resulting gap.
1161+
See also [`deleteat!`](@ref) and [`splice!`](@ref).
1162+
1163+
!!! compat "Julia 1.5"
1164+
This function is available as of Julia 1.5.
1165+
1166+
# Examples
1167+
```jldoctest
1168+
julia> a = [4, 3, 2, 1]; popat!(a, 2)
1169+
3
1170+
1171+
julia> a
1172+
3-element Array{Int64,1}:
1173+
4
1174+
2
1175+
1
1176+
1177+
julia> popat!(a, 4, missing)
1178+
missing
1179+
```
1180+
"""
1181+
function popat!(a::Vector, i::Integer)
11571182
x = a[i]
11581183
_deleteat!(a, i, 1)
11591184
x
11601185
end
11611186

1162-
function pop!(a::Vector, i::Integer, default)
1187+
function popat!(a::Vector, i::Integer, default)
11631188
if 1 <= i <= length(a)
11641189
x = @inbounds a[i]
11651190
_deleteat!(a, i, 1)

base/dict.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,6 @@ end
576576
Delete and return the mapping for `key` if it exists in `collection`, otherwise return
577577
`default`, or throw an error if `default` is not specified.
578578
579-
!!! compat "Julia 1.5"
580-
For `collection::Vector`, this method requires at least Julia 1.5.
581-
582579
# Examples
583580
```jldoctest
584581
julia> d = Dict("a"=>1, "b"=>2, "c"=>3);

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ export
473473
append!,
474474
insert!,
475475
pop!,
476+
popat!,
476477
prepend!,
477478
push!,
478479
resize!,

doc/src/base/collections.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ Partially implemented by:
269269
```@docs
270270
Base.push!
271271
Base.pop!
272+
Base.popat!
272273
Base.pushfirst!
273274
Base.popfirst!
274275
Base.insert!

test/arrayops.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,17 +467,17 @@ end
467467
@test_throws BoundsError insert!(v, 5, 5)
468468
end
469469

470-
@testset "pop!(::Vector, i, [default])" begin
470+
@testset "popat!(::Vector, i, [default])" begin
471471
a = [1, 2, 3, 4]
472-
@test_throws BoundsError pop!(a, 0)
473-
@test pop!(a, 0, "default") == "default"
472+
@test_throws BoundsError popat!(a, 0)
473+
@test popat!(a, 0, "default") == "default"
474474
@test a == 1:4
475-
@test_throws BoundsError pop!(a, 5)
476-
@test pop!(a, 1) == 1
475+
@test_throws BoundsError popat!(a, 5)
476+
@test popat!(a, 1) == 1
477477
@test a == [2, 3, 4]
478-
@test pop!(a, 2) == 3
478+
@test popat!(a, 2) == 3
479479
@test a == [2, 4]
480-
badpop() = @inbounds pop!([1], 2)
480+
badpop() = @inbounds popat!([1], 2)
481481
@test_throws BoundsError badpop()
482482
end
483483

0 commit comments

Comments
 (0)