An Unprecedented Subtraction

Let’s go back to the origins of this blog with a bit of unexpected code behavior. This time, let’s try removing elements from a list…

Image for post
Image for post
Maxwell Smart — because I couldn’t find any proper image for this article

Let’s Subtract!

The beauty of this trick is that the code examples work in both Erlang and Elixir, with barely any changes at all… I’ll do it in Elixir, feel free to try them out in Erlang or efene if you like.

iex(1)> [1, 2, 3]
[1, 2, 3]
iex(2)> [1, 2, 3] -- [1, 2]
iex(3)> [1, 2, 3] -- [1, 2] -- [3]
Image for post
Image for post
Santa Clarita Diet — Confusing, I know

What’s going on here?

As usual, it’s better if you first try to decipher the mystery on your own. So, go ahead, check the docs…

iex(6)> ([1, 2, 3] -- [1, 2]) -- [3]
iex(7)> [1, 2, 3] -- ([1, 2] -- [3])

In Elixir

What we’re looking for is operator associativity. And elixir actually has a whole page in the docs dedicated to it. There you can see that all binary (as in “with two arguments”) list operations (++ -- .. and <>) associate from Right to Left.

iex(7)> 1 .. 10
iex(8)> 1 .. 10 .. 20
** (ArgumentError) ranges (first..last) expect both sides to be integers, got: 1..10..20
(elixir) lib/range.ex:55:
Image for post
Image for post

In Erlang

Now let’s try to find that same thing in Erlang… 🕵️‍♀️

Image for post
Image for post
Table 8.6: Operator Precedence
Image for post
Image for post
1> self() ! self() ! self() ! something.
2> flush().
Shell got something
Shell got something
Shell got something
3> Pid1 ! Pid2 ! Pid3 ! something. # "broadcast" a message :P

Is that it?

Yeah, I know… this article is not very insightful, deep or revealing as others, but this topic was in sitting in my To-Write list for almost 2 years. Now it’s out of the way.

Image for post
Image for post

In Other News…


As I mentioned in my previous article, SpawnFest is coming!
This year it will happen on September 21st & 22nd.
Register your team and join us for FREE to win several amazing prizes provided by our great sponsors!


ElixirConf is coming to Latin America for the first time!
Thanks to our friends from PlayUS Media, we’ll meet on Medellín, Colombia for ElixirConfLA on October 24th & 25th.
The schedule was already announced, Early Bird Tickets are on sale and the CFT is open until July 19th.

Erlang Battleground

Finally, the usual reminder: We’re looking for writers. If you want to write on Erlang Battleground, just le me (Brujo Benavides) know :)

Written by

Father / Long Distance Walker / Erlanger @ AdRoll via BairesDev / Trainer @ CodeMentor - You can…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store