diff --git a/src/PersistentVector.jl b/src/PersistentVector.jl index 8ec580c..775a15d 100644 --- a/src/PersistentVector.jl +++ b/src/PersistentVector.jl @@ -81,7 +81,14 @@ function assoc(v::PersistentVector{T}, i::Int, el) where T end function pop(v::PersistentVector{T}) where T - if isempty(v.tail) + if length(v) == 1 + return PersistentVector{T}() + end + taillen = length(v.tail) + if taillen == 1 + newtail = peek(v.trie) + PersistentVector{T}(pop(v.trie), newtail, v.length - 1) + elseif taillen == 0 newtail = peek(v.trie)[1:end-1] PersistentVector{T}(pop(v.trie), newtail, v.length - 1) else diff --git a/test/PersistentVectorTest.jl b/test/PersistentVectorTest.jl index c70bbb4..25a2fa8 100644 --- a/test/PersistentVectorTest.jl +++ b/test/PersistentVectorTest.jl @@ -131,5 +131,16 @@ end @test convert(PersistentVector{Int}, pv) === pv @test typeof(convert(PersistentVector{Float64}, pv)) == PersistentVector{Float64} end + + @testset "pop" begin + p = pvec(1:33) + @test length(pop(p).tail) == 32 + + p = pvec(1:100) + for _=1:100 + p = pop(p) + end + @test p == pvec{Int}() + end end