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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/sergio" rel="nofollow">@sergio</a><br>
After messing with queues a lot today, here is what I dislike about <code>:queue</code>:</p>
<ol>
<li>The datatype it works with does not (and cannot) implement protocols.</li>
<li>The datatype it works with does not show to the user what it is: It is indistinguishable from any other two-tuple. Introspection is a great tool to help a new person who reads your code (or a stacktrace).</li>
<li>The <code>:queue</code> module has three different name aliases for most functions.</li>
<li>There are only a few functions that allow you to manipulate <code>:queue</code>s in an enumerable-like way. The only other approach you can take is to first convert it to a list.</li>
<li>It cannot be used in pipes as it follows Erlang’s convention to have the most significant argument as last argument.</li>
<li>Because it is an Erlang module, we do not have documentation for it in <code>IEx</code>.</li>
<li>insertion and extraction are <em>amortized</em> O(1). Its implementation means that if someone first inserts n = 100_000 items, then retrieving the first inserted item afterwards is going to mean that this list with a n = 100_000 elements first needs to be reversed. As <a class="mention" href="/u/overminddl1" rel="nofollow">@OvermindDL1</a> noted: List reversal is a native piece of code that is relatively fast; still I expect that at a certain <em>n</em>, it becomes too slow and too unpredictable, and a hard constant time variant should be used instead.</li>
</ol> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="36905" 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/elixir-needs-a-fifo-type/5701/33">Post #32</a>
	                </div>
	            </div>
              <div id="likers-container-36905" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="36905"
                     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 #32"></div>
  </section>
</div>
    <div class="postbit" id="36907" data-post-id="36907">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Qqwy" data-post="33" data-topic="5701">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qqwy/48/1349_2.png" class="avatar"> Qqwy:</div>
<blockquote>
<p>It cannot be used in pipes as it follows Erlang’s convention to have the most significant argument as last argument.</p>
</blockquote>
</aside>
<p>Like how every language with piping pipes?  Elixir is the odd one out here.  <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<p>Remember that Erlang has a last-place kind of piping call too with tuple-calls:  ^.^</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex&gt; defmodule Testering do
...&gt;   def blorp(x, {Testering, y}), do: x + y
...&gt; end
{:module, Testering,
 &lt;&lt;70, 79, 82, 49, 0, 0, 4, 228, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 164,
   131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115,
   95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...&gt;&gt;, {:blorp, 2}}
iex&gt; {Testering, 32}.blorp(10)
42
iex&gt; t = {Testering, 32}
{Testering, 32}
iex&gt; t.blorp(10)
42
</code></pre>
<p><em>/me really likes tuple calls, they are so useful in so many ways, especially as a form of first-class modules</em></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="36907" 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/elixir-needs-a-fifo-type/5701/34">Post #33</a>
	                </div>
	            </div>
              <div id="likers-container-36907" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="36907"
                     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 #33"></div>
  </section>
</div>
    <div class="postbit" id="36908" data-post-id="36908">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Oh yes, I am totally aware that Elixir is the ‘odd one out’. That being said, there is not really a ‘better choice’; it is an endianness-type of problem (in Haskell, which only has arity-1 functions, it definitely makes more sense to do significant-last because of currying, but Erlang does not support this and neither does Elixir). Elixir chose it differently from Erlang and Haskell, and we will need to cope with it because it will not change (in the forseeable future, and possibly ever). Therefore, something that adapts to Elixir’s form of calling would be preferred by me when writing Elixir 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="36908" 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/elixir-needs-a-fifo-type/5701/35">Post #34</a>
	                </div>
	            </div>
              <div id="likers-container-36908" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="36908"
                     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 #34"></div>
  </section>
</div>
    <div class="postbit" id="36913" data-post-id="36913">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Qqwy" data-post="35" data-topic="5701">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qqwy/48/1349_2.png" class="avatar"> Qqwy:</div>
<blockquote>
<p>Therefore, something that adapts to Elixir’s form of calling would be preferred by me when writing Elixir code.</p>
</blockquote>
</aside>
<p>Easy enough to fix.  ^.^</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule ToLast do
  defmacro to_last(arg, {fun, meta, args}) when is_list(args) do
    {fun, meta, args++[arg]}
  end
end
</code></pre>
<p>Used like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex&gt; import ToLast
ToLast
iex&gt; q = :queue.new()
{[], []}
iex&gt; q = q \
...&gt; |&gt; to_last(:queue.in(1)) \
...&gt; |&gt; to_last(:queue.in(2)) \
...&gt; |&gt; to_last(:queue.in(42))
{[42, 2], [1]}
iex&gt; :queue.out(q)
{{:value, 1}, {[42], [2]}}
</code></pre>
<p>Though would be nice if <code>|&gt;</code> could some how know (protocol? or query something for a list at compile-time?) or something like <code>~&gt;</code> could be used for the end.  ^.^</p>
<p>I could imagine a <code>defdelegate flip:true</code> or something like that to bring in a function and cast to another module but flip the last to the first argument.  That would be convenient to make an Elixir <code>Queue</code> module that delegates to Erlang’s <code>:queue</code> but flipping the order in only like 4 lines of 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="36913" 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/elixir-needs-a-fifo-type/5701/36">Post #35</a>
	                </div>
	            </div>
              <div id="likers-container-36913" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="36913"
                     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 #35"></div>
  </section>
