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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="iamafanasyev" data-post="1" data-topic="55973">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/iamafanasyev/48/30587_2.png" class="avatar"> iamafanasyev:</div>
<blockquote>
<p>it [for comprehension] works <em><strong>only for lists</strong></em></p>
</blockquote>
</aside>
<pre data-code-wrap="elixir"><code class="lang-elixir">pixels = &lt;&lt;213, 45, 132, 64, 76, 32, 76, 0, 0, 234, 32, 15&gt;&gt;
for &lt;&lt;r::8, g::8, b::8 &lt;- pixels&gt;&gt;, do: {r, g, b}
#⇒ [{213, 45, 132}, {64, 76, 32}, {76, 0, 0}, {234, 32, 15}]
</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="289060" 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/bindable-general-purpose-for-comprehension-at-your-service/55973/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-289060" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="289060"
                     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="289071" data-post-id="289071">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="iamafanasyev" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/iamafanasyev/120/30587_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  iamafanasyev
                    <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>Nice!</p>
<p>However, we can discuss this case, since streams are lazy:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">λ (stream x &lt;- [1, 2], x &gt; 2, y &lt;- [0], do: x * y) |&gt; Enum.to_list                 
[]
λ (stream x &lt;- [1, 2], x &gt; 2, y &lt;- [raise "boom"], do: x * y) |&gt; Enum.to_list
** (RuntimeError) boom
</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="289071" 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/bindable-general-purpose-for-comprehension-at-your-service/55973/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-289071" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="289071"
                     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="289075" data-post-id="289075">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>That’s a common misconception. <code>for</code> is always translated into <code>Enum.reduce</code> and <code>:lists.reverse</code>. You can check the debug info in the beam file</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(1)&gt; defmodule X do
...(1)&gt;   def f(list) do
...(1)&gt;     for x &lt;- list, do: x
...(1)&gt;   end
...(1)&gt; end
{:module, X,
 &lt;&lt;70, 79, 82, 49, 0, 0, 5, 220, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 206,
   0, 0, 0, 22, 8, 69, 108, 105, 120, 105, 114, 46, 88, 8, 95, 95, 105, 110,
   102, 111, 95, 95, 10, 97, 116, 116, 114, ...&gt;&gt;, {:f, 1}}
iex(2)&gt; {_, _, b, _} = v
iex(3)&gt; :beam_lib.chunks b, [:abstract_code]
...
       {:function, 2, :f, 1,
        [
          {:clause, 2, [{:var, 2, :_list@1}], [],
           [
             {:call, 3, {:remote, 3, {:atom, 3, :lists}, {:atom, 3, :reverse}},
              [
                {:call, 3, {:remote, 3, {:atom, 3, Enum}, {:atom, 3, :reduce}},
                 [
                   {:var, 3, :_list@1},
                   {nil, 3},
                   {:fun, 3,
                    {:clauses,
                     [{:clause, 3, [{:var, 3, ...}, {:var, ...}], [], [{...}]}]}}
                 ]}
              ]}
           ]}
        ]}
...
</code></pre>
<p>I have a project which optimizes such behaviour: <a href="https://github.com/hissssst/tria" class="inline-onebox" rel="noopener nofollow ugc">GitHub - hissssst/tria: Elixir optimizing compiler · GitHub</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="289075" 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/bindable-general-purpose-for-comprehension-at-your-service/55973/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-289075" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="289075"
                     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="289077" data-post-id="289077">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I got your point, and I’ve provided an example to show why nobody uses <code>Stream.resource</code> for managing resources and why it’s actually a bad idea. <code>Stream.resource</code> is an optimistic way to manage resources, and it has a lot of flaws (like leaving resource open in case of infinite recursion bug, memory limit, etc.) . As I’ve said, an idiomatic way to manage resources is using a separate monitoring process.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="289077" 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/bindable-general-purpose-for-comprehension-at-your-service/55973/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-289077" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="289077"
                     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="289172" data-post-id="289172">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="iamafanasyev" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/iamafanasyev/120/30587_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  iamafanasyev
                    <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">
								<aside class="quote no-group" data-username="hst337" data-post="15" data-topic="55973" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/h/41988e/48.png" class="avatar"> hst337:</div>
<blockquote>
<p><code>Stream.resource</code> is an optimistic way to manage resources, and it has a lot of flaws (like leaving resource open in case of infinite recursion bug, memory limit, etc.)</p>
</blockquote>
</aside>
<p>Seems like we can safely refactor timeout-divergence into separate risk. Say we have some facility to run the expression evaluation within the provided timeout, so we can always apply time bound <em>externally</em> (e.g. on the client side). By the way, using <code>Stream.resource/3</code>/<code>try-rescue</code> in <a href="https://forum.elixirforum.com/t/withtimeout-both-total-and-time-limited-evaluation-of-expressions" rel="nofollow">WithTimeout</a> looks safe, since the only thing performed on the <em>resource</em> (<code>Task.Supervisor</code>) is spawning task to await.</p>
<aside class="quote no-group" data-username="hst337" data-post="8" data-topic="55973">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/h/41988e/48.png" class="avatar"> hst337:</div>
<blockquote>
<p>For more on resource management, I’d suggest reading libraries <code>NimblePool</code> and <code>DBConnection</code> since they’re doing exactly what I’ve described above.</p>
</blockquote>
</aside>
<p>Good insights. For example, the one on a data copying concern (addressed in <code>DBConnection.Holder</code>). Again as a sidenote — <code>Stream.resource/3</code>/<code>try-rescue</code> does everything in-place, so no cost on copy messages.</p>
<aside class="quote no-group" data-username="hst337" data-post="8" data-topic="55973">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/h/41988e/48.png" class="avatar"> hst337:</div>
<blockquote>
<p>It handles hardware outages (sic!!!), because we can spawn the working process on the separate node, and when the separate node has hardware outage, we will receive either exit signal or a timeout in receive</p>
</blockquote>
</aside>
<p>That is tough! Agree, an existing library for such case would be Elixir (Erlang) itself.</p>
<p>To sum up on fault tolerance:</p>
<ul>
<li>While <code>Resource</code> uses <code>Stream.resource/3</code> under the hood, it could be seen as a <em>solution for the same class of problems</em>, one can approach with <code>Stream.resource/3</code> (up to resource streaming necessity);</li>
<li>timeout-divergence <em>could</em> be addressed on demand (applying time bounds on the resource usage).</li>
</ul>
<p>And yes, <code>Resource</code> is not the way you should approach highly specific <em>resource management</em> domains (e.g. HTTP connection pools). Now I see, that <code>Resource</code>’s name could be slightly misleading in such a context. Its focus is mainly <strong>not</strong> on the <em>resource object</em> itself (there could be no “object” at all), but on the “bracketing-the-usage” ceremony (<em>do this</em> on acquire, <em>do that</em> on release).</p>
<p>But I’ll try to dig more on <code>Stream.resource/3</code> backend alternative.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="289172" 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/bindable-general-purpose-for-comprehension-at-your-service/55973/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-289172" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="289172"
                     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="310998" data-post-id="310998">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="iamafanasyev" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/iamafanasyev/120/30587_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  iamafanasyev
                    <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>Hey, guys, I’m back <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>Thanks for <a class="mention" href="/u/hst337" rel="nofollow">@hst337</a> good points, I was able to release <a href="https://hexdocs.pm/bindable/Bindable.html" rel="noopener nofollow ugc">1.0.0 and 1.1.0 versions</a> of the library:</p>
<ul>
<li>1.0.0 introduces an Elixir way to write for-comprehension (yep, macro application to <code>Kernel.SpecialForms.for/1</code> solves the variadic-problem);</li>
<li>1.1.0 extends 1.0.0 with pattern matching filtration semantics across generated values (that was really fun challenge to implement saving all native compile-time warnings[1]).</li>
</ul>
<p>So <em>now</em> the main focus of these releases was on extending already existing Elixir’s for-comprehension syntax to work with “other-for-comprehendable-structures” and both syntactically and effectively be as close as possible to <code>Kernel.SpecialForms.for/1</code> on <code>List</code>’s. As a result I end up with identical to Elixir’s for-comprehension syntax up to macro application of cause (well and <code>Kernel.SpecialForms.for/1</code>’s <em>exclusive</em> reduce syntax/semantics, there is no such in <code>bindable</code>-for):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">import Bindable.ForComprehension

xs =  [[], [2, 2], [3], [4]]
xys = [[1, 1], [2, 2], [3, 3], [4, 4]]
bindable for [x] &lt;- xs,      # pattern matching guard
             [^x, y] &lt;- xys, # pattern matching guard with ^ operator
             y &gt; 0,          # "conventional" guard
             do: [x, y]
# [[3, 3], [4, 4]]
</code></pre>
<p>As always happy to hear your thoughts and critics on the subject, cheers <img src="https://forum.elixirforum.com/images/emoji/apple/v.png?v=15" title=":v:" class="emoji" alt=":v:" loading="lazy" width="20" height="20"></p>
<p>[1] — <code>match?/2</code> <em>may</em> generate unused variable warnings, when the pattern contains (in some sense) “free” variables, e.g. <code>match?([x], [42])</code> or simply <code>match?(x, 42)</code>; function head definitions could also generate warnings about “unreachable head”, e.g. <code>fn x -&gt; 42; _ -&gt; :causes_warning end</code>; so the answer was <code>with/1</code>, that provides pattern matching, assigns <em><strong>and</strong></em> a (<code>do</code>-)scope to work across.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="310998" 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/bindable-general-purpose-for-comprehension-at-your-service/55973/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-310998" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310998"
                     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>