<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="138599" data-post-id="138599">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="liskin" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/liskin/120/16293_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  liskin
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="tomekowal" data-post="11" data-topic="24382" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomekowal/48/2847_2.png" class="avatar"> tomekowal:</div>
<blockquote>
<p>For infinite streams without side effects like the stream of puzzles or <code>Stream.cycle([1,2,3])</code> setting atomics references seems wasteful.</p>
</blockquote>
</aside>
<p>It’s possible to use a less generic version of <code>callable_once</code> which instead of creating new atomics at every step uses a single atomics reference for the entire stream and increments it. I think the performance impact of such a thing is negligible. But I’m no expert on low-level erlang/elixir performance issues. Just wanted to mention this for completeness (I did write the code for this and then later simplified it).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="138599" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/best-way-to-iterate-a-stream/24382/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-138599" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="138599"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="138604" data-post-id="138604">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sasajuric" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sasajuric/120/991_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sasajuric
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Elixir In Action</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="tomekowal" data-post="11" data-topic="24382">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomekowal/48/2847_2.png" class="avatar"> tomekowal:</div>
<blockquote>
<p>Suddenly the amount of work to implement it becomes more significant. Next question arises: is it worth it?</p>
</blockquote>
</aside>
<p>Personally, I would write a small abstraction around puzzle sequence, with the following operations:</p>
<ul>
<li><code>PuzzleSequence.new(seed)</code></li>
<li><code>PuzzleSequence.next_puzzle(sequence)</code></li>
</ul>
<p>From there, I’d write the rest of the system and see how it unfolds. If I notice that there is some significant overlap between what I wrote and <code>Stream</code>/<code>Enum</code> functions, I’d try to see if I can somehow reuse the existing enum logic, e.g. by doing some trickery proposed here. But before that, I’d just stick with this simple interface.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="138604" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/best-way-to-iterate-a-stream/24382/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-138604" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="138604"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="138615" data-post-id="138615">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="tomekowal" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomekowal/120/2847_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  tomekowal
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yep, if I was doing it for work, I wouldn’t think twice about it and went with <code>PuzzleSequence.new(seed)</code> and <code>PuzzleSequence.next_puzzle(sequence)</code>. But I am doing it mostly for fun and learning. I am totally aware I am over-engineering but just from those discussions I understood Streams and Enumerables better and learned about <code>:atomics</code> <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"> I feel that was worth it <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="138615" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/best-way-to-iterate-a-stream/24382/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-138615" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="138615"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="181616" data-post-id="181616">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="BorjaEst" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/BorjaEst/120/19583_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  BorjaEst
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Pobably for simplicity, the developers set the definition of Stream to a function:<br>
<code>Stream :: function(_,_)</code></p>
<p>Which is used together with <em>fold</em> (wich iterates until the end).</p>
<p>When I try to do lazy generations in erlang, I use the following construction:<br>
<code>Stream :: [term() | function()]</code></p>
<p>Where the tail is the function that will generate the next iteration.<br>
Here an example (in elixir):</p>
<pre data-code-wrap="ex"><code class="lang-ex">  def lazy_seq(cycle) do
    lazy_queue(:queue.from_list(cycle))
  end

  defp lazy_queue(queue) do
    {{:value, h}, queue} = :queue.out(queue)
    [h | fn -&gt; lazy_queue(:queue.in(h, queue)) end]
  end
</code></pre>
<p>To use it you have to trigger the function  at the tail:</p>
<pre data-code-wrap="iex"><code class="lang-iex">iex(1)&gt; [_|lz] = Test.lazy_seq([1,2,3,4])
[1 | #Function&lt;0.133222699/0 in Test.lazy_queue/1&gt;]
iex(2)&gt; [_|lz] = lz.()
[2 | #Function&lt;0.133222699/0 in Test.lazy_queue/1&gt;]
iex(3)&gt; [_|lz] = lz.()
[3 | #Function&lt;0.133222699/0 in Test.lazy_queue/1&gt;]
iex(4)&gt; [_|lz] = lz.()
[4 | #Function&lt;0.133222699/0 in Test.lazy_queue/1&gt;]
iex(5)&gt; [_|lz] = lz.()
[1 | #Function&lt;0.133222699/0 in Test.lazy_queue/1&gt;]
iex(6)&gt; [_|lz] = lz.()
[2 | #Function&lt;0.133222699/0 in Test.lazy_queue/1&gt;]
...
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="181616" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/best-way-to-iterate-a-stream/24382/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-181616" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="181616"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="191656" data-post-id="191656">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="evadne" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/evadne/120/13949_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  evadne
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Enjoy the updated version… forgot to add clause for <code>{:halted, item}</code></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule StreamIterator.Suspension do
  defstruct continuation: nil

  def start(stream) do
    reduce_fun = fn item, _acc -&gt; {:suspend, item} end
    {:suspended, nil, continuation} = Enumerable.reduce(stream, {:suspend, nil}, reduce_fun)
    %__MODULE__{continuation: continuation}
  end

  def next(state)
  
  def next(%{continuation: nil} = state) do
    {:eof, state}
  end

  def next(state) do
    case state.continuation.({:cont, nil}) do
      {:suspended, item, continuation} -&gt; {:next, item, %{state | continuation: continuation}}
      {:halted, item} -&gt; {item, %{state | continuation: nil}}
      {:done, nil} -&gt; {:eof, %{state | continuation: nil}}
    end
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="191656" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/best-way-to-iterate-a-stream/24382/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-191656" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="191656"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>