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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>You’ll often see the <code>acc</code> last, rather than first, in your example.</p>
</blockquote>
<p>Totally agree.</p>
<p>To provide more detail, I structured it specifically to adapt for <a href="https://hexdocs.pm/elixir/1.16.2/Agent.html#get_and_update/5" rel="noopener nofollow ugc"><code>Agent.get_and_update/5</code></a>, i.e. to do this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">@spec next(n :: integer) :: integer
def next(n) do
  Agent.get_and_update(__MODULE__, RLEIterator, :rle_next, [n])
end
</code></pre>
<ul>
<li>I changed the name from <code>do_next/2</code> to (public) <code>rle_next/2</code> to distinguish against the <code>do_*</code> pattern. I felt it isn’t a traditional reducer/accumulator. Felt more akin to something like <code>Map.pop/3</code>, where I return some value alongside the “updated” data structure in a tuple (i.e. <code>{a, state()}</code></li>
<li>For <code>args :: [term()]</code>, the state is added first, i.e. the state list needs to be the first argument passed to <code>do_next/2</code>/<code>rle_next/2</code>. So <code>[n]</code> effectively becomes <code>[state, n]</code> → <code>rle_next(state, n)</code> (similar to the recursive case).</li>
<li><code>Agent.get_and_update/5</code> (and <code>/3</code>) expect the return to be <code>{a, state()}</code>, which is why the return is in that same ordered tuple (like mentioned above, similar to the likes of <code>Map.pop/3</code>)</li>
</ul>
<blockquote>
<p>This is also a reasonable use-case for the (often discouraged in production use) process dictionary</p>
</blockquote>
<p>For sure. I chose <code>Agent</code> since common behaviors like <code>get_and_update</code> are provided “for free” and just what I was more familiar with.<br>
But <code>Process.put/2</code> for <code>init_/1</code> could be cleaner since I think you can call it the same the first time and subsequent times.<br>
Imagine you could do something like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule RLEIterator do
  def init_(encoding), do: Process.put(__MODULE__, encoding)

  def next(n) do
    Process.get(__MODULE__)
    |&gt; rle_next(n)
    |&gt; then(fn {val, encoding} -&gt;
      Process.put(__MODULE__, encoding)
      val
    end)
  end
 
  def rle_next(encoding, n)
  # ...
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="324750" 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-use-genserver-in-leetcode/43064/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-324750" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="324750"
                     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>