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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="28" 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/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>The goal is to initialize variables to be used as state during the <code>for</code>. All variables initialized as part of the <code>for</code> must then be returned inside <code>do-end</code> block.</p>
</blockquote>
</aside>
<p>One idea that came to me yesterday was</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for binding sum = 0, x &lt;- [1, 2, 3] do
  {x * 2, sum + x}
end
</code></pre>
<p>It works on 3 levels:</p>
<ul>
<li>you are binding the variables’ initial values, with full pattern matching support</li>
<li>you are “binding” these values between iterations of the comprehension</li>
<li>we can refer to variables declared as such as “for-bindings”, which is better than the very poor “imperative variables” I have been calling them in my head throughout these proposals</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="237112" 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/introducing-for-let-and-for-reduce/44773/53">Post #52</a>
	                </div>
	            </div>
              <div id="likers-container-237112" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237112"
                     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 #52"></div>
  </section>
</div>
    <div class="postbit" id="237116" data-post-id="237116">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="christhekeele" data-post="53" 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/christhekeele/48/1039_2.png" class="avatar"> christhekeele:</div>
<blockquote>
<p>One idea that came to me yesterday was</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for binding sum = 0, x &lt;- [1, 2, 3] do
  {x * 2, sum + x}
end
</code></pre>
</blockquote>
</aside>
<p>I was thinking <code>bind</code> instead of <code>binding</code> because there is already <code>binding/0/1</code>, so:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for bind sum = 0, x &lt;- [1, 2, 3] do
  {x * 2, sum + x}
end
</code></pre>
<p><img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji only-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="237116" 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/introducing-for-let-and-for-reduce/44773/54">Post #53</a>
	                </div>
	            </div>
              <div id="likers-container-237116" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237116"
                     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 #53"></div>
  </section>
</div>
    <div class="postbit" id="237118" data-post-id="237118">
  <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="thiagomajesk" data-post="52" 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/thiagomajesk/48/31904_2.png" class="avatar"> thiagomajesk:</div>
<blockquote>
<p>Hey <a class="mention" href="/u/josevalim" rel="nofollow">@josevalim</a>, no problem at all. I have to say that I’m kinda surprised, which kind of breakage are you talking about?</p>
</blockquote>
</aside>
<p>Constructs like <code>for</code> are called special forms, because they have to be implemented by the compiler for optimizations and so on. We only have a few of them. The issue is that you can’t define/import a function with the same name as a special form and the variations would most likely have to be a special form.</p>
<aside class="quote no-group" data-username="christhekeele" data-post="53" 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/christhekeele/48/1039_2.png" class="avatar"> christhekeele:</div>
<blockquote>
<p><code>for binding sum = 0, x &lt;- [1, 2, 3] do</code></p>
</blockquote>
</aside>
<p>I am not convinced <code>binding</code> or <code>bind</code> is any different than <code>let</code>? And only the first proposal was imperative but that has been thrown out of the window by now. <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="237118" 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/introducing-for-let-and-for-reduce/44773/55">Post #54</a>
	                </div>
	            </div>
              <div id="likers-container-237118" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237118"
                     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 #54"></div>
  </section>
</div>
    <div class="postbit" id="237119" data-post-id="237119">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I think the biggest mental blockers to me is that the accumulator is declared in the first term, and the iterator is in the second term, then your output is a tuple with the iteratee as the <em>first</em> term and the accumulator as the <em>second</em>.  This feels a bit un-elixirey because in reduce the enumerable/enumerated are both first term and the accumulator is the second term.</p>
<p>Of slightly less concern, but still a bit of a stumbling point for me was that there isn’t some sort of a semantic expectation that a tuple is being restructured as part of the result.  Maybe something like this might help:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for {n &lt;- enum, let s = 0} do ...
</code></pre>
<p>Don’t know if I love that either.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237119" 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/56">Post #55</a>
	                </div>
	            </div>
              <div id="likers-container-237119" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237119"
                     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 #55"></div>
  </section>
</div>
    <div class="postbit" id="237125" data-post-id="237125">
  <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="dorgan" data-post="51" 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/dorgan/48/22107_2.png" class="avatar"> dorgan:</div>
