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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Ok i think the issue is that you should not do that at all. You should not simulate imperative or object oriented (with mutable state) constructs in functional languages. Your example is still not good.<br>
<code>for x &lt;- 0..10, do: action(x)</code><br>
is what you probably should have used.</p>
<p>Anyway that old plain C-like for loop with starting value accumulator is something that is not really readable, and has no use in modern languages.<br>
In ruby you have:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">some_objects.each {  |an_object| action(object) }
# or
for an_object in some_objects
  action(an_object)  
end
</code></pre>
<p>In python you also iterate over collection like this</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for an_object in some_objects:
    action(an_object)
</code></pre>
<p>Even in Java since Java 5 you would probably do</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for(ClassName an_object : some_objects)  {
   action(an_object)
}
</code></pre>
<p>One of my most hated languages: JavaScript, has similar constructs</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">function callback_function(currentObject, index, array) {
  action(currentObject)
}
some_objects.forEach(callback_function)
</code></pre>
<p>Why? Because all of those are much more readable and descriptive, then that old plain C-like for loop. Answer yourself, how often you really need that index? Don’t you really want the object that stands behind that index?</p>
<p>And in Elixir you have what <a class="mention" href="/u/thousandsofthem" rel="nofollow">@thousandsofthem</a> and <a class="mention" href="/u/jwarlander" rel="nofollow">@jwarlander</a> wrote. And you should be using it instead of trying to wrap some logic into totally (in Elixir) out of place C-like for loop <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> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="3832" 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/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-3832" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="3832"
                     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="3834" data-post-id="3834">
  <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’m jumping in late here, but I worry this thread is going off in the weeds. If one is helping to introduce an imperative programmer into Elixir and they ask “how do you handle for loops?” is it more helpful to show them that recursion can do everything imperative iteration can do, or jump immediately to higher order functions or for comprehensions? I see value in showing the recursive solution, but quickly moving over it into better abstractions. IMHO, if a functional programmer has not, at some point, implemented map and reduce with recursion, then they don’t really understand FP.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’ve never implemented map and reduce and I don’t feel like I’m a worse functional developer because of that? (maybe I did implement them in Haskell classes a few years ago). Why do you say that about map and reduce specifically?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="3835" 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/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-3835" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="3835"
                     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="3836" data-post-id="3836">
  <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>Maybe I spoke too hastily. map can be implemented on top of reduce. reduce can be implemented on top of recursion. I’m drawing a blank right now for what recursion can do that reduce cannot. I suppose my core point is that reduce is a level above recursion. If you can implement reduce from recursion, then you’ve got a solid handle on the core of FP. Other higher order functions can be derived from reduce.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="3836" 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/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-3836" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="3836"
                     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 #15"></div>
  </section>
</div>
    <div class="postbit" id="3837" data-post-id="3837">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="bentanweihao" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bentanweihao/120/326_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  bentanweihao
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of The Little Elixir and OTP Guidebook</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>All these talk about <code>fold</code> reminds me of a paper that I’ve yet to read: <a href="http://www.cs.nott.ac.uk/~pszgmh/fold.pdf" rel="noopener nofollow ugc">A tutorial on the universality and<br>
expressiveness of fold</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="3837" 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/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-3837" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="3837"
                     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 #16"></div>
  </section>
</div>
    <div class="postbit" id="3843" data-post-id="3843">
  <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">
								<aside class="quote no-group" data-username="bentanweihao" data-post="17" 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/bentanweihao/48/326_2.png" class="avatar"> bentanweihao:</div>
<blockquote>
<p>All these talk about <code>fold</code> reminds me of a paper that I’ve yet to read: <a href="http://www.cs.nott.ac.uk/~pszgmh/fold.pdf" rel="noopener nofollow ugc">A tutorial on the universality and<br>
expressiveness of fold</a></p>
</blockquote>
</aside>
<p>Hm! Interesting paper. Very cool to see that so much can be built on top of <code>foldr</code> / <code>foldl</code>.</p>
<p>One thing the paper does not state, (and is often overlooked), is that <code>foldl</code> is tail-recursive, while <code>foldr</code> is not. There is <a href="https://wiki.haskell.org/Foldr_Foldl_Foldl'" rel="noopener nofollow ugc">a great example on the Haskell wiki</a> about why this is the case.</p>
<hr>
<p><strong>EDIT:</strong> It seems that the distinction between tail-call and non-tail-call functions in Erlang/Elixir is not that important.</p>
<p>See:</p>
<ul>
<li><a href="http://ferd.ca/erlang-s-tail-recursion-is-not-a-silver-bullet.html" rel="noopener nofollow ugc">Erlangs Tail Recursion is not a silver bullet</a></li>
<li><a href="http://erlang.org/doc/efficiency_guide/myths.html#id60476" rel="nofollow">the Eight Myths of Erlang Performance</a></li>
<li>and also <a href="https://github.com/elixir-lang/elixir/pull/4623" rel="noopener nofollow ugc">this rejected Pull-Request by me to add tail-recursion to the documentation of foldr/foldl <img src="https://forum.elixirforum.com/images/emoji/apple/sweat_smile.png?v=15" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"> </a></li>
</ul> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="3843" 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/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-3843" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="3843"
                     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 #17"></div>
  </section>
</div>
    <div class="postbit" id="3846" data-post-id="3846">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>There is nice book <a href="https://www.manning.com/books/functional-programming-in-scala" class="inline-onebox" rel="noopener nofollow ugc">Functional Programming in Scala - Paul Chiusano and Runar Bjarnason</a> .There a a lot common concepts and more like what is pure function . The drawback is the code is in Scala <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> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="3846" 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/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-3846" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="3846"
                     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 #18"></div>
  </section>
</div>
    <div class="postbit" id="3847" data-post-id="3847">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="rvirding" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rvirding/120/1409_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  rvirding
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Erlang</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I also think there is one benefit for basically everyone of implementing various types of loops explicitly using recursion, and that is to understand what is really going on. So while you might most of the time use libraries or provided constructs instead of actually explicitly doing yourself, really knowing what is going will make you a better programmer. It will allow you use libraries and provided constructs in a better way.</p>
<p>Robert</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="3847" data-batch-url="/posts/batch_likers">
                        11
                      </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/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-3847" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="3847"
                     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 #19"></div>
  </section>
</div>
    <div class="postbit" id="3850" data-post-id="3850">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Wise words from Programming Elixir by Dave Thomas</p>
<blockquote>
<p>L. Peter Deutsch once penned, “To iterate is human, to recurse divine.” And<br>
that’s certainly the way I felt when I first started coding Elixir. The joy of<br>
pattern-matching lists in sets of recursive functions drove my designs. After<br>
a while, I realized that perhaps I was taking this too far.<br>
In reality, most of our day-to-day work is better handled using the various<br>
enumerators built into Elixir. They make your code smaller, easier to understand,<br>
and probably more efficient.<br>
Part of the process of learning to be effective in Elixir is working out for<br>
yourself when to use recursion and when to use enumerators. I recommend<br>
enumerating when you can.</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="3850" 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/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-3850" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="3850"
                     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 #20"></div>
  </section>
</div>
    <div class="postbit" id="3877" data-post-id="3877">
  <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>I found your question challenging <a class="mention" href="/u/onor.io" rel="nofollow">@Onor.io</a>, so I tinkered with your approach for a bit, until I ended up with this</p>
<aside class="onebox githubgist" data-onebox-src="https://gist.github.com/eksperimental/03ff05172b174a7f29861ccd0be0acc3">
  <header class="source">

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

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



  </article>

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

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

<p>the main difference with your approach come from the question “why limit ourselves with integers when we can pass any data type, specially functions”.<br>
Any feedback is more than welcome.</p>
<p>PS: I haven’t looked at the implementation of <code>Enum.reduce/3</code>  but It feels like it’s something of the sorts of what I have just implemented.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="3877" 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/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-3877" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="3877"
                     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 #21"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/595/load_more?page=3">Load more posts (8 remaining)</a>
</div></template></turbo-stream>