Skip to content

Commit fd30157

Browse files
committed
Support last for finite-guarded zip iterators
Inconsistent behaviours for `last(::Zip)` with differing iterator size types cause. `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 6ea14d6 commit fd30157

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

base/iterators.jl

Lines changed: 6 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,10 @@ function _nth(::IteratorSize, itr, n::Integer)
17001701
y === nothing && throw(BoundsError(itr, n))
17011702
y[1]
17021703
end
1704+
1705+
# issue #58922
1706+
_nth(::Union{HasShape, HasLength}, itr::Zip n::Integer) = map(nth(n), z.is)
1707+
17031708
"""
17041709
nth(n::Integer)
17051710

0 commit comments

Comments
 (0)