Elixir Function Capture
&
is the capture operator in Elixir.
&Module.function/arity
is shorthand for creating an anonymous function.
add_one = fn x -> x + 1 end
- Capture means
&
can turn a function into an anonymous functions which can be passed as arguments to other function or be bound to a variable.
So we can do things like this
speak = &(IO.puts/1)
speak.("hello") # hello
The capture operator can also be used to create anonymous functions, for example:
add_one = &(&1 + 1)
add_one.(1) # 2
is the same with:
add_one = fn x -> x + 1 end
add_one.(1) # 2