Skip to content

Commit

Permalink
[3.x] add popat! (#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Jun 1, 2022
1 parent 95ea27f commit 952300c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
@@ -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
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
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
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

2 comments on commit 952300c

@martinholters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/61629

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.45.0 -m "<description of version>" 952300c15c5494e52f49c885d4d1b3de0d2e162e
git push origin v3.45.0

Please sign in to comment.