Skip to content

collect + Iterators.Stateful + Base.Generator lose last element #42107

@jacg

Description

@jacg
julia> collect(Iterators.Stateful(2x for x in 1:3))
2-element Vector{Int64}:
 2
 4

The input has 3 elements, I would therefore expect the output also to have 3 elements, but there are only 2.

On the other hand, the problem disappears if

  • we remove the generator

    collect(Iterators.Stateful(1:3))
    3-element Vector{Int64}:
     1
     2
     3
    
  • we use iterate directly

    julia> s = Iterators.Stateful(2x for x in 1:3);
    
    julia> iterate(s)
    (2, nothing)
    
    julia> iterate(s, ans[2])
    (4, nothing)
    
    julia> iterate(s, ans[2])
    (6, nothing)
    
    julia> iterate(s, ans[2])
    
    julia> 
    

versioninfo

julia> versioninfo()
Julia Version 1.6.1
Commit 6aaedecc44 (2021-04-23 05:59 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, ivybridge)

julia> 

The problem also appears in julia 1.3.1, but it seems to be behaving as expected in 1.0.

Edit:

My 1.3.1 trial was in an online REPL (https://replit.com/languages/julia) which claims to be 1.3.1 but versioninfo() shows 1.4.1

julia version 1.3.1
 versioninfo()
Julia Version 1.4.1
Commit 381693d3df* (2020-04-14 17:20 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU @ 2.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-8.0.1 (ORCJIT, haswell)

 collect(Iterators.Stateful(2x for x in 1:3))
2-element Array{Int64,1}:
 2
 4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions