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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="NobbZ" data-post="10" data-topic="18593">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/48/27235_2.png" class="avatar"> NobbZ:</div>
<blockquote>
<p>The second versions is syntactic sugar for the second.</p>
</blockquote>
</aside>
<p><a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/test/erlang/string_test.erl" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/elixir-lang/elixir/blob/master/lib/elixir/test/erlang/string_test.erl</a></p>
<p><a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_interpolation.erl" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_interpolation.erl</a></p>
<p><a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_tokenizer.erl" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_tokenizer.erl</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="107248" 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/formatting-a-list-of-strings-am-i-missing-anything/18593/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-107248" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="107248"
                     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="107250" data-post-id="107250">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m not quite sure what you want to say by linking those 3 files…</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="107250" 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/formatting-a-list-of-strings-am-i-missing-anything/18593/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-107250" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="107250"
                     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="107251" data-post-id="107251">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I was just trying to make it easier to investigate the handling of interpolated strings for anyone reading the topic. I guess I should have suppressed the “quote/reply handling” of Discourse.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="107251" 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/formatting-a-list-of-strings-am-i-missing-anything/18593/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-107251" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="107251"
                     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="107262" data-post-id="107262">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Rich_Morin" data-post="9" data-topic="18593">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rich_morin/48/2020_2.png" class="avatar"> Rich_Morin:</div>
<blockquote>
<p>doesn’t seem all that different to this newbie…</p>
</blockquote>
</aside>
<blockquote>
<p>The difference is that interpolation invokes a protocol to convert to string, the concat operator does not.</p>
</blockquote>
<p><a href="https://www.reddit.com/r/elixir/comments/62v1ka/why_is_string_interpolation_with_so_much_slower/#t1_dfpmeg3" rel="noopener nofollow ugc">From</a>:</p>
<aside class="quote no-group">
<blockquote>
<p>You need to tell us how you are benchmarking it. The difference is that interpolation invokes a protocol to convert to string, the concat operator does not. Some tips:</p>
<ol>
<li>Do not benchmark on IEx. Code in IEx is evaluated instead of compiled and executed</li>
<li>Always benchmark inside a project so protocols are also consolidated</li>
</ol>
<p>If you do so, both will become much faster and the difference should reduce considerably.</p>
</blockquote>
</aside>
<p><a href="https://hexdocs.pm/elixir/String.Chars.html" rel="noopener nofollow ugc"><code>String.Chars</code></a></p>
<aside class="quote no-group">
<blockquote>
<p>String interpolation also invokes <a href="https://hexdocs.pm/elixir/String.Chars.html#to_string/1" rel="noopener nofollow ugc"> <code>to_string/1</code> </a> in its arguments. For example,  <code>"foo#{bar}"</code>  is the same as  <code>"foo" &lt;&gt; to_string(bar)</code> .</p>
</blockquote>
</aside>
<p>Now in this context we are explicitly using <code>to_string/1</code> in combination with concatenation anyway.</p>
<p>FYI: to me (from the compiler perspective) “syntactic sugar” implies that there is <em>no</em> runtime (performance) difference between the “sugared” and “unsugared” expression. I’m starting to wonder whether some people are using the term in a much looser sense.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="107262" 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/formatting-a-list-of-strings-am-i-missing-anything/18593/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-107262" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="107262"
                     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="107271" data-post-id="107271">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>If you’re outputting this with e.g. <code>IO.puts</code> or <code>send_resp</code> in Phoenix, you don’t have to output interpolated strings as results, you can output iolists. That’s the main trick that I would use. The second trick is to try to unify the handling of ‘x and y’ and ‘x, y, and z’ by realizing that the latter case reduces to the former case if you see that ‘x, y,’ in the latter corresponds to ‘x’ in the former. My version:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Test do
  def list_str([]), do: ""
  def list_str([x]), do: Integer.to_string(x)
  def list_str([x1, x2]) when is_integer(x1) do
    list_str([[Integer.to_string(x1), " "], x2])
  end
  def list_str([x1, x2]), do: [x1, "and ", Integer.to_string(x2)]
  def list_str(xs) do
    {init, [last]} = Enum.split(xs, -1) # Traverses twice
    list_str([Enum.map(init, &amp;append_comma/1), last])
  end

  defp append_comma(x), do: [Integer.to_string(x), ", "]
end

[] |&gt; Test.list_str |&gt; IO.puts
[1] |&gt; Test.list_str |&gt; IO.puts
[1, 2] |&gt; Test.list_str |&gt; IO.puts
[1, 2, 3] |&gt; Test.list_str |&gt; IO.puts
</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="107271" 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/formatting-a-list-of-strings-am-i-missing-anything/18593/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-107271" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="107271"
                     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="107274" data-post-id="107274">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<pre data-code-wrap="elixir"><code class="lang-elixir">def join([a, b]), do: "#{a} and #{b}"
def join([a, b, c]), do: "#{a}, #{b}, and #{c}"
def join([a, b | rest]), do: join(["#{a}, #{b}" | rest])
def join(list), do: to_string(list)
</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="107274" 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/formatting-a-list-of-strings-am-i-missing-anything/18593/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-107274" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="107274"
                     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="107282" data-post-id="107282">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Using</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def join([a, b]), do: "#{a} and #{b}"
def join([a, b, c]), do: join([[a, ', ', b, ','], c])
def join([a, b | rest]), do: join([[a, ', ', b] | rest])
def join(list), do: to_string(list)
</code></pre>
<p>with</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[] |&gt; Demo.join() |&gt; IO.puts()
[1] |&gt; Demo.join() |&gt; IO.puts()
[1, 2] |&gt; Demo.join() |&gt; IO.puts()
[1, 2, 3] |&gt; Demo.join() |&gt; IO.puts()
</code></pre>
<p>produces</p>
<pre><code class="lang-plaintext">

1 and 2
, , and 3
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def join([]), do: ""
  def join([a]), do: to_string(a)
  def join([a, b]), do: "#{a} and #{b}"
  def join(list), do: join(list, [])
  def join([last], strl), do: to_string([strl, 'and ', to_string(last)])
  def join([h | t], strl), do: join(t, [strl, to_string(h), ', '])
</code></pre>
<p>produces the desired result.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
1
1 and 2
1, 2, and 3
</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="107282" 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/formatting-a-list-of-strings-am-i-missing-anything/18593/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-107282" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="107282"
                     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="107283" data-post-id="107283">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Didn’t realize the inputs weren’t strings. Edited to support integers.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="107283" 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/formatting-a-list-of-strings-am-i-missing-anything/18593/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-107283" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="107283"
                     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>