-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Fix inconsistent behaviour for last(::Zip)
with differing iterator size types
#59217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
655ef5a
9856827
193b6fc
da76f8c
54ee79f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -473,7 +473,12 @@ 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))) | ||||||
function last(z::Zip) | ||||||
IteratorSize(z) == SizeUnknown() && | ||||||
throw(ArgumentError("Cannot get last element of zipped iterators of undefined lengths")) | ||||||
return nth(z, length(z)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing |
||||||
end | ||||||
|
||||||
function reverse(z::Zip) | ||||||
if !first(_zip_lengths_finite_equal(z.is)) | ||||||
throw(ArgumentError("Cannot reverse zipped iterators of unknown, infinite, or unequal lengths")) | ||||||
|
@@ -1700,6 +1705,9 @@ function _nth(::IteratorSize, itr, n::Integer) | |||||
y === nothing && throw(BoundsError(itr, n)) | ||||||
y[1] | ||||||
end | ||||||
|
||||||
_nth(::Union{HasShape, HasLength}, z::Zip, n::Integer) = Base.map(nth(n), z.is) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it will hit the generic fallback above that just calls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch. I didn't consider that case. This does indeed fail on iterators of unknown size. For example, Iterators.filter(x -> x > 0, -5:5) As humans, it's obvious that this is synonymous to We might need use function last(z::Zip)
n = last(_zip_lengths_finite_equal(z.is))
return nth(z, n)
end Is it worth keeping the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, that will fail if we have two iterators of unknown size in the zip(Iterators.filter(x -> x > 0, -5:5), Iterators.filter(x -> x % 2 == 0, -5:5)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for clarifying @Seelengrab. I agree, I think we don't properly handle the second option. What do you think of 193b6fc? Explicitly throwing an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm.. I'm not even sure
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. julia> struct Collatz
start::Int
end
julia> Base.eltype(::Type{Collatz}) = Int
julia> Base.IteratorSize(::Type{Collatz}) = Base.SizeUnknown()
julia> Base.iterate(c::Collatz) = (c.start, c.start)
julia> function Base.iterate(c::Collatz, last::Int)
isone(last) && return nothing
next = if iseven(last)
last ÷ 2
else
3*last + 1
end
(next, next)
end
julia> zip(1:10, Collatz(3)) |> collect
8-element Vector{Tuple{Int64, Int64}}:
(1, 3)
(2, 10)
(3, 5)
(4, 16)
(5, 8)
(6, 4)
(7, 2)
(8, 1) This is an iterator that just produces the Collatz sequence for a given number. This can do anything from being shorter than the other zipped iterator or longer than the zipped iterator (or loop infinitely on its own, who knows!). Still, if it does finish for a given number, we can definitely zip this with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
and defer to the component iterators. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implemented in da76f8c 🙂 |
||||||
|
||||||
""" | ||||||
nth(n::Integer) | ||||||
|
||||||
|
Uh oh!
There was an error while loading. Please reload this page.