79757267

Date: 2025-09-06 01:06:48
Score: 0.5
Natty:
Report link

Remember that &(&1 * &2) is just "syntax sugar". The following are equivalent:

1- Anonymous Function (fn ... -> ... end)

Enum.reduce([1,2,3,4], fn a, b -> a * b end)

2- Capture Anonymous Function (&...)

Enum.reduce([1,2,3,4], &(&1 * &2))

3- Capture Named Function (def ... do ... end)

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:

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @coderVishal
  • User mentioned (0): @sobolevn's
  • Low reputation (0.5):
Posted by: Bukov