Remember that &(&1 * &2)
is just "syntax sugar". The following are equivalent:
Enum.reduce([1,2,3,4], fn a, b -> a * b end)
Enum.reduce([1,2,3,4], &(&1 * &2))
Enum.reduce([1,2,3,4], &multiply/2)
# ...
def multiply(a, b) do
a * b
end
The Capture Anonymous Function syntax (#2) is nice and concise but as @coderVishal pointed out, mostly useful for short, easy-to-understand expressions
If you have a more complicated expression -- or (as you discovered) if you need more than one captured function in a single line -- then it is time to switch to longer fn
-style anonymous functions (#1) or make regular functions and use them by name (#3)
In @sobolevn's example, he used #2 for the outer Enum.reduce
and then #3 for the inner multiply
Enum.map([[1,2],[3,4]], &Enum.reduce(&1, fn(x, acc) -> x * acc end))
Helpful links: