From 16765b6bc63e19fb2686d860f7b10174fd666b32 Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Fri, 29 May 2020 11:36:38 +0200 Subject: [PATCH] rename pop!(vector, idx, [default]) to popat! --- base/array.jl | 29 +++++++++++++++++++++++++++-- base/dict.jl | 3 --- doc/src/base/collections.md | 1 + test/arrayops.jl | 14 +++++++------- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/base/array.jl b/base/array.jl index d52997bf7af539..a9d098d6aa150d 100644 --- a/base/array.jl +++ b/base/array.jl @@ -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) diff --git a/base/dict.jl b/base/dict.jl index 65eacd9b1962ba..1760e62338cf8a 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -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); diff --git a/doc/src/base/collections.md b/doc/src/base/collections.md index fae621b302871d..383dbcda4f93ef 100644 --- a/doc/src/base/collections.md +++ b/doc/src/base/collections.md @@ -269,6 +269,7 @@ Partially implemented by: ```@docs Base.push! Base.pop! +Base.popat! Base.pushfirst! Base.popfirst! Base.insert! diff --git a/test/arrayops.jl b/test/arrayops.jl index 6e915f8c83e9c4..9ecd9f4a0bab29 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -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