Skip to content

Commit 9856827

Browse files
committed
Support last for finite-guarded zip iterators
Inconsistent behaviour for `last(::Zip)` with differing iterator size types causes `MethodError`. `last` works with finite zipped iterators of different length, but fails when one of them is an infinite iterator. The desired behaviour is to match the behaviour of `last` with finite iterators of different lengths. Closes #58922
1 parent 655ef5a commit 9856827

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

base/iterators.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ zip_iteratoreltype() = HasEltype()
473473
zip_iteratoreltype(a) = a
474474
zip_iteratoreltype(a, tail...) = and_iteratoreltype(a, zip_iteratoreltype(tail...))
475475

476-
last(z::Zip) = getindex.(z.is, minimum(Base.map(lastindex, z.is)))
476+
last(z::Zip) = nth(z, length(z))
477+
477478
function reverse(z::Zip)
478479
if !first(_zip_lengths_finite_equal(z.is))
479480
throw(ArgumentError("Cannot reverse zipped iterators of unknown, infinite, or unequal lengths"))
@@ -1700,6 +1701,9 @@ function _nth(::IteratorSize, itr, n::Integer)
17001701
y === nothing && throw(BoundsError(itr, n))
17011702
y[1]
17021703
end
1704+
1705+
_nth(::Union{HasShape, HasLength}, z::Zip, n::Integer) = Base.map(nth(n), z.is)
1706+
17031707
"""
17041708
nth(n::Integer)
17051709

0 commit comments

Comments
 (0)