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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="axelson" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/axelson/120/26351_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  axelson
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Scenic Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You’re second example pretty much works as is, I occasionally use a similar pattern.</p>
<p>I just added an extra paren for the “Space Cake” clause:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    inventory = [%{id: 1, name: "Space Cake", count: 11}]

    cond do
      moon_cake = Enum.find(inventory, &amp;(&amp;1.name == "Moon Cake")) -&gt;
        {:eat, moon_cake.id}

      ((space_cake = Enum.find(inventory, &amp;(&amp;1.name == "Space Cake")))[:count] || 0) &gt; 10 -&gt;
        {:eat, space_cake.id}

      true -&gt;
        nil
    end
</code></pre>
<p>Although if you can structure it as a single reduce it will be more performant (although you’ll need quite a long list before that will begin to matter much), but turning this into a reduce might make it more difficult to follow since it doesn’t seem like the operation you are doing is easily thought of as a transformation. Also if the clause is as complex as the “Space Cakes” clause I would probably think about restructuring the logic away from a <code>cond</code>, or maybe extracting a helper function.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="191295" 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/a-hybrid-between-a-cond-statement-and-a-multi-does-it-exist/34738/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-191295" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="191295"
                     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="191386" data-post-id="191386">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="vans163" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/vans163/120/13216_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  vans163
                    <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>Wow cool. How did I miss that.</p>
<p>Another thing missing thought is being able to treat it kinda like a fusion with a <code>with</code> statement, in that earlier assignments can be accessed later.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    cond do
      moon_cake = Enum.find(inventory, &amp;(&amp;1.name == "Moon Cake")) -&gt;
        {:eat, moon_cake.id}

      ((space_cake = Enum.find(inventory, &amp;(&amp;1.name == "Space Cake")))[:count] || 0) &gt; 10 -&gt;
        {:eat, space_cake.id}

      !!space_cake and space_cake.count &gt; 5 -&gt;
        {:cook, space_cake.id}

      true -&gt;
        nil
    end
</code></pre>
<p>Probably not so clean but just using for example purposes.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="191386" 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/a-hybrid-between-a-cond-statement-and-a-multi-does-it-exist/34738/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-191386" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="191386"
                     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="191440" data-post-id="191440">
  <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">
								<p>Often I use what I call the “reverse with” form:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">with :error &lt;- try_this(),
     :error &lt;- try_that(),
     :error &lt;- try_other_stuff() do
  :error
else
  {:ok, _} = ok -&gt; ok
end
</code></pre>
<p>In your case, that could be:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">with nil &lt;- Enum.find(inventory, &amp;(&amp;1.name == "Moon Cake")),
     nil &lt;- Enum.find(inventory, &amp;(&amp;1.name == "Space Cake" &amp;&amp; (&amp;1[:count] || 0) &gt; 10)) do
  nil
else
  item -&gt; {:eat, item.id}
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="191440" 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/a-hybrid-between-a-cond-statement-and-a-multi-does-it-exist/34738/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-191440" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="191440"
                     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="191442" data-post-id="191442">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi, I believe you’re trying to find a language feature that will exactly solve your particular business problem. I think that there’s an inherent mismatch with this approach as languages provide low-level tools that can be composed together to solve bigger business problems, they do not provide off-the-shelf solutions for every different business problem imaginable.</p>
<p>I believe that you could use most of the solutions already mentioned as most of them are quite extendable and can be made to fit the current problem as well as future changing requirements (like the Blackhole Cake curve balls that business people are so adept at throwing when you least expect them).</p>
<p>For what it’s worth, I believe you could break your problem down into 2 separate phases, the discovery and the action phases. You can <code>Enum.reduce/3</code> over you inventory taking notes of which items you discovered (so all items, not a quick exit) and then decide on the importance/precedence of those findings with a simple <code>if/else</code> (or similar) decision making 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="191442" 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/a-hybrid-between-a-cond-statement-and-a-multi-does-it-exist/34738/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-191442" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="191442"
                     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="191480" data-post-id="191480">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="vans163" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/vans163/120/13216_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  vans163
                    <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>Yea this can work to an extent but its hard to read. I tried this pattern before but did not like that it reads very poorly and its hard to figure out what is going on since all the implications of the with (else clause and silently returning the match failure by default; I feel the last part causes chaos in a loosely typed language)</p>
<p>its also possible to assign then check in the with as well</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">inventory = [%{id: 1, name: "Space Cake", count: 11}]
with moon_cake &lt;- Enum.find(inventory, &amp; &amp;1.name == "Moon Cake"),
     true &lt;- is_nil(moon_cake) || {:eat, moon_cake.id},
     space_cake &lt;- Enum.find(inventory, &amp; &amp;1.name == "Space Cake"),
     true &lt;- is_nil(space_cake) || space_cake.count &lt; 10 || {:eat, space_cake.id} do 
end
</code></pre>
<p><a class="mention" href="/u/bottlenecked" rel="nofollow">@bottlenecked</a></p>
<p>Yea definitely, but the with statement was created for exactly that.  So if there is a nice way to solve a pattern it seems plausible to consider it at least as a feature.  The reason why creating multiple guard-like functions does not work is that there are just so many variations and combinations that you end up with small functions that get passed 5+ arguments, then you start thinking maybe I should just pass a state object to each, which then leads to nightmare refactors if that state object structure changes. (Same nightmare refactor problem I see with ecto multis, tho the new 1.11 map key precompile checks might remedy this abit)</p>
<p>You can  <code>Enum.reduce/3</code>.. yes this is a fine pattern and occasionally we use it but its still not ideal from a readability perspective IMO. Readability and maintainability is always subject to debate tho.  On this line maybe to take this a step further, we should redesign our thinking and perhaps always use this type of pattern with a weight assigned to each action result.  Then sort by weight and take the first. Kind of a GOAP like pattern.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="191480" 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/a-hybrid-between-a-cond-statement-and-a-multi-does-it-exist/34738/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-191480" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="191480"
                     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>