<blockquote>
<p>So in summary: the new syntax doesn’t feel wrong to me, and I’m interested in the new patterns it enables, but I’m not convinced that it reduces boilerplate, thus I’m not sure it would improve the situation for newcomers save for the “exit early” case.</p>
</blockquote>
</aside>
<p>It reduces a small amount of boilerplate in the original example but I agree it is not significant. My hope though is that it reduces the conceptual overhead of reaching to that solution in the first place.</p>
<p>However, I think there are examples where it reduces considerably. Today, as I worked, I tried to see examples where to apply it. Here are two.</p>
<p>This:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    Enum.flat_map_reduce(subs, sources, fn sub, sources -&gt;
      sub_formatter = Path.join(sub, ".formatter.exs")

      if File.exists?(sub_formatter) do
        formatter_opts = eval_file_with_keyword_list(sub_formatter)

        {formatter_opts_and_subs, sources} =
          eval_deps_and_subdirectories(:in_memory, [sub], formatter_opts, sources)

        {[{sub, formatter_opts_and_subs}], sources}
      else
        {[], sources}
      end
    end)
</code></pre>
<p>Can be rewritten to this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">   for let(sources),
        sub &lt;- subs,
        sub_formatter = Path.join(sub, ".formatter.exs"),
        File.exists?(sub_formatter) do
      formatter_opts = eval_file_with_keyword_list(sub_formatter)

      {formatter_opts_and_subs, sources} =
        eval_deps_and_subdirectories(:in_memory, [sub], formatter_opts, sources)

      {{sub, formatter_opts_and_subs}, sources}
    end
</code></pre>
<p>And this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def find_asset_info(notebook, hash) do
    Enum.find_value(notebook.sections, fn section -&gt;
      Enum.find_value(section.cells, fn cell -&gt;
        is_struct(cell, Cell.Elixir) &amp;&amp;
          Enum.find_value(cell.outputs, fn
            {:js_static, %{assets: %{hash: ^hash} = assets_info}, _data} -&gt; assets_info
            {:js_dynamic, %{assets: %{hash: ^hash} = assets_info}, _pid} -&gt; assets_info
            _ -&gt; nil
          end)
      end)
    end)
  end
</code></pre>
<p>to this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  for reduce(value = nil),
      value == nil,
      section &lt;- notebook.sections,
      %Cell.Elixir{} = cell &lt;- section.cells,
      output &lt;- cell.outputs do
    case output do
      {:js_static, %{assets: %{hash: ^hash} = assets_info}, _data} -&gt; assets_info
      {:js_dynamic, %{assets: %{hash: ^hash} = assets_info}, _pid} -&gt; assets_info
      _ -&gt; nil
    end
  end
</code></pre>
<p>As soon as you get any kind of nesting, the comprehension format really starts to stand out.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="55" 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/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>I am not convinced <code>binding</code> or <code>bind</code> is any different than <code>let</code>? And only the first proposal was imperative but that has been thrown out of the window by now. <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>
</blockquote>
</aside>
<p>I’m fine with <code>let</code> personally – and I think I understand why it was chosen – but in reading through all of the posts thus far, I saw there was some concern regarding <code>let</code> and some other suggestions etc. In light of that, <code>bind</code> just seemed more Elixir<em>ish</em> to me. <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="237127" 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/introducing-for-let-and-for-reduce/44773/58">Post #57</a>
	                </div>
	            </div>
              <div id="likers-container-237127" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237127"
                     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 #57"></div>
  </section>
</div>
    <div class="postbit" id="237128" data-post-id="237128">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I like the abilities that this proposal brings, but <code>let/reduce</code> are a bit of a non-obvious pair to my eyes. <code>map_reduce/reduce</code> seem more obvious, but I believe we’re trying to avoid the introduction of those words. It would be nice to either use a single word for both reduce and map reduce or no word at all. If we were to use a single word, <code>accum</code> makes pretty good sense to me.</p>
<p>So how do we make it such that no (or one) word is necessary? I don’t think this has been mentioned yet and perhaps for good reason <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"> … One option might be to let the shape of the return dictate whether it is effectively a reduce or a map reduce. In the case where an accumulator is included, a list must be returned. When the list includes two items it is a map reduce and with one item it is a reduce.</p>
<p>Effectively a map reduce</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex&gt; for sum = 0, i &lt;- [1, 2, 3] do
...&gt;   sum = sum + i
...&gt;   [i * 2, sum]
...&gt; end
[[2, 4, 6], 6]
</code></pre>
<p>Effectively a reduce</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex&gt; for sum = 0, i &lt;- [1, 2, 3] do
...&gt;   sum = sum + i
...&gt;   [sum]
...&gt; end
[6]
</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="237128" 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/introducing-for-let-and-for-reduce/44773/59">Post #58</a>
	                </div>
	            </div>
              <div id="likers-container-237128" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237128"
                     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 #58"></div>
  </section>
</div>
    <div class="postbit" id="237130" data-post-id="237130">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>What about <code>set</code>? And what about moving it into the KW list at the end?</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for i &lt;- [1, 2, 3], set: sum = 0 do
  {i * 2, sum + i}
