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

[3.x] add popat! #774

Merged
merged 3 commits into from
Jun 1, 2022
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.44.0"
version = "3.45.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ changes in `julia`.

## Supported features

* `popat!` removes the item at the given `i` and returns it ([#36070]). (since
Compat 3.45.0)

* `keepat!` removes the items at all the indices which are not given and returns
the modified source ([#36229], [#42351]). (since Compat 3.44.0, 4.1.0)

Expand Down Expand Up @@ -298,6 +301,7 @@ Note that you should specify the correct minimum version for `Compat` in the
[#29790]: https://github.com/JuliaLang/julia/pull/29790
[#38449]: https://github.com/JuliaLang/julia/pull/38449
[#36018]: https://github.com/JuliaLang/julia/pull/36018
[#36070]: https://github.com/JuliaLang/julia/pull/36070
[#36199]: https://github.com/JuliaLang/julia/pull/36199
[#37454]: https://github.com/JuliaLang/julia/pull/37454
[#40729]: https://github.com/JuliaLang/julia/pull/40729
Expand Down
21 changes: 21 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,27 @@ end
end
end

# This function is available as of Julia 1.5.
@static if !isdefined(Base, :popat!)
export popat!

function popat!(a::Vector, i::Integer)
x = a[i]
Base._deleteat!(a, i, 1)
x
end

function popat!(a::Vector, i::Integer, default)
if 1 <= i <= length(a)
x = @inbounds a[i]
Base._deleteat!(a, i, 1)
x
else
default
end
end
end

include("iterators.jl")
include("deprecated.jl")

Expand Down
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1451,3 +1451,18 @@ end
keepat!(ea, Bool[])
@test isempty(ea)
end

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