Skip to content

Commit 3ccc916

Browse files
rfourquetKristofferC
authored andcommitted
rename pop!(vector, idx, [default]) to popat! (#36070)
* rename pop!(vector, idx, [default]) to popat! * popat! : explain what `default` is (cherry picked from commit 0164101)
1 parent 99adb51 commit 3ccc916

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

base/array.jl

+33-2
Original file line numberDiff line numberDiff line change
@@ -1153,13 +1153,44 @@ 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+
When `i` is not a valid index for `a`, return `default`, or throw an error if
1162+
`default` is not specified.
1163+
See also [`deleteat!`](@ref) and [`splice!`](@ref).
1164+
1165+
!!! compat "Julia 1.5"
1166+
This function is available as of Julia 1.5.
1167+
1168+
# Examples
1169+
```jldoctest
1170+
julia> a = [4, 3, 2, 1]; popat!(a, 2)
1171+
3
1172+
1173+
julia> a
1174+
3-element Array{Int64,1}:
1175+
4
1176+
2
1177+
1
1178+
1179+
julia> popat!(a, 4, missing)
1180+
missing
1181+
1182+
julia> popat!(a, 4)
1183+
ERROR: BoundsError: attempt to access 3-element Array{Int64,1} at index [4]
1184+
[...]
1185+
```
1186+
"""
1187+
function popat!(a::Vector, i::Integer)
11571188
x = a[i]
11581189
_deleteat!(a, i, 1)
11591190
x
11601191
end
11611192

1162-
function pop!(a::Vector, i::Integer, default)
1193+
function popat!(a::Vector, i::Integer, default)
11631194
if 1 <= i <= length(a)
11641195
x = @inbounds a[i]
11651196
_deleteat!(a, i, 1)

base/dict.jl

-3
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

+1
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ export
472472
append!,
473473
insert!,
474474
pop!,
475+
popat!,
475476
prepend!,
476477
push!,
477478
resize!,

doc/src/base/collections.md

+1
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

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