Elixir: Capture Operator: &

Using ‘&’ as a Capture Operator

capture_oper.ex

IEx CLI Examples

In the code example below, the function was specified using & as a capture operator.

Capture Operator

iex(1)> Hof.tripler(6, &(20 * &1))
360

The capture operator example above becomes an economical alternative to the higher-order function example below:

Higher-Order Function

iex(1)> Hof.tripler(6, (fn(value) -> 20 * value end))
360