-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Make IteratorSize(::Iterators.Cycle)
not always IsInfinite
and document why
#54187
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
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: adienes <[email protected]>
# IsInfinite() would be false if iterator ever becomes empty | ||
IteratorSize(I) === IsInfinite() ? IsInfinite() : SizeUnknown() | ||
end | ||
IteratorSize(it::Cycle) = isempty(it.xs) ? HasLength() : IsInfinite() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have other examples where IteratorSize(it)
and IteratorSize(typeof(it))
(potentially) disagree? Makes me a bit uncomfortable.
And if it.xs
is stateful, it seems that IsInfinite
might turn out to be wrong, because isempty(it.xs)
might become true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have other examples where IteratorSize(it) and IteratorSize(typeof(it)) (potentially) disagree? Makes me a bit uncomfortable.
No, this is the first case, and changes the docs to suggest the possibility.
And if it.xs is stateful, it seems that IsInfinite might turn out to be wrong, because isempty(it.xs) might become true.
IMO that's because Iterators.cycle on stateful iterators is broken. It is explicitly documented to be infinite for non-empty arguments.
This PR includes:
Would it be possible to separate this into multiple PRs? |
function IteratorSize(::Type{Cycle{I}}) where I | ||
# TODO: find a better way of communicating the size of a cycle | ||
# IsInfinite() would be false if iterator ever becomes empty | ||
IteratorSize(I) === IsInfinite() ? IsInfinite() : SizeUnknown() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A refinement, to improve precision:
IteratorSize(I) === IsInfinite() ? IsInfinite() : SizeUnknown() | |
c = IteratorSize(I) | |
((c === IsInfinite()) || (c === HasShape{0}())) ? IsInfinite() : SizeUnknown() |
The idea is that there are two cases when we know the number of elements is not zero (and thus infinite):
- when
IteratorSize(I) === IsInfinite()
, already accounted for, just listing it for completeness - when
IteratorSize(I) === HasShape{0}()
, because a zero-dimensional shape implies a length of exactly one
Fixes #53169
Deletes an "XXX: this is false if iterator ever becomes empty" comment