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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="OvermindDL1" data-post="10" data-topic="3149">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/48/2677_2.png" class="avatar"> OvermindDL1:</div>
<blockquote>
<p>I’m often lazy and just put a case between things…  &gt;.&gt;</p>
</blockquote>
</aside>
<p>Woah. Did not realize you can do this. So much more readable than anon functions. Time to go clean up some code <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="32891" 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/how-to-check-for-a-condition-inside-a-pipe-chain/3149/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-32891" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="32891"
                     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="183859" data-post-id="183859">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Bringing the discussion up, as it’s been a while I felt like I was missing something, and I may finally have nailed it.</p>
<h3><a name="p-183859-context-1" class="anchor" href="#p-183859-context-1" aria-label="Heading link" rel="nofollow"></a>Context</h3>
<p>As far as I understand, the philosophy of a functional programming language is about <em>data transformation</em>.</p>
<p>Hence the pipe operator is amazing in Elixir as it is exactly what it does:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">initial_data
|&gt; first_transformation()
|&gt; second_transformation()
|&gt; third_transformation_with_params(my_param)
|&gt; another_one()
# final_data
</code></pre>
<p>But there are cases where the transformation should be applied conditionally, which is what is discussed here.</p>
<p>Of course, one can write another sub-method like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def maybe_apply_second_transformation(data, true), do: second_transformation(data)
def maybe_apply_second_transformation(data, false), do: data

initial_data
|&gt; first_transformation()
|&gt; maybe_apply_second_transformation(should_apply)
|&gt; third_transformation_with_params(my_param)
|&gt; another_one()
</code></pre>
<p>But that’s way too much extra code for a simple condition.</p>
<p>So I like <a class="mention" href="/u/overminddl1" rel="nofollow">@OvermindDL1</a> quite a lot:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">initial_data
|&gt; first_transformation()
|&gt; case do
  data when should_apply -&gt; second_transformation(data)
  data -&gt; data
end
</code></pre>
<p>But this syntax is quite heavy for a simple <code>if</code>, the <code>data -&gt; data</code> line doesn’t bring any valuable information, not perfect IMHO.</p>
<h3><a name="p-183859-question-2" class="anchor" href="#p-183859-question-2" aria-label="Heading link" rel="nofollow"></a>Question</h3>
<p>Shouldn’t this be valid:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">initial_data
|&gt; first_transformation()
|&gt; (if should_apply, do: second_transformation())
|&gt; third_transformation_with_params(my_param)
|&gt; another_one()
</code></pre>
<p>I know it doesn’t now, I guess, the <code>data</code> is given as the first parameter of <code>if</code>, instead of <code>should_apply</code>.</p>
<p>But when writing a <code>if</code> in a pipeline, it is more useful to get the <code>data</code> as the first parameter of the <code>do</code> block than of the <code>if</code> condition.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># Not useful
boolean
|&gt; if do: data

# More useful Useful
data
|&gt; if boolean, do: fn d -&gt; d end
# data
</code></pre>
<p>If the <code>if</code> operator shouldn’t be used for backward compatibility reasons, shouldn’t we imagine another one, like <code>if_map</code>? Or even a syntax like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">initial_data
|&gt; first_transformation()
|&gt;? should_apply, do: second_transformation()
|&gt; third_transformation_with_params(my_param)
|&gt; another_one()
</code></pre>
<p>Thanks for reading and feedback</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="183859" 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/how-to-check-for-a-condition-inside-a-pipe-chain/3149/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-183859" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="183859"
                     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="183862" data-post-id="183862">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="augnustin" data-post="14" data-topic="3149">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/augnustin/48/14538_2.png" class="avatar"> augnustin:</div>
<blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">def maybe_apply_second_transformation(data, true), do: second_transformation(data)
def maybe_apply_second_transformation(data, false), do: data

initial_data
|&gt; first_transformation()
|&gt; maybe_apply_second_transformation(should_apply)
|&gt; third_transformation_with_params(my_param)
|&gt; another_one()
</code></pre>
<p>But that’s way too much extra code for a simple condition.</p>
</blockquote>
</aside>
<p>I feel that statement is the trap here. Instead of naming it <code>maybe_apply_second_transformation</code> I’d suggest something like <code>prepare_for_third_transformation</code> (maybe inline <code>second_transformation(data)</code>) and then the code is totally fine. Most importantly that code will age the best. Say at some later point you also need to do some different transformation for the <code>false</code> case as well. Just replace <code>data</code> with whatever that other case needs to be applied to.</p>
<p>All your proposed short inline forms will likely no longer be short and sweet once you need to deal with 2 cases and once you’re no longer at 1 or 2 cases you’ll need to switch to the function (or case) version anyways.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="183862" 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/how-to-check-for-a-condition-inside-a-pipe-chain/3149/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-183862" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="183862"
                     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="183866" data-post-id="183866">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>As a possible alternative to writing a full fledged <code>maybe_*</code> function, you can also consider to use a general purpose <code>maybe_if/3</code> function. I’ve done that in the past when I needed to chain multiple optional steps.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">initial_data
|&gt; first_transformation()
|&gt; maybe_if(should_apply_step2, &amp;second_transformation/1)
|&gt; maybe_if(should_apply_step3, &amp;third_transformation_with_params(&amp;1, my_param))
|&gt; another_one()
</code></pre>
<p>where <code>maybe_if</code> is defined like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def maybe_if(data, true, action)
  when is_function(action, 1),
  do: apply(action, data)

def maybe_if(data, false, _action), do: data
</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="183866" 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/how-to-check-for-a-condition-inside-a-pipe-chain/3149/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-183866" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="183866"
                     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="183876" data-post-id="183876">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks for feedback that’s very interesting.</p>
<p><a class="mention" href="/u/wolf4earth" rel="nofollow">@wolf4earth</a> I like your solution very much, I shall go for it. <img src="https://forum.elixirforum.com/images/emoji/apple/smile.png?v=15" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>
<blockquote>
<p>All your proposed short inline forms will likely no longer be short and sweet once you need to deal with 2 cases and once you’re no longer at 1 or 2 cases you’ll need to switch to the function (or case) version anyways.</p>
</blockquote>
<p><a class="mention" href="/u/lostkobrakai" rel="nofollow">@LostKobrakai</a> I don’t really agree  with your point.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">data
|&gt; first_transformation()
|&gt; if should_apply, 
      do: second_transformation(), 
      else: alternate_transformation()
|&gt; third_transformation()
</code></pre>
<p>reads much clearer IMHO than</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">data
|&gt; first_transformation()
|&gt; case do
  data when should_apply -&gt; second_transformation(data)
  data -&gt; alternate_transformation(data)
end
|&gt; third_transformation()
</code></pre>
<p>My thinking made me understand that in order to get my ideal version to work, one should write the <code>if/4</code> macro (from <a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel.ex#L3293" rel="noopener nofollow ugc"><code>if source code</code></a>). Apologies I suck at macros… <img src="https://forum.elixirforum.com/images/emoji/apple/disappointed.png?v=15" title=":disappointed:" class="emoji" alt=":disappointed:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defp build_if(nil, condition, do: do_clauses, else: else_clauses) do
    build_if(condition, do: do_clauses, else: else_clauses)
  end

  defp build_if(entry, condition, do: do_clauses, else: else_clauses) do
    quote do
      if(condition, entry |&gt; unquote(do_clauses), entry |&gt; unquote(else_clauses))
    end
  end
</code></pre>
<p>Though my code may not be correct, couldn’t it work?</p>
<p>The cool thing is that from my understanding, it could be backward compatible, as it is easy to detect that <code>entry</code> is <code>nil</code>, so the very same operator <em>could</em> be used (though it may be confusing at first).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="183876" 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/how-to-check-for-a-condition-inside-a-pipe-chain/3149/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-183876" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="183876"
                     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="183878" data-post-id="183878">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="augnustin" data-post="17" data-topic="3149">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/augnustin/48/14538_2.png" class="avatar"> augnustin:</div>
<blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">data
|&gt; first_transformation()
|&gt; if should_apply, 
      do: second_transformation(), 
      else: alternate_transformation()
|&gt; third_transformation()
</code></pre>
<p>reads much clearer IMHO than</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">data
|&gt; first_transformation()
|&gt; case do
  data when should_apply -&gt; second_transformation(data)
  data -&gt; alternate_transformation(data)
end
|&gt; third_transformation()
</code></pre>
</blockquote>
</aside>
<p>I’d say so as well, but imo non of that needs to be handled as part of the pipeline. The pipeline should only be concerned with the steps needing to happen, but not with what happens as part of those steps – and doing nothing is a valid option for the latter.</p>
<p>For your examples <code>second_transformation</code> and <code>alternate_transformation</code> are both versions of <code>some_transformation_needed_before_the_third_transformation</code>, which should be what’s piped into.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="183878" 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/how-to-check-for-a-condition-inside-a-pipe-chain/3149/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-183878" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="183878"
                     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="191917" data-post-id="191917">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I guess I’m not yet comfortable with the expressiveness of idiomatic Elixir code.</p>
<p>Take this <a href="https://stackoverflow.com/a/63898855/1620081" rel="noopener nofollow ugc">SO question</a>.</p>
<p>It seems like the solution in Elixir is to always create more functions, whereas I’m a one-liner fan, as context always varies, and on-the-fly definitions seem more appropriate to me than more code lines.</p>
<p>But this would be a long topic to discuss!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="191917" 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/how-to-check-for-a-condition-inside-a-pipe-chain/3149/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-191917" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="191917"
                     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>