purplesquirrel

purplesquirrel

How does &(&1 + &2) work?

I was trying to write this Enum.reduce([1,2,3], &Kernel.+(&1, &2)) more succinctly and I discovered I could do Enum.reduce([1,2,3]), &(&1 + &2)).

What is this doing in the background? What kind of expressions can you put in &()? What is the term for this so I can google more or find the docs?

Most Liked

cmkarlsson

cmkarlsson

I have heard this mentioned as shorthand notation for anonymous function. They are mentioned in the elixir getting started guide under Function capturing: https://elixir-lang.org/getting-started/modules-and-functions.html

The & is a capture operator (https://hexdocs.pm/elixir/Kernel.SpecialForms.html#&/1)

In your example &(&1 + &2) translates to fun (x, y) -> x + y end The &() “creates” the function and the &1 and &2 means the position of the argument to the function.

If you google elixir shorthand anonymous function you’ll get better explanations and examples :smiley:

rodrigues

rodrigues

The expression could have been even shorter

Enum.reduce([1, 2, 3], &+/2)

For this specific case, the more succint option is Enum.sum/1.

The capture is very powerful, you can make things like &"Hello #{&1}", also with charlists, tuples, maps, structs… :slightly_smiling_face:

purplesquirrel

purplesquirrel

Ah, Doh. I was awake enough to know + maps to Kernel.+ but didn’t even consider that Kernel.& would obviously map to &

This is great, I can trim down all those one liner fn (val) -> val == search_target end functions! That always seemed awkward that Elixir didn’t have a nice clean short hand!

Where Next?

Popular in Questions Top

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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
gazoon
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
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 Postg...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement