Captures
import IEx.Helpers
Intuition
fun = &(&1 + 1)
# x + 1
#Function<42.105768164/1 in :erl_eval.expr/6>
fun.(42)
43
i(fun)
Term
#Function<42.105768164/1 in :erl_eval.expr/6>
Data type
Function
Type
local
Arity
1
Description
This is an anonymous function.
Implemented protocols
Enumerable, IEx.Info, Inspect
fun2 = &(&1 + &2)
&:erlang.+/2
fun2.(42, 43)
85
fun3 = &(&1 + &1)
#Function<42.105768164/1 in :erl_eval.expr/6>
fun3.(4)
8
Performance
&[&1|&2]
or
fn item, list ->
[item|list]
end
prefer using anonymous function in elixir
fn item, list ->
[item|list]
end
over
&[&1|&2]
unless!!!
prefer:
&IO.inspect/1
over:
fn item -> IO.inspect(item) end
prefer &IO.inspect/1
- initial ampersand marks as capture