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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m a big fan of comprehensions, and I love the idea of making them even more useful.</p>
<p>My first impression is that <code>for let</code> and <code>for reduce</code> don’t read like most of the language. Even though the <code>:reduce</code> option is a little strange, matching on the acc reminds me of a <code>case</code> or <code>fn</code>.</p>
<p>Edit: to me it’s not clear that the variables bound in <code>for let</code> and <code>for reduce</code> are repeatedly bound to. I’m not used to seeing that elsewhere in the language (except for the left side of a generator; maybe I’m only okay with that because I’m used to it, but I think the <code>&lt;-</code> operator 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="236930" data-batch-url="/posts/batch_likers">
                        5
                      </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/introducing-for-let-and-for-reduce/44773/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-236930" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="236930"
                     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="236932" data-post-id="236932">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="brettbeatty" data-post="13" data-topic="44773">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/brettbeatty/48/36494_2.png" class="avatar"> brettbeatty:</div>
<blockquote>
<p>matching on the acc reminds me of a <code>case</code> or <code>fn</code>.</p>
</blockquote>
</aside>
<p>But it <em>is</em> like a <code>case</code> !</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for i &lt;- 1..10, reduce: {:even, 0} do
  {:even, n} -&gt; {:odd, n + i}
  {:odd, n} -&gt; {:even, (n + 1) * i}
end
</code></pre>
<blockquote>
<p>Edit: to me it’s not clear that the variables bound in <code>for let</code> and <code>for reduce</code> are repeatedly bound to. I’m not used to seeing that elsewhere in the language (except for the left side of a generator; maybe I’m only okay with that because I’m used to it, but I think the <code>&lt;-</code> operator helps).</p>
</blockquote>
<p>I think the same. Something like <code>for {sum, count} &lt;- let {0, 0}, i &lt;- my_list, do: ...</code> could be cool too.</p>
<p>Edit: I like it actually because in that case <code>let</code> would be just a thing that implements the ForLoop protocol <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"> and not specific <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="236932" 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/introducing-for-let-and-for-reduce/44773/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-236932" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="236932"
                     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="236933" data-post-id="236933">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>That was my point: I feel like the <code>:reduce</code> option feels more like the rest of the language than the proposed <code>for let</code> and <code>for reduce</code>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="236933" data-batch-url="/posts/batch_likers">
                        8
                      </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/introducing-for-let-and-for-reduce/44773/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-236933" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="236933"
                     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="236935" data-post-id="236935">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="lud" data-post="11" data-topic="44773" 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/lud/48/14382_2.png" class="avatar"> lud:</div>
<blockquote>
<p>Yest but you both missed my point. If that new code cannot be ran by previous elixir versions then what is the strong argument about bringing new syntax, if it is simpler ?</p>
</blockquote>
</aside>
<p>The proposal brings no new syntax. With current Elixir you can already write your own version of <code>for</code>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">myfor let sum = 0, i &lt;- [1, 2, 3] do
  :bar
end
</code></pre>
<p>because it’s equivalent to this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">myfor(let(sum = 0, i &lt;- [1, 2, 3], do: :bar))
</code></pre>
<p>See <a class="mention" href="/u/benwilson512" rel="nofollow">@benwilson512</a>’s answer.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="236935" 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/introducing-for-let-and-for-reduce/44773/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-236935" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="236935"
                     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="236941" data-post-id="236941">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The more I think about it, the more of a curve ball I think <code>for let</code> is, specially for newcomers.</p>
<p>If I read this elixir line:</p>
<p><code>for let {count, sum} = {0, 0}, i &lt;- [1, 2, 3] do</code></p>
<p>In plain English I read:</p>
<p><code>for let count &amp; sum equal to 0, item in elements do</code></p>
<p>Which is not very descriptive on it’s own, and a little confusing considering the meaning in other languages. Certainly I would never expect it to be basically <code>Enum.map_reduce</code>.</p>
<p>I don’t know the intricacies of Elixir lang, but I would try to make it understandable on a english language level (that’s why the Python version is so understandable in my opinion).</p>
<p>Uninformed &amp; probably silly proposal:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for map_reduce i &lt;- [1, 2, 3], acc: {count, sum} = {0, 0} do
</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="236941" 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/introducing-for-let-and-for-reduce/44773/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-236941" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="236941"
                     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="236942" data-post-id="236942">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I do, though, like the flat_map-style interface. I wonder if an option to do more of a <a href="https://hexdocs.pm/elixir/Enum.html#flat_map_reduce/3" rel="noopener nofollow ugc">flat_map_reduce-style</a> interface would cover most of these cases. You could conditionally emit zero, one, or many output values for every generator value or even halt iteration. I don’t think it’s <em>that</em> big a deal to throw out some part of an acc (or the collection paired with the acc, in this case).</p>
<p>I’m picturing something like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">{results, acc} =
  for value &lt;- my_enumerable(), acc: @initial_acc do
    acc -&gt;
      case do_something_with(value, acc) do
        {:one, result} -&gt;
          {[result], update_acc(result, acc)}

        {:many, results} -&gt;
          {results, Enum.reduce(results, acc, &amp;update_acc/2)}

        :ignore -&gt;
          {[], acc}

        :error -&gt;
          {:halt, acc}
      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="236942" 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/introducing-for-let-and-for-reduce/44773/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-236942" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="236942"
                     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="236967" data-post-id="236967">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="fceruti" data-post="17" data-topic="44773">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fceruti/48/28850_2.png" class="avatar"> fceruti:</div>
<blockquote>
<p>If I read this elixir line:</p>
<p><code>for let {count, sum} = {0, 0}, i &lt;- [1, 2, 3] do</code></p>
<p>In plain English I read:</p>
<p><code>for let count &amp; sum equal to 0, item in elements do</code></p>
</blockquote>
</aside>
<p>I agree that this makes the comprehension “read” oddly in English. What about this kind of qualifier syntax:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for i &lt;- [1,2,3,4],
let {count, sum} = {0, 0} do 
    sum = sum + i
    count = count + 1
    {i, {sum, count}}
end
</code></pre>
<p>Now in English it’s more obvious what this parses out to: “For every item in the list let variables count and sum be zero and do the following functions using the item, count, and sum.”<br>
Actually, as an implementation detail this might be the simplest as behind the scenes the <code>let</code> keyword just wraps the initial assignment in an enumerable:<br>
<code>let {count, sum} = {0,0}</code> being equivalent to <code>{count, sum} &lt;- [{0,0}]</code></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="236967" data-batch-url="/posts/batch_likers">
                        8
                      </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/introducing-for-let-and-for-reduce/44773/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-236967" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="236967"
                     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="236970" data-post-id="236970">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>To me the <code>let</code> word carries the imperative / mutable connotation still. Has it been considered that using e.g. <code>acc</code> and not <code>let</code> might be steering the reader into the right way of thinking about this new <code>for</code> syntax?</p>
<aside class="quote no-group" data-username="stevensonmt" data-post="19" data-topic="44773">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stevensonmt/48/20503_2.png" class="avatar"> stevensonmt:</div>
<blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">for i &lt;- [1,2,3,4],
let {count, sum} = {0, 0} do 
    sum = sum + i
    count = count + 1
    {i, {sum, count}}
end
</code></pre>
<p>Now in English it’s more obvious what this parses out to: “For every item in the list let variables count and sum be zero and do the following functions using the item, count, and sum.”</p>
</blockquote>
</aside>
<p>I like this, a lot, and here <code>let</code> actually makes more sense (to me at least).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="236970" 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/introducing-for-let-and-for-reduce/44773/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-236970" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="236970"
                     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="236983" data-post-id="236983">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                    <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 class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="brettbeatty" data-post="18" data-topic="44773">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/brettbeatty/48/36494_2.png" class="avatar"> brettbeatty:</div>
<blockquote>
<p>I do, though, like the flat_map-style interface.</p>
</blockquote>
</aside>
<p>In a comprehension, the flattening happens by supporting multiple generators. I don’t think we need to introduce another way of flattening, especially because the way comprehensions currently flatten also allow you to filter easily. And this adds an even bigger departure from the regular <code>for</code> (as in the most common case we now need to wrap each of the collection’s element in a list).</p>
<p>The proposal also explains why it is beneficial to have the variables declared before the generators, as it gives us more power to express other constructs later on.</p>
<aside class="quote no-group" data-username="stevensonmt" data-post="19" data-topic="44773">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stevensonmt/48/20503_2.png" class="avatar"> stevensonmt:</div>
<blockquote>
<p>I agree that this makes the comprehension “read” oddly in English. What about this kind of qualifier syntax:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for i &lt;- [1,2,3,4],
let {count, sum} = {0, 0} do 
    sum = sum + i
    count = count + 1
    {i, {sum, count}}
end
</code></pre>
</blockquote>
</aside>
<p>I strongly disagree with this version. <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"> If I were to read such code, I would expect count and sum to be reset on every new value of <code>i</code>.</p>
<p>To be honest, I would say “having to read perfectly in english” is a red herring. I understand the current syntax can be confusing for some (and there is criticism saying so), but optimizing for “english readability” is not what we should aim for to address it.</p>
<p>The proposal also explains why it is beneficial to declare the “let” variables early on, as that gives the ability to express more complex scenarios in the future.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="236983" data-batch-url="/posts/batch_likers">
                        5
                      </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/introducing-for-let-and-for-reduce/44773/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-236983" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="236983"
                     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="236984" data-post-id="236984">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                    <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 class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="fceruti" data-post="17" data-topic="44773">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fceruti/48/28850_2.png" class="avatar"> fceruti:</div>
<blockquote>
<p>I don’t know the intricacies of Elixir lang, but I would try to make it understandable on a english language level (that’s why the Python version is so understandable in my opinion).</p>
<p>Uninformed &amp; probably silly proposal:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for map_reduce i &lt;- [1, 2, 3], acc: {count, sum} = {0, 0} do
</code></pre>
</blockquote>
</aside>
<p>The proposal explains why it is important for the variables to be declared at the beginning, instead of at the end, but I also want to discuss the point you bring about <code>map_reduce</code>.</p>
<p>In my mind, “for” already means “map”, because that’s what it does by default. So “for map_reduce” is, in a way, the equivalent to “map map_reduce”. Also, I am worried that “map_reduce” doesn’t mean much for someone who doesn’t know what it means (but perhaps that should not be a reason to not use it). Other words that we considered were using <code>for reduce</code> instead of <code>for let</code> and <code>for reduce_only</code> to mean only reducing.</p>
<p>My first thought was actually to call it <code>for with</code>, to mean I want to do a <code>for</code> with these variables, but that would be ultimately very confusing with the <code>with</code> special form. I also considered <code>for using</code>, to mean I want to do a <code>for</code> using these variables. However, given the way to introduce variables in many imperative and functional languages is via <code>let</code>, that’s where we landed at the end. We also considered <code>for given</code>.</p>
<aside class="quote no-group" data-username="dimitarvp" data-post="20" data-topic="44773">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/48/38664_2.png" class="avatar"> dimitarvp:</div>
<blockquote>
<p>To me the <code>let</code> word carries the imperative / mutable connotation still. Has it been considered that using e.g. <code>acc</code> and not <code>let</code> might be steering the reader into the right way of thinking about this new <code>for</code> syntax?</p>
</blockquote>
</aside>
<p><code>acc</code> is functional jargon and an abbreviation. We could call it <code>accumulate</code> but maybe that’s too long? We could consider synonyms to accumulate too, such as <code>gather</code> and <code>collect</code>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex&gt; for accumulate(sum = 0), i &lt;- [1, 2, 3] do
...&gt;   sum = sum + i
...&gt;   {i * 2, sum}
...&gt; end
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex&gt; for gather(sum = 0), i &lt;- [1, 2, 3] do
...&gt;   sum = sum + i
...&gt;   {i * 2, sum}
...&gt; end
</code></pre>
<p>Then the follow up question is: would <code>for accumulate</code> and <code>for reduce</code> be clear enough to say which one is a <code>map_reduce</code> and which one is a <code>reduce</code>?</p>
<p>Also see above in this reply for a discussion on the names considered so far. <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>
<p>EDIT: Oh, Haskell calls it <code>mapAccum</code> instead of <code>mapFold</code>/<code>mapReduce</code>…</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="236984" 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/introducing-for-let-and-for-reduce/44773/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-236984" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="236984"
                     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/44773/load_more?page=3">Load more posts (103 remaining)</a>
</div></template></turbo-stream>