Skip to content
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

Iterators.cycle, IteratorSize: infinite but empty iterator #53169

Open
nsajko opened this issue Feb 3, 2024 · 1 comment · May be fixed by #54187
Open

Iterators.cycle, IteratorSize: infinite but empty iterator #53169

nsajko opened this issue Feb 3, 2024 · 1 comment · May be fixed by #54187
Labels
bug Indicates an unexpected problem or unintended behavior iteration Involves iteration or the iteration protocol

Comments

@nsajko
Copy link
Contributor

nsajko commented Feb 3, 2024

The IteratorSize doc string says that if its return value is IsInfinite(), "the iterator yields values forever". The iterator returned by Iterators.cycle is documented to be empty in some cases, even though it seems to always be an infinite iterator currently:

julia> it = Iterators.cycle(())
Base.Iterators.Cycle{Tuple{}}(())

julia> isempty(it)
true

julia> iterate(it)

julia> Base.IteratorSize(typeof(it))
Base.IsInfinite()

julia> versioninfo()
Julia Version 1.11.0-DEV.1451
Commit f117a500ca9 (2024-02-01 15:38 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver2)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

Not sure what's a good fix, perhaps make Iterators.cycle return () when it's given an empty iterator as input. This wouldn't always be type stable, though.

@nsajko nsajko added bug Indicates an unexpected problem or unintended behavior collections Data structures holding multiple items, e.g. sets iteration Involves iteration or the iteration protocol correctness bug ⚠ Bugs that are likely to lead to incorrect results in user code without throwing labels Feb 3, 2024
@Seelengrab
Copy link
Contributor

Seelengrab commented Feb 4, 2024

In a similar vein (though not a duplicate): #43235

There too the IteratorSize won't match the actual iterator length.

@LilithHafner LilithHafner removed the correctness bug ⚠ Bugs that are likely to lead to incorrect results in user code without throwing label Apr 22, 2024
@nsajko nsajko removed the collections Data structures holding multiple items, e.g. sets label Jun 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior iteration Involves iteration or the iteration protocol
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants