klo

klo

Concat/appending lists

Got a question about when to concat vs. prepending items to list then reversing to achieve appending.

So i know lists boil down to [1 | [2 | []]]. I also know that i can append to a list using something such as

iex(1)> list = [1, 2, 3]
[1, 2, 3]
iex(2)> list ++ [4]
[1, 2, 3, 4]

and I can prepend to a list such as

iex(3)> [4 | list]
[4, 1, 2, 3]

But to achieve what I did before on the line above, i would have to reverse the list, prepend, then reverse again. What if i tried to append a list of items? why would the fastest way be to use something such as Enum.concat or using the ++ to stitch together the two lists and not doing a recursive call that will do just prepend the bits and flip the entire list?

First 10 of 11 Posts Switch mode

kip

kip

ex_cldr Core Team

Enum.concat/2 for two lists is implemented as:

  @spec concat(t, t) :: t
  def concat(left, right) when is_list(left) and is_list(right) do
    left ++ right
  end

:slight_smile:

ityonemo

ityonemo

In 99% of the code I write performance is not important so I just use whichever is more expressive for the data I have. For example, compile-time list concatenation? Hell yeah use ++.

However, I believe many general libraries (and the enum module itself in some list building methods) take a prepend then reverse strategy. You’ll see that if you search for Enum.reverse in the elixir code.

gregvaughn

gregvaughn

Down in erlang, reversing a list executes a heavily optimized native BIF (built-in-function) because it’s a frequently used feature, so it’s probably not as expensive as you think. That being said, unless your lists are long or you’re in a performance critical part of code, don’t worry too much. Do what’s most expressive.

But if you do need to optimize, be sure to benchmark. For some length of list and algorithm append may be faster, but if the length of the list changes, then prepend-then-reverse may be faster.

I find many times the ordering does not have bearing on the correctness of the code. In those cases I prepend out of habit.

klo

klo

Thank you all for the comments. I think i was just overthinking it..

dimitarvp

dimitarvp

It’s very good to be curious – shows intellect. :smiley:

IMO make a very small Elixir project where you benchmark all the approaches you can think of – and with differently sized lists. benchee is an excellent library for this.

Definitely do satisfy your curiosity but also do measure because often you’d end up quite surprised.

And, in real projects, absolutely go for what’s more readable as others said.

sorentwo

sorentwo

Oban Core Team

Or you can look at the fast-elixir benchmark which breaks down the various techniques by list size.

srowley

srowley

That’s a great resource. Based on the results with respect to this question, it makes me wonder why Enum.concat/1 is implemented the way that it is.

NobbZ

NobbZ

Because it removes one level of nesting, for any kind of enumerable.

hauleth

hauleth

In general, you should always append to the front of the list and reverse list only when needed, as often one will need to append to list much more often than reading it in order. This is one of the improvements that I have introduced in Sentry some time ago, as breadcrumbs were constructed much more often than these were used (only in case of error).

dimitarvp

dimitarvp

Say what now? You worked at Sentry?

Where Next?

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2976 91332 914
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
AstonJ
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
bjorng
We want to introduce a new native datatype to Erlang: native records. Although replacing all tuple records with native records is not our...
New

We're in Beta

About us Mission Statement