<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; collect(Iterators.Stateful(2x for x in 1:3))
2-element Vector{Int64}:
 2
 4"><pre class="notranslate"><code class="notranslate">julia&gt; collect(Iterators.Stateful(2x for x in 1:3))
2-element Vector{Int64}:
 2
 4
</code></pre></div>
<p dir="auto">The input has 3 elements, I would therefore expect the output also to have 3 elements, but there are only 2.</p>
<p dir="auto">On the other hand, the problem disappears if</p>
<ul dir="auto">
<li>
<p dir="auto">we remove the generator</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="collect(Iterators.Stateful(1:3))
3-element Vector{Int64}:
 1
 2
 3"><pre class="notranslate"><code class="notranslate">collect(Iterators.Stateful(1:3))
3-element Vector{Int64}:
 1
 2
 3
</code></pre></div>
</li>
<li>
<p dir="auto">we use <code class="notranslate">iterate</code> directly</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; s = Iterators.Stateful(2x for x in 1:3);

julia&gt; iterate(s)
(2, nothing)

julia&gt; iterate(s, ans[2])
(4, nothing)

julia&gt; iterate(s, ans[2])
(6, nothing)

julia&gt; iterate(s, ans[2])

julia&gt; "><pre class="notranslate"><code class="notranslate">julia&gt; s = Iterators.Stateful(2x for x in 1:3);

julia&gt; iterate(s)
(2, nothing)

julia&gt; iterate(s, ans[2])
(4, nothing)

julia&gt; iterate(s, ans[2])
(6, nothing)

julia&gt; iterate(s, ans[2])

julia&gt; 
</code></pre></div>
</li>
</ul>
<h1 dir="auto">versioninfo</h1>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; 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&gt; "><pre class="notranslate"><code class="notranslate">julia&gt; 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&gt; 
</code></pre></div>
<p dir="auto">The problem also appears in julia 1.3.1, but it seems to be behaving as expected in 1.0.</p>
<p dir="auto">Edit:</p>
<p dir="auto">My 1.3.1 trial was in an online REPL (<a href="https://replit.com/languages/julia" rel="nofollow">https://replit.com/languages/julia</a>) which claims to be 1.3.1 but <code class="notranslate">versioninfo()</code> shows 1.4.1</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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"><pre class="notranslate"><code class="notranslate">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
</code></pre></div>
<p dir="auto">Strage behaviour of Stateful iterator:<br>
Although</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; for a in Iterators.Stateful([1,2,3])
             print(a)
         end
123"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-k">for</span> a <span class="pl-k">in</span> Iterators<span class="pl-k">.</span><span class="pl-c1">Stateful</span>([<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>])
             <span class="pl-c1">print</span>(a)
         <span class="pl-k">end</span>
<span class="pl-c1">123</span></pre></div>
<p dir="auto">works as it should,<br>
With list comprehension it fails to iterate over last element</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; [a for a in Iterators.Stateful([1,2,3])]
2-element Array{Int64,1}:
 1
 2"><pre class="notranslate">julia<span class="pl-k">&gt;</span> [a <span class="pl-k">for</span> a <span class="pl-k">in</span> Iterators<span class="pl-k">.</span><span class="pl-c1">Stateful</span>([<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>])]
<span class="pl-c1">2</span><span class="pl-k">-</span>element Array{Int64,<span class="pl-c1">1</span>}<span class="pl-k">:</span>
 <span class="pl-c1">1</span>
 <span class="pl-c1">2</span></pre></div>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; [a for a in Iterators.Stateful([1])]
ERROR: BoundsError: attempt to access 0-element Array{Int64,1} at index [1]"><pre class="notranslate">julia<span class="pl-k">&gt;</span> [a <span class="pl-k">for</span> a <span class="pl-k">in</span> Iterators<span class="pl-k">.</span><span class="pl-c1">Stateful</span>([<span class="pl-c1">1</span>])]
ERROR<span class="pl-k">:</span> BoundsError<span class="pl-k">:</span> attempt to access <span class="pl-c1">0</span><span class="pl-k">-</span>element Array{Int64,<span class="pl-c1">1</span>} at index [<span class="pl-c1">1</span>]</pre></div>
<p dir="auto">tried on Julia 1.3.1, Julia 1.4.0 and Julia 1.4.1.<br>
More details in <a href="https://discourse.julialang.org/t/strange-behavior-with-list-comprehensions-using-iterators-stateful/29715" rel="nofollow">https://discourse.julialang.org/t/strange-behavior-with-list-comprehensions-using-iterators-stateful/29715</a></p>