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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Qqwy" data-post="10" data-topic="2806">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qqwy/48/1349_2.png" class="avatar"> Qqwy:</div>
<blockquote>
<p>The third one, ‘cheating’ using message-passing, is often too much overhead for what you want to use the generator for. Exceptions of course do exist: A GenServer that returns guaranteed-unique identifiers, for instance.</p>
</blockquote>
</aside>
<p>I don’t think it’s cheating. It’s taking advantage of the environment’s capabilities. Due to their threaded nature, languages like Python and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator" rel="noopener nofollow ugc">JavaScript</a> have to have dedicated language constructs for generators.</p>
<p><a href="https://youtu.be/0jsdXFUvQKE?t=272" rel="noopener nofollow ugc">Joe Armstrong demonstrates building a Future with concurrency features</a>, so if you <em>need</em> a generator - go ahead, use a process (even a GenServer) to build one. I still need to completely digest “<a href="http://theerlangelist.com/article/spawn_or_not" rel="noopener nofollow ugc">To spawn, or not to spawn?</a>” but I’m not advocating going on a spawn-fest; just sometimes it is good to leave the “sequential mindset” behind.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="31243" 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/python-generators-equivalent/2806/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-31243" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="31243"
                     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="31247" data-post-id="31247">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="benwilson512" data-post="6" data-topic="2806">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/48/1457_2.png" class="avatar"> benwilson512:</div>
<blockquote>
<p>An API like</p>
<p>s = step(1, 2)<br>
s.next() # 1<br>
s.next() # 3<br>
s.next() # 5</p>
<p>relies upon mutable state. T</p>
</blockquote>
</aside>
<p>Just a small note (not sure how on topic it is) about simulating functionality like this is to use a pipeline. I’ve used this approach a number of times in APIs.</p>
<blockquote>
<p>step(1,2)<br>
|&gt; next<br>
|&gt; next<br>
|&gt; block(fn s → … end)<br>
|&gt; next</p>
</blockquote> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="31247" data-batch-url="/posts/batch_likers">
                        0
                      </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/python-generators-equivalent/2806/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-31247" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="31247"
                     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="151654" data-post-id="151654">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>One of the points of yield/generators in languages like ruby and python is to compute solutions incrementally instead of materializing the solution all at once thus allowing large or even infinite results to be computed. While Elixir doesn’t have yield, it achieves the same goal using <a href="https://hexdocs.pm/elixir/Stream.html" rel="noopener nofollow ugc">Streams</a>. Let me illustrate with a simple backtracking problem: n queens. An inefficient but concise formulation in ruby might go:</p>
<pre data-code-wrap="rb"><code class="lang-rb">def solve(n, queens, rank, &amp;block)
  if rank == n
    yield queens
  else
    n.times do |file|
      if safe?(queens, file, rank)
        queens &lt;&lt; [file, rank]
        solve(n, queens, rank + 1, &amp;block)
        queens.pop
      end
    end
  end
end

solve(ARGV[0].to_i, [], 0) { |solution| puts solution.inspect }
</code></pre>
<p>The use of yield allows us to use the same code to print one result, all results or count the number of results by supplying a different block to consume then result.</p>
<p>To accomplish the same goal in Elixir, you’d use Streams and flatmap, like so:</p>
<pre data-code-wrap="ex"><code class="lang-ex">def solve(n, queens, r) when r == n, do: [{reverse(queens)}]
def solve(n, queens, r) do
  0..(n-1)
  |&gt; filter(fn f -&gt; safe?(queens, {f, r}) end)
  |&gt; flat_map(fn f -&gt; solve(n, [{f, r} | queens], r+1) end)
end

System.argv() |&gt; at(0) |&gt; String.to_integer |&gt; solve([], 0) |&gt; each(&amp;IO.inspect/1)
</code></pre>
<p>Hope that helps.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="151654" data-batch-url="/posts/batch_likers">
                        0
                      </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/python-generators-equivalent/2806/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-151654" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="151654"
                     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="151742" data-post-id="151742">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>After posting, I realised that my Elixir is ambiguous. It should be:</p>
<blockquote>
<p>def solve(n, queens, r) when r == n, do: [{Enum.reverse(queens)}]<br>
def solve(n, queens, r) do<br>
0..(n-1)<br>
|&gt; Stream.filter(fn f → safe?(queens, {f, r}) end)<br>
|&gt; Stream.flat_map(fn f → solve(n, [{f, r} | queens], r+1) end)<br>
end</p>
</blockquote> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="151742" 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/python-generators-equivalent/2806/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-151742" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="151742"
                     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>