Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "&1" mean in elixir function?

Tags:

elixir

Given this function, what does &1 refer to?

Enum.map [1, 2, 3, 4], &(&1 * 2)
like image 548
Mike Avatar asked Mar 08 '19 16:03

Mike


1 Answers

&1 refers to the first argument the callback function would receive. The ampersand by itself (&) is a shorthand for a captured function. This is how you could expand that function.

Enum.map([1, 2, 3, 4], fn x -> x * 2 end)

fn -> is equal to &(...

x -> x is equal to ...(&1

A quick reference can be found here

like image 136
Mike Avatar answered Sep 22 '22 23:09

Mike