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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Just like <code>Enum.map/2</code> (actually <a href="https://github.com/erlang/otp/blob/master/lib/stdlib/src/lists.erl#L1313=" rel="noopener nofollow ugc"><code>:lists.map/2</code></a>), this implementation is body recursive, not tail recursive.</p>
<p>A tail-recursive version would need to call <code>Enum.reverse/2</code> at the end, just like the example provided by <a class="mention" href="/u/eksperimental" rel="nofollow">@eksperimental</a>.</p>
<p>Either one is probably fine in this case and the performance should be roughly equivalent when building lists, cf <a href="https://www.erlang.org/doc/efficiency_guide/myths.html#myth--tail-recursive-functions-are-much-faster-----than-recursive-functions" rel="nofollow">the Erlang efficiency guide</a> and the <a href="https://ferd.ca/erlang-s-tail-recursion-is-not-a-silver-bullet.html" rel="noopener nofollow ugc">article</a> it refers to. Body recursion might even be faster / use less memory depending on the function being optimized, list size, your architecture etc…</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="250101" data-batch-url="/posts/batch_likers">
                        2
                      </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/append-to-list-after-enum-map-how-to-do-this-efficiently/47626/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-250101" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="250101"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="250200" data-post-id="250200">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="odohMei7" data-post="9" data-topic="47626" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/o/aca169/48.png" class="avatar"> odohMei7:</div>
<blockquote>
<p>Thank you, but I think the list reverse has to go through the entire loop a second time.</p>
</blockquote>
</aside>
<p>I would like to mention that <code>Enum.reverse</code> in turn calls <code>:lists.reverse</code> if the first argument (the enumerable) is a <code>List</code>.</p>
<p>With that said, after reading the following in <a href="https://pragprog.com/titles/jaerlang2/programming-erlang-2nd-edition/" rel="noopener nofollow ugc">Programming Erlang (2nd edition)</a> by Joe Armstrong, I felt much better about using <code>Enum.reverse</code> with a <code>List</code> as the enumerable:</p>
<blockquote>
<p>…<br>
If the order matters, then call lists:reverse/1, which is highly optimized.<br>
…</p>
<p>Note: Whenever you want to reverse a list, you should call lists:reverse and nothing else. If you look in the source code for the module lists, you’ll find a definition of reverse. However, this definition is simply used for illustration. The compiler, when it finds a call to lists:reverse, calls a more efficient internal version of the function.</p>
</blockquote>
<p>So, with a long list, most of the time I’ll always add elements to a list head and then call <code>Enum.reverse</code> at the end and trust that Joe and Erlang have my back. <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>But as Joe points out a little later:</p>
<blockquote>
<p>The best thing to do is first write your programs as clearly as possible and then, if there are performance problems, measure before making any optimizations.</p>
</blockquote> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="250200" data-batch-url="/posts/batch_likers">
                        6
                      </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/append-to-list-after-enum-map-how-to-do-this-efficiently/47626/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-250200" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="250200"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="250212" data-post-id="250212">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="odohMei7" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  odohMei7
                    <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>Thank you for this precious additional information!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="250212" 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/append-to-list-after-enum-map-how-to-do-this-efficiently/47626/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-250212" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="250212"
                     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="312134" data-post-id="312134">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>To avoid the first iteration you can use Stream.map (a lazy iterator) instead of Enum.map and append using Enum.concat. Example:</p>
<blockquote>
<p>[0, 1, 2]<br>
|&gt; Stream.map(&amp;(&amp;1 + 1))<br>
|&gt; Enum.concat([4, 5])</p>
<p>[1, 2, 3, 4, 5]</p>
</blockquote>
<p>Good luck!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="312134" 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/append-to-list-after-enum-map-how-to-do-this-efficiently/47626/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-312134" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="312134"
                     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>