end
</code></pre>
<p>That way if there is already a <code>sum</code>, it would be</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">sum = 0
for i &lt;- [1, 2, 3], set: sum = sum do
  {i * 2, sum + i}
end
</code></pre>
<p>Also, as a separate issue, (while consistent since plenty of other things do this in the language) I feel like without any type system implicitly returning the iterator value combined with the sum joined in a tuple is not intuitive. It would be nice to see syntax specifically for this form so that the implementation of it being returned as a tuple is not a common bug new users trip on. Creating a macro something like <code>using(i * 2, sum + i)</code> for the <code>for</code> return value would be best for readability and writability.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="55" 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/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>I am not convinced <code>binding</code> or <code>bind</code> is any different than <code>let</code>?</p>
</blockquote>
</aside>
<p>What I don’t really like about <code>let</code> is that it’s <em><strong>very</strong></em> specific to assignment. (<code>init</code> is even worse in this regard IMO.) <code>let var = 0</code> means <em>cause <code>var</code> to become <code>0</code></em>, but that’s only the surface of what this qualifier is doing in the proposal. Sure, it causes <code>var</code> to become <code>0</code>; but then it <em><strong>rebinds <code>var</code> each loop</strong></em> based on the output of the previous one.</p>
<p><code>for (let x = 0...</code> feels natural in JS, but that is because all the <code>let</code> keyword is doing semantically in that case is actual assignment. In our case, <code>let</code> is actually changing what happens as <code>for</code> generates values, by rebinding certain variables in the block’s scope before it is re-executed.</p>
<p>I guess, calling this qualifier <code>let</code> because it lets variables start off as something… before <em>rebinding them each loop</em>, feels a lot like if Elixir decided to call <code>=</code> the assignment operator, because it assigns to variables… if they were unbound as the end of a <em>very powerful pattern-match feature</em>. <code>let</code> is doing much more here, it deserves a better name! <img src="https://forum.elixirforum.com/images/emoji/apple/slightly_smiling_face.png?v=15" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:" loading="lazy" width="20" height="20"></p>
<p>I like <code>bind</code> because it means more literally “keep these things close together”, whereas <code>let</code> is a more dismissive, one-time thing. We are not just <code>let</code>ting var = 0, we are <code>bind</code>ing var to 0 and <em>binding the lifetime of <code>var</code> to the execution of the <code>for</code> loop</em>. I guess? It is hard to talk about how words feel in programming…</p>
<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="55" 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/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>And only the first proposal was imperative but that has been thrown out of the window by now. <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>
</blockquote>
</aside>
<p>That’s fair, shouldn’t bring it back into discussion!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237131" 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/introducing-for-let-and-for-reduce/44773/61">Post #60</a>
	                </div>
	            </div>
              <div id="likers-container-237131" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237131"
                     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 #60"></div>
  </section>
</div>
    <div class="postbit" id="237142" data-post-id="237142">
  <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 lost track of the message responding to my plug for more of a flat_map_reduce than flat_map, but since I’ve seen early halt come up again: I think my point was that the flat_map_reduce would allow us to put the acc <em>after</em> the generators and filters (which I feel reads more like the rest of Elixir) and still have a mechanism to filter or emit more values within the <code>do end</code>, which for more complicated logic I feel would be clearer than trying to cram it all into generators and filters.</p>
<p>Taking the reduce-as-a-complex-find example:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  for reduce(value = nil),
      value == nil,
      section &lt;- notebook.sections,
      %Cell.Elixir{} = cell &lt;- section.cells,
      output &lt;- cell.outputs do
    case output do
      {:js_static, %{assets: %{hash: ^hash} = assets_info}, _data} -&gt; assets_info
      {:js_dynamic, %{assets: %{hash: ^hash} = assets_info}, _pid} -&gt; assets_info
      _ -&gt; nil
    end
  end
</code></pre>
<p>That would look more like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  for section &lt;- notebook.sections,
      %Cell.Elixir{} = cell &lt;- section.cells,
      output &lt;- cell.outputs,
      acc: nil do
    acc -&gt;
      case output do
        {:js_static, %{assets: %{hash: ^hash} = assets_info}, _data} -&gt; {:halt, assets_info}
        {:js_dynamic, %{assets: %{hash: ^hash} = assets_info}, _pid} -&gt; {:halt, assets_info}
        _ -&gt; {[], acc}
      end
  end
  #=&gt; {[], assets_info | nil}
</code></pre>
<p>To me that seems easier to see what’s going on.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237142" 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/62">Post #61</a>
	                </div>
	            </div>
              <div id="likers-container-237142" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237142"
                     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 #61"></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=7">Load more posts (63 remaining)</a>
</div></template></turbo-stream>