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

PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
sashaafm
I’m trying to evaluate the best combo/stack for a BEAM Web app. Right now I’m exploring Yaws a bit, after having dealt with Phoenix for a...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
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
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
jesse
Hi everyone, I hesitated to post this here because I don’t want you to think I’m spamming, but I’ve been working on a Platform-as-a-Serv...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement