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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="fceruti" data-post="11" data-topic="44781">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fceruti/48/28850_2.png" class="avatar"> fceruti:</div>
<blockquote>
<p>Could you provide some high level code?</p>
</blockquote>
</aside>
<p>Sure, sorry! I’m thinking something like</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Result do
  def tap(result, tapper) do
    case result do
      {:ok, value} = ok -&gt;
        tapper.(value)
        ok

      {:error, _} = error -&gt;
        error
    end
  end
end

# Usage

|&gt; Repo.transaction()
|&gt; Result.tap(fn %{goal: goal} -&gt; PubSub.broadcast_goal_created(user, goal) end)
</code></pre>
<p>I realized my earlier message was wrong because i thought you were mapping the current value but it looks you are only interested in “doing something with the value but keeping it as is”? That’s why i named it <code>tap</code> now like in the standard library.</p>
<p>Now this module should be able to handle every data in the shape <code>{:ok, data} | {:error, info}</code> and allow you to run some sideeffects in the case of <code>{:ok, _}</code> via <code>Result.tap/2</code>.</p>
<p>To be honest: If that is now better than your macro? Not really sure. But it’s a function (and definitively very functional, see lots of other languages <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">). And also there might be libraries definining something like the <code>Result</code> module for you and it’s a bit more “standard”.</p>
<p>(Also: I wasn’t able to test this code, i hope i didn’t do a really stupid mistake somewhere…)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237059" 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/small-useful-macro-case-continue/44781/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-237059" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237059"
                     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="237197" data-post-id="237197">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="fceruti" data-post="4" data-topic="44781">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fceruti/48/28850_2.png" class="avatar"> fceruti:</div>
<blockquote>
<p>Yup, bug found! Too quick to publish (solved above) <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Timetask.Helpers.Macros do
  defmacro case_continue(prev, do: block) do
    quote do
      case unquote(prev) do
        unquote(block ++ [{:-&gt;, [], [[{:_, [], nil}], nil]}])
      end

      unquote(prev)
    end
  end
end
</code></pre>
<p>I must be missing a fancy optimization: how does this avoid running the code in <code>prev</code> twice?</p>
<p>Concrete example:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%Foo{name: "nothing in particular"}
|&gt; Repo.insert()
|&gt; case_continue do
  {:ok, foo} -&gt; IO.inspect(foo)
end
</code></pre>
<p>naively expanding the macro results in this code:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%Foo{name: "nothing in particular"}
|&gt; Repo.insert()
|&gt; case do
  {:ok, foo} -&gt; IO.inspect(foo)
  _ -&gt; nil
end

%Foo{name: "nothing in particular"}
|&gt; Repo.insert()
</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="237197" 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/small-useful-macro-case-continue/44781/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-237197" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237197"
                     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="237200" data-post-id="237200">
  <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>The macro should probably bind the inbound to a variable then case on it.</p>
<p>I would also suggest “match_or_continue” as a name because I think that’s a better name since you inject a default path</p>
<p>Another piping tool I’ve always wanted is some sort of “then if” statement.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237200" 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/small-useful-macro-case-continue/44781/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-237200" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237200"
                     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="237207" data-post-id="237207">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="fceruti" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fceruti/120/28850_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  fceruti
                    <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="al2o3cr" data-post="14" data-topic="44781">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/48/3457_2.png" class="avatar"> al2o3cr:</div>
<blockquote>
<p>I must be missing a fancy optimization: how does this avoid running the code in <code>prev</code> twice?</p>
</blockquote>
</aside>
<p>I don’t lol. I was just struggling with this. It was a surprise to see my pipe going back an forwards and somehow landing on it’s feet at the end. The test that I added to see the failure, was exactly what you mention, i.e  is count == 1?</p>
<aside class="quote no-group" data-username="ityonemo" data-post="15" data-topic="44781">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/48/11341_2.png" class="avatar"> ityonemo:</div>
<blockquote>
<p>The macro should probably bind the inbound to a variable then case on it.</p>
</blockquote>
</aside>
<p>Taken from the docs:  <code>:bind_quoted</code> can be used in many cases and is seen as good practice, not only because it helps <strong>prevent us from running into common mistakes</strong> <img src="https://forum.elixirforum.com/images/emoji/apple/face_with_head_bandage.png?v=15" title=":face_with_head_bandage:" class="emoji" alt=":face_with_head_bandage:" loading="lazy" width="20" height="20"><img src="https://forum.elixirforum.com/images/emoji/apple/sweat_smile.png?v=15" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"></p>
<p>I can’t seem to fix it thou, I feel like I’m 95% there, but gotta go now. Happy holidays to everyone helping me out here <img src="https://forum.elixirforum.com/images/emoji/apple/grinning_face.png?v=15" title=":grinning_face:" class="emoji" alt=":grinning_face:" loading="lazy" width="20" height="20"><img src="https://forum.elixirforum.com/images/emoji/apple/christmas_tree.png?v=15" title=":christmas_tree:" class="emoji" alt=":christmas_tree:" loading="lazy" width="20" height="20"><img src="https://forum.elixirforum.com/images/emoji/apple/santa_claus/2.png?v=15" title=":santa_claus:t2:" class="emoji" alt=":santa_claus:t2:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Timetask.Helpers.Macros do
  defmacro maybe_case(prev, do: block) do
    quote bind_quoted: [prev: prev] do
      case prev do
        unquote(block ++ [{:-&gt;, [], [[{:_, [], nil}], nil]}])
      end

      prev
    end
  end
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="237207" 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/small-useful-macro-case-continue/44781/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-237207" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237207"
                     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="237208" data-post-id="237208">
  <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>unfortunately, using bind_quoted disables the use of unquotes.  So you might have to do this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defmacro maybe_case(prev, do: block) do
    quote do
      prev = unquote(prev)
      case prev do
        unquote(block ++ [{:-&gt;, [], [[{:_, [], nil}], nil]}])
      end
    end
  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="237208" 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/small-useful-macro-case-continue/44781/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-237208" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237208"
                     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>