Papillon6814

Papillon6814

Is this behavior of ++ and -- intended?

Hello.
I found a strange behavior of ++ and --.

iex(1)> x = [:a, :b, nil, :c]
[:a, :b, nil, :c]
iex(2)> x -- [:a] -- [:c]
[:b, nil, :c]
iex(3)> x -- [:a] ++ [:c]
[:b, nil]

But a result I expected is this.

iex(4)> Kernel.--(x, [:a]) |> Kernel.++([:c])
[:b, nil, :c, :c]
iex(5)> Kernel.--(x, [:a]) |> Kernel.--([:c])
[:b, nil]

I think the calculation is processed one by one from the beginning of the expression. So the result should be same as latter one, I think.

How does the former one come?

Marked As Solved

NobbZ

NobbZ

--/2 and ++/2 are “right to left” associative, x -- [:a] -- [:c] is therefore equivalent to x -- ([:a] -- [:c]).

As [:a] without [:c] is still [:a], only :a gets removed from x then.

Also Liked

rvirding

rvirding

Creator of Erlang

If you think about it is more efficient for ++. So X ++ Y ++ Z will evaluate faster, less copying, if it is interpreted as X ++ ( Y ++ Z ) than if it interpreted as ( X ++ Y ) ++ Z. Hence it is right associative. -- was just given the same to try and be a little consistent.

Actually -- came later. It was a bit of a joke actually: if we have ++ couldn’t we have -- and ** and // as well? I couldn’t come up with something sensible for lists for ** and //. :smile: It also took a while to work out the semantics.

hauleth

hauleth

** could generate cross product of A and B. Because why not.

NobbZ

NobbZ

Not that I am aware of. Though I think it was just taken from erlang, such that it can be inlined without complex rewrite.

Last Post!

hauleth

hauleth

** could generate cross product of A and B. Because why not.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New

We're in Beta

About us Mission Statement