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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>here’s my simplified version from my previous post.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Loop.Basic do
  @spec loop(term, (term -&gt; term), (term -&gt; term), (term -&gt; term)) :: list
  def loop(term, condition, transform \\ &amp;(&amp;1 + 1), fun \\ &amp;(&amp;1))
      when is_function(condition, 1) and is_function(transform, 1) and is_function(fun, 1) do
    do_loop(term, condition, transform, fun, [])
  end

  defp do_loop(current, condition, transform, fun, acc) do
    if condition.(current) do
      do_loop(transform.(current), condition, transform, fun, [fun.(current) | acc])
    else
      Enum.reverse(acc)
    end
  end  
end
</code></pre>
<p>Examples:</p>
<pre data-code-wrap="iex"><code class="lang-iex">      iex&gt; Loop.Basic.loop(1, &amp;(&amp;1 &lt;= 5))
      [1, 2, 3, 4, 5]

      iex&gt; Loop.Basic.loop(12345, &amp;(String.length("#{&amp;1}") &lt;= 10), &amp;(&amp;1 * 7), &amp;(:"#{&amp;1}"))
      [:"12345", :"86415", :"604905", :"4234335", :"29640345", :"207482415", :"1452376905"]

</code></pre>
<p>it’s pretty functional in the mathematical sense of the word.</p>
<p>I called it basic, because functions only accept one argument (term)</p>
<p>Once I finished that, I was like…well, it gotta be concurrent as well,<br>
so I ended up with a library.<br>
<a href="https://github.com/eksperimental/loop/" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/eksperimental/loop/</a></p>
<p>please have a look at the sources:</p>
<ul>
<li><a href="https://github.com/eksperimental/loop/blob/master/lib/loop/concurrent.ex" class="inline-onebox" rel="noopener nofollow ugc">loop/lib/loop/concurrent.ex at master · eksperimental/loop · GitHub</a></li>
<li><a href="https://github.com/eksperimental/loop/blob/master/lib/loop/sequential.ex" class="inline-onebox" rel="noopener nofollow ugc">loop/lib/loop/sequential.ex at master · eksperimental/loop · GitHub</a></li>
</ul>
<p>and tell me what you think of it,<br>
and if it can be improved in some way.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="3956" 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/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-3956" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="3956"
                     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 #22"></div>
  </section>
</div>
    <div class="postbit" id="8479" data-post-id="8479">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>How to do a <code>for</code> loop in Elixir (using only recursion)</p>
</blockquote>
<p>I had this same question, but it seems that I have to think differently about this, and instead of trying to make a for loop, I just need to embrace recursion as the proper way to drill through things. Someone asked for a link: I thought this was a good one.</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="http://elixir-lang.org/getting-started/recursion.html#loops-through-recursion">
  <header class="source">

      <a href="http://elixir-lang.org/getting-started/recursion.html#loops-through-recursion" target="_blank" rel="noopener nofollow">elixir-lang.org</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="http://elixir-lang.org/getting-started/recursion.html#loops-through-recursion" target="_blank" rel="noopener nofollow">Loops through recursion - Recursion — Elixir v1.20.2</a></h3>

  <p>Elixir does not provide loop constructs. Instead we leverage recursion and high-level functions for working with collections. This chapter will explore the former.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="8479" 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/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-8479" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="8479"
                     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 #23"></div>
  </section>
</div>
    <div class="postbit" id="11026" data-post-id="11026">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="junebug" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  junebug
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m Johnny Come lately on this subject but from what I’ve been told by the pro’s you’re not a programmer or at least you cannot get hired as one until you actually understand what’s going on under the hood. Sure <strong>Enum.each</strong>, <strong>Enum.map</strong>, etc are already implemented for us but most books I’ve read teach you <strong>how</strong> recursion works. I would assume that once Elixir becomes more mainstream programmers will be asked to implement recursive functions in interviews.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="11026" 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/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-11026" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="11026"
                     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 #24"></div>
  </section>
</div>
    <div class="postbit" id="11028" data-post-id="11028">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Qqwy" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Qqwy/120/1349_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Qqwy
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>TypeCheck Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I wonder, is using the <a href="http://rosettacode.org/wiki/Y_combinator" rel="noopener nofollow ugc">Y-combinator</a> cheating? I would actually suggest that it might be the ‘nicest’ solution, because it sort of does ‘meta’-recursion; calling a function with itself as argument, so the function can recurse if and when it wants to, without using explicit recursion. (For instance, this will even work when using anonymous functions).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="11028" 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/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-11028" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="11028"
                     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 #25"></div>
  </section>
</div>
    <div class="postbit" id="11103" data-post-id="11103">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="eksperimental" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eksperimental/120/1401_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  eksperimental
                  </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="26" data-topic="595" 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/qqwy/48/1349_2.png" class="avatar"> Qqwy:</div>
<blockquote>
<p>I wonder, is using the Y-combinator cheating? I would actually suggest that it might be the ‘nicest’ solution, because it sort of does ‘meta’-recursion; calling a function with itself as argument, so the function can recurse if and when it wants to, without using explicit recursion. (For instance, this will even work when using anonymous functions).</p>
</blockquote>
</aside>
<p>I would love to see a solution like this.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="11103" 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/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-11103" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="11103"
                     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 #26"></div>
  </section>
</div>
    <div class="postbit" id="11105" data-post-id="11105">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Qqwy" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Qqwy/120/1349_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Qqwy
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>TypeCheck Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I think it would be something like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule For do
    # Create a closure which contains a function that applies itself to itself.
    # Call this function with as argument a function that calls the passed function with the passed `arg`.
    # In lambda calculus: `Y = λf.(λx.f(x x))(λx.f(x x))`
    # An easier definition: `Y f = f (Y f)`
    # If there exist at least one 'fixed point' where the function `f` does not call the passed function, this structure terminates.
    # 
    # This `y_combinator/0` can be called with a double anonymous function: 
    #   The outermost receives the fixpoint function as input, which can be called when you want to recurse.
    #   The innermost receives whatever argument needs to be passed during iterations.
  def y_combinator do
   fn f -&gt; 
      (fn x -&gt; 
        x.(x) 
      end).(
        fn y -&gt; 
          f.(
            fn arg -&gt; 
              y.(y).(arg) 
            end
          ) 
        end
      ) 
    end
  end

  def loop(initialization, condition, body) do
    y_combinator.(fn recurse -&gt;
      fn state -&gt;
        if !condition.(state) do 
          state
        else
          state
          |&gt; body.()
          |&gt; recurse.()
        end
      end
    end).(initialization)
  end
end

</code></pre>
<p>Which could then be called as follows:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">For.loop(0, &amp;(&amp;1 &lt;= 10), fn i -&gt; 
  IO.puts(i)
  i + 1
end)
</code></pre>
<p>I wonder if this could be made even more ‘idiomatic imperative programming-like’ when doing something with (unhygienic) macros.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="11105" data-batch-url="/posts/batch_likers">
                        6
                      </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/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-11105" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="11105"
                     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 #27"></div>
  </section>
</div>
    <div class="postbit" id="11133" data-post-id="11133">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This might be on of my favorite posts of all time.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="11133" data-batch-url="/posts/batch_likers">
                        2
                      </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/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-11133" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="11133"
                     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 #28"></div>
  </section>
</div>
    <div class="postbit" id="11361" data-post-id="11361">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I once used a recursive anonymous function for an elixir golf challenge <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p><aside class="onebox githubgist" data-onebox-src="https://gist.github.com/gvaughn/fb89c65e01207f1d281d">
  <header class="source">

      <a href="https://gist.github.com/gvaughn/fb89c65e01207f1d281d" target="_blank" rel="noopener nofollow ugc">gist.github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://gist.github.com/gvaughn/fb89c65e01207f1d281d" target="_blank" rel="noopener nofollow ugc">https://gist.github.com/gvaughn/fb89c65e01207f1d281d</a></h4>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="11361" 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/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-11361" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="11361"
                     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>