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
--/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
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 //.
It also took a while to work out the semantics.
hauleth
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
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









