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?

Most Liked

sorentwo

sorentwo

Oban Core Team

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

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:

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.

Where Next?

Popular in Discussions Top

Donovan
Hello everyone, I’m so glad to have discovered this awesome community. Thanks for creating it! This is my second post, and apologies for...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19675 166
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement