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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The thing that gets me is that the first argument is the argument that is overwritten in memory as the return value. Since the return value would be placed in memory location one, which is the same as the first argument I would assume that you would get a small performance difference in favor of swapping the order from the current implementation.</p>
<p>We would only get the benefit of the argument pattern if we changed Erlang since <code>Enum.reduce/3</code> delegates straight to <code>:list.foldl/3</code> id the first argument is a list.</p>
<p>I haven’t tested this so I’m speculating. It is what I do.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="131439" 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/why-enum-reduce-fun-arguments-are-x-acc-instead-of-acc-x/23037/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-131439" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="131439"
                     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="131444" data-post-id="131444">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This is a really good point.</p>
<p>I frequently switch these as well.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="131444" 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/why-enum-reduce-fun-arguments-are-x-acc-instead-of-acc-x/23037/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-131444" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="131444"
                     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="131819" data-post-id="131819">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>In my opinion that’s because Elixir has the convention of having “target as the first argument”. This is mostly because of how pipes work. The direct target of the function is almost always the first argument. I would say that the element is the target of the reduction function and not the accumulator.<br>
Other languages like haskell have that around, but they use last parameter as a target for the exact same reasons - pipes pipe to the last argument because it’s implemented on currying and not on 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="131819" 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/why-enum-reduce-fun-arguments-are-x-acc-instead-of-acc-x/23037/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-131819" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="131819"
                     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="131914" data-post-id="131914">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="stefanluptak" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stefanluptak/120/34398_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  stefanluptak
                    <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>I agree with the first part, that usually the first argument is the one being modified and most of the time also returned from the function.<br>
But in case of a function passed to <code>Enum.reduce</code>, I would say, that the accumulator is the one being modified based on the element currently iterated. You’re even returning modified <code>acc</code> from the reducing function. Having it in the form of <code>acc, x</code> should be the preferred form in my opinion.<br>
I understand that this can be viewed from different angles and it’s not easy to decide which form is better or even change it in the next major Elixir release. I was just curious what was the reason it is the way it is.<br>
Anyway, thanks a lot for all the responses. For me, it’s very interesting and informative to read them.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="131914" 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/why-enum-reduce-fun-arguments-are-x-acc-instead-of-acc-x/23037/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-131914" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="131914"
                     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="267472" data-post-id="267472">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Sorry for digging up such an old thread. Recently I found a new reason. For non-commutative operations like matrix multiplication, the code would be shorter if we put the <code>acc</code> first. For example, given a list of matrices, to calculate the dot product of them, we have to write something like</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">prod = Enum.reduce(matrices, fn matrix, acc -&gt; Matrix.dot(acc, matrix) end)
</code></pre>
<p>or</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">prod = Enum.reduce(matrices, &amp;Matrix.dot(&amp;2, &amp;1))
</code></pre>
<p>But if <code>acc</code> is the first argument, then we can simply write</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">prod = Enum.reduce(matrices, &amp;Matrix.dot/2)
</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="267472" 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/why-enum-reduce-fun-arguments-are-x-acc-instead-of-acc-x/23037/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-267472" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="267472"
                     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="267478" data-post-id="267478">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I was also switching said arguments, but honestly that was never a pain as it’s not enough big % of all <code>reduce</code> calls. Also I think that <code>element</code> is passed before <code>accumulator</code> for consistency i.e. not all <code>Enum</code> functions use <code>accumulator</code> and the only function argument everywhere is <code>element</code>. I think that for newbies it would be confusing why <code>element</code> is one time a first argument and the other time the second argument.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="267478" 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/why-enum-reduce-fun-arguments-are-x-acc-instead-of-acc-x/23037/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-267478" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="267478"
                     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>