</div>
    <div class="postbit" id="37257" data-post-id="37257">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/overminddl1" rel="nofollow">@OvermindDL1</a> Thank you for showing how to write a macro wrapper to flip the arguments passed to a function.</p>
<p>I do feel however that we’ve become a little side-tracked.</p>
<p>Even wrapping <code>:queue</code> like that does not fix its other shortcomings:</p>
<aside class="quote no-group quote-modified" data-username="Qqwy" data-post="33" data-topic="5701">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qqwy/48/1349_2.png" class="avatar"> Qqwy:</div>
<blockquote>
<ol>
<li>The datatype it works with does not (and cannot) implement protocols.</li>
<li>The datatype it works with does not show to the user what it is: It is indistinguishable from any other two-tuple. Introspection is a great tool to help a new person who reads your code (or a stacktrace).</li>
<li>The :queue module has three different name aliases for most functions.</li>
<li>There are only a few functions that allow you to manipulate :queues in an enumerable-like way. The only other approach you can take is to first convert it to a list.</li>
<li><del>It cannot be used in pipes as it follows Erlang’s convention to have the most significant argument as last argument.</del> (<em>Unless you wrap it as <a class="mention" href="/u/overminddl1" rel="nofollow">@OvermindDL1</a> described</em>)</li>
<li>Because it is an Erlang module, we do not have documentation for it in IEx.</li>
<li>insertion and extraction are amortized O(1). Its implementation means that if someone first inserts n = 100_000 items, then retrieving the first inserted item afterwards is going to mean that this list with a n = 100_000 elements first needs to be reversed. As <a class="mention" href="/u/overminddl1" rel="nofollow">@OvermindDL1</a> noted: List reversal is a native piece of code that is relatively fast; still I expect that at a certain n, it becomes too slow and too unpredictable, and a hard constant time variant should be used instead.</li>
</ol>
</blockquote>
</aside>
<p>I’m going to benchmark the hard real-time vesion of the queues and deques vs. the amortized versions (which includes <code>:queue</code>’s) to see how performant they are.</p>
<p>As an aside: I really want to create an <code>Traversable</code>(<code>Trav</code>?) for Elixir, to be used instead of <code>Enum</code> when you do not want to throw away the structure of the data you have. <img src="https://forum.elixirforum.com/images/emoji/apple/grin.png?v=15" title=":grin:" class="emoji" alt=":grin:" 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="37257" 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/elixir-needs-a-fifo-type/5701/37">Post #36</a>
	                </div>
	            </div>
              <div id="likers-container-37257" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="37257"
                     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 #36"></div>
  </section>
</div>
    <div class="postbit" id="37287" data-post-id="37287">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sribe" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sribe
                    <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>Right. Which is why, while the macro to flip arguments around is useful, it’s not that useful to have a macro that just does that to all exported functions. So one would want to wrap the queue with more explicit code:</p>
<ol>
<li>
<p>Are you sure you cannot implement Enumerable on top of a queue??? Admittedly, I haven’t tried it, but I don’t see why it’s not possible.</p>
</li>
<li>
<p>Clearly the exact kind of thing to solve with a wrapper: return {:queue, the_actual_queue}</p>
</li>
<li>
<p>An Elixir queue would clearly not replicate that.</p>
</li>
<li>
<p>Back to 1, if the Enum protocol can be implemented over a queue…</p>
</li>
<li>
<p>Yep, wrap it.</p>
</li>
<li>
<p>One of the reasons that I think Elixir needs its own deque. This is too fundamental of a data structure to expect people to rummage around in the Erlang docs.</p>
</li>
<li>
<p>Agreed.</p>
</li>
</ol>
<p>[edit] Bah humbug, got going too fast and forgot that I wanted to add my own 8, that the return types, and use of {:error, …} vs raising exceptions is not really Elixirish. And there are a handful of xxx! functions that should probably be added. At this point I’ve got a lot of it already written, but on top of deque, which I’m not sure is the absolute best choice.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="37287" 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/elixir-needs-a-fifo-type/5701/38">Post #37</a>
	                </div>
	            </div>
              <div id="likers-container-37287" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="37287"
                     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 #37"></div>
  </section>
</div>
    <div class="postbit" id="39504" data-post-id="39504">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Because this topic was the catalyst that started it: <a href="https://github.com/Qqwy/elixir_okasaki" rel="noopener nofollow ugc">Okasaki</a> has been released on Hex.PM!</p>
<p>It contains four different implementations of queues (two queues, two deques). In the near future, a wrapper around <code>:queue</code> will be added as well.</p>
<p><code>Okasaki</code> implements a slew of protocols and behaviours that should make it as easy and straightforwards to use as possible.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="39504" 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/elixir-needs-a-fifo-type/5701/39">Post #38</a>
	                </div>
	            </div>
              <div id="likers-container-39504" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="39504"
                     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>