Reputation: 452
In the same way as push!(collection, items...) -> collection
allows to add multiple items in one operation I want to pop!
n
items at once.
I am looking for this behavior
function popMany!(vector, n)
result = vector[1:n]
deleteat!(vector, 1:n)
return result
end
I found the related issue, but in contrast to the push!
variations, pop!(collection, n)
was never implemented.
Also popat!.(1:9,1:3)
throws DimensionMismatch
. popfirst!
does not support additional arguments.
Is there already a function to pop multiple / many / a range of / indexed items?
Upvotes: 1
Views: 1117
Reputation: 2838
Is there already a function to pop multiple / many / a range of / indexed items?
splice!
?
Upvotes: 4
Reputation: 216
I'm not aware of a built-in function that does this automatically, but you can write a short one-liner using a comprehension:
julia> pop_many!(x, n) = [pop!(x) for _ in 1:n]
pop_many! (generic function with 1 method)
julia> x = rand(5)
5-element Array{Float64,1}:
0.14855795213861422
0.18593252832228968
0.3353192149050159
0.4537045868321117
0.3912440300789175
julia> pop_many!(x, 3)
3-element Array{Float64,1}:
0.3912440300789175
0.4537045868321117
0.3353192149050159
julia> x
2-element Array{Float64,1}:
0.14855795213861422
0.18593252832228968
Upvotes: 4