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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="dimitarvp" data-post="40" data-topic="55524">
<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>Zealotry and pyramid hierarchy are a sad fact of life of our profession. I’ve successfully challenged and changed the minds of CTOs and VPs of Engineering but it was a rarity, most people default to ego or traumatic past experiences and will ride that until the heat death of the Universe to justify what they do.</p>
</blockquote>
</aside>
<p>lol, well, yes, true that <img src="https://forum.elixirforum.com/images/emoji/apple/joy.png?v=15" title=":joy:" class="emoji" alt=":joy:" loading="lazy" width="20" height="20"></p>
<p>As for <code>with</code> I’m talking scenarios where you’re letting the user know what happened.  Something like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def generate_report(user, filename) do
  with {:ok, file} &lt;- read_read(filename),
       {:ok, csv} &lt;- parse_csv(file),
       {:ok, report} &lt;- generate_report(csv) do
    send_email(user, report)
  end
end

case generate_report(user, filename) do
  {:ok, _} -&gt; IO.puts("Sent to #{user.email}!")
  {:error, error} -&gt; IO.puts(error)
end
</code></pre>
<p>The consumer a wee bit contrived but like something like that.</p>
<p>The heat death I’d suffer is keeping side-effects out of pipelines and not so much if it’s with <code>with</code> or <code>Multi</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="286895" 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/conditional-piping-part-of-the-language/55524/42">Post #41</a>
	                </div>
	            </div>
              <div id="likers-container-286895" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="286895"
                     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 #41"></div>
  </section>
</div>
    <div class="postbit" id="286950" data-post-id="286950">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Gilou06" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Gilou06
                    <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>Here is my code. It handles user clicks within a LiveView (Surface) page.</p>
<p>You will notice that &amp;reset_slide_management/1 is called only if fold? is false, but fold_slide_menu is always piped through with !fold? as parameter.<br>
That’s my need : have some set / reset helper functions that I pipe through either always (plain pipe) or conditionally (maybe_do). I don’t want to put the conditional logic into the helper because the decision to apply or not is not of the helper business.<br>
I find easier to read that kind of code, but am very opened to change my habits.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def maybe_do(value, true, func) when is_function(func, 1), do: func.(value)
def maybe_do(value, _, _), do: value

def handle_event(
        "slide_navigator_menu_buttons" = _event,
        params,
        socket
      ) do

    socket =
      case params["button"] do

        "etc" -&gt;
          # Unfold menu when ... is pressed
          fold_slide_navigator(socket, false)

        "min" -&gt;
          # Fold menu when min is pressed
          fold_slide_navigator(socket, true)

        # Other cases not shown here
      end

    {:noreply, socket}
  end

def fold_slide_navigator(
        %{assigns: %{slide_navigator_menu_state: slide_navigator_menu_state}} = socket,
        fold?
      )
      when is_boolean(fold?) do
    assign(socket,
      slide_navigator_menu_state: put_in(slide_navigator_menu_state.folded, fold?)
    )
    # we reset slide management ONLY when this menu is unfolded
    |&gt; Tools.maybe_do(!fold?, &amp;reset_slide_management/1)
    # Slide_menu folds/unfolds opposite to slide_navigator state
    |&gt; fold_slide_menu(!fold?)
  end

  def reset_slide_management(socket) do
    assign(socket, slide_menu_state: @default_slide_menu_state)
    |&gt; reset_block_management()
  end

  def reset_block_management(socket) do
    assign(socket, block_menu_state: @default_block_menu_state, current_block: nil)
  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="286950" 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/conditional-piping-part-of-the-language/55524/43">Post #42</a>
	                </div>
	            </div>
              <div id="likers-container-286950" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="286950"
                     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 #42"></div>
  </section>
</div>
    <div class="postbit" id="286969" data-post-id="286969">
  <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">
								<p>I apologize if I’m misreading it. It looks to me like you don’t even need the conditional check. Assuming the “folded” state is the same as the <code>@default_slide_menu_state</code>, if the menu is in the default or folded state then resetting it does nothing. So why check? Just reset it. I’m not super familiar with Phoenix, but my understanding is that if the DOM doesn’t change then no reload occurs. So if the reset does not result in a change you shouldn’t have any performance penalty.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="286969" 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/conditional-piping-part-of-the-language/55524/44">Post #43</a>
	                </div>
	            </div>
              <div id="likers-container-286969" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="286969"
                     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 #43"></div>
  </section>
</div>
    <div class="postbit" id="286972" data-post-id="286972">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Gilou06" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Gilou06
                    <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>Hi,<br>
Your assumption is incorrect. Some menus are folded by default, some not.<br>
I really need to NOT call helpers depending on current GUI state. Also helpers call each other, see the code: reset_slide calls reset_block, so calling them all the time leads to incorrect behaviour.</p>
<p>Also, I don’t want the helpers to take decisions. I have written those to make the code cleaner and less redundant.</p>
<p>I did not publish the real code at first because I have the feeling that my need is quite generic:</p>
<p>1/ event handlers that compute decision making based on user event and current genserver state. Those call mutation helpers based on the decision.<br>
2/ bunch of mutation helpers, with no decision making</p>
<p>I wanted to be able to write those event handlers in a clean and concise way. That why I came up with the maybe_do, but as I said before, since I’m quite new in Elixir I wanted to ask the community.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="286972" 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/conditional-piping-part-of-the-language/55524/45">Post #44</a>
	                </div>
	            </div>
              <div id="likers-container-286972" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="286972"
                     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 #44"></div>
  </section>
</div>
    <div class="postbit" id="287113" data-post-id="287113">
  <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">
								<p>If there is really just the one decision point then any option seems fine and within Elixir’s idiomatic style, including the way you’ve done it.</p>
<p>The <code>with</code> syntax would make your code something like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">with socket &lt;- assign(socket, slide_navigator_menu_state: put_in(slide_navigator_menu_state.folded, fold?),
       maybe_reset &lt;- Tools.maybe_do(!fold?, &amp;reset_slide_management/1) do 
         fold_slide_menu(maybe_reset, !fold?)
else
    _ -&gt; fold_slide_menu(socket, !fold?)
end 
</code></pre>
<p>This is clearly not an improvement with the style and structure and context you’re actually using. But if you have multiple transforms that could fail or multiple conditional branch points you might derive more benefit from the <code>with</code> approach.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">with {:ok, new_socket} &lt;- imaginary_failable_assign(socket, slide_navigator_menu_state: put_in(slide_navigator_menu_state.folded, fold?),
     {:reset_slide, reset_slide} &lt;- maybe_reset_slide_management(new_socket, fold?),
     {:reset_block, reset_block} &lt;- maybe_reset_block_management(reset_slide, fold?) do
         fold_slide_menu(reset_block, !fold?)
else
     {:assigns_err, assigns_err} -&gt; # log the error and return the original socket or whatever you like
     {:slide_not_reset, not_reset} -&gt; fold_slide_menu(not_reset, !fold?)
     {:block_not_reset, not_reset} -&gt; fold_slide_menu(not_reset, !fold?)
end
</code></pre>
<p>Again, with this particular case I don’t think that’s any real improvement, but if you like that granular control over logging messages or handling various branchpoints in one flow it can be nice.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="287113" 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/conditional-piping-part-of-the-language/55524/46">Post #45</a>
	                </div>
	            </div>
              <div id="likers-container-287113" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287113"
                     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>