Skip to content
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
6 changes: 5 additions & 1 deletion base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ zip_iteratoreltype() = HasEltype()
zip_iteratoreltype(a) = a
zip_iteratoreltype(a, tail...) = and_iteratoreltype(a, zip_iteratoreltype(tail...))

last(z::Zip) = getindex.(z.is, minimum(Base.map(lastindex, z.is)))
last(z::Zip) = nth(z, length(z))

function reverse(z::Zip)
if !first(_zip_lengths_finite_equal(z.is))
throw(ArgumentError("Cannot reverse zipped iterators of unknown, infinite, or unequal lengths"))
Expand Down Expand Up @@ -1700,6 +1701,9 @@ function _nth(::IteratorSize, itr, n::Integer)
y === nothing && throw(BoundsError(itr, n))
y[1]
end

_nth(::IteratorSize, z::Zip, n::Integer) = Base.map(nth(n), z.is)

"""
nth(n::Integer)

Expand Down
28 changes: 26 additions & 2 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ using Random
using Base: IdentityUnitRange
using Dates: Date, Day

isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
using .Main.OffsetArrays

@test (@inferred Base.IteratorSize(Any)) isa Base.SizeUnknown

# zip and filter iterators
Expand Down Expand Up @@ -1140,7 +1143,6 @@ end
end

@testset "nth" begin

Z = Array{Int,0}(undef)
Z[] = 17
it_result_pairs = Dict(
Expand Down Expand Up @@ -1170,7 +1172,6 @@ end
(Iterators.cycle(((),)), 1000) => ()
)


@testset "iter: $IT" for (IT, n) in keys(it_result_pairs)
@test it_result_pairs[(IT, n)] == nth(IT, n)
@test_throws BoundsError nth(IT, -42)
Expand Down Expand Up @@ -1206,3 +1207,26 @@ end
@test Base.infer_return_type((Vector{Any},)) do xs
[x for x in xs if x isa Int]
end == Vector{Int}

@testset "issue #58922" begin
# `last` short circuits correctly
@test last(zip(1:10, 2:11)) == (10, 11) # same length
@test last(zip(1:3, 2:11)) == (3, 4) # different length

# Finite-guarded zip iterator: one iterator bounded and the other is not
@test last(zip(1:3, Iterators.countfrom(2))) == (3, 4)
@test last(zip(1:3, Iterators.cycle(('x', 'y')))) == (3, 'x')
@test last(zip(1:3, Iterators.repeated('x'))) == (3, 'x')
@test last(zip(OffsetArray(1:10, 2), OffsetArray(1:10, 3))) == (10, 10)

# Cannot statically know length of zipped iterator if any of its components are of
# unknown length
@test_throws MethodError last(zip(1:3, Iterators.filter(x -> x > 0, -5:5))) # (3, 3)
@test_throws MethodError last(zip(Iterators.filter(x -> x > 0, -5:5), 1:3)) # (3, 3)
@test_throws MethodError last(zip(1:10, Iterators.filter(x -> x > 0, -5:5))) # (5, 5)

# We also can't know the length of zipped iterators when all constituents are of an
# unknown length. In this test, the answer is (5, 4), but we can't know that without
# a greedy algorithm
@test_throws MethodError last(zip(Iterators.filter(x -> x > 0, -5:5), Iterators.filter(x -> x % 2 == 0, -5:5))) # (5, 4)
end