Irreductible Weirdness

End of the week almost, I think I am seeing things:

> Enum.reduce [1,2,3], &//2
1.5

I am pretty sure it’s “foldl” however.

I get the same when I run the reverse (which is how I expect it to work)

> Enum.reduce [3,2,1], &//2
1.5

I am certainly doing something very wrong. Please point it out : )

2 Likes

It works correctly. In the first case you basically do

iex(1)> 3 / (2 / 1)
1.5

In the second one, you do

iex(2)> 1 / (2 / 3)
1.5
2 Likes

1.5 = 3/(2/1)

i.e. the accumulator is the second parameter, i.e. the denominator

iex> Enum.reduce [1,2,3], &//2
1.5
iex> Enum.reduce [1,2,3], &(&1/&2)
1.5
iex> Enum.reduce [1,2,3], &(&2/&1) 
0.16666666666666666
2 Likes