Reduce is a function which will take an array, function and accumulator and use accumulator as seed to start the iteration with the first element to give next accumulator and the iteration continues for all the elements in the array (refer below example)
defmodule MyList do
def reduce([], _func, acc) do
acc
end
def reduce([head | tail], func, acc) do
reduce(tail, func, func.(acc, head))
end
end
Copy paste the above snippet in iex:
MyList.reduce [1,2,3,4], fn acc, element -> acc + element end, 0
MyList.reduce [1,2,3,4], fn acc, element -> acc * element end, 1
Explanation for example 1:
Iteration 1 => acc = 0, element = 1 ==> 0 + 1 ===> 1 = next accumulator
Iteration 2 => acc = 1, element = 2 ==> 1 + 2 ===> 3 = next accumulator
Iteration 3 => acc = 3, element = 3 ==> 3 + 3 ===> 6 = next accumulator
Iteration 4 => acc = 6, element = 4 ==> 6 + 4 ===> 10 = next accumulator = result(as all elements are done)
Filter the list using reduce
MyList.reduce [1,2,3,4], fn acc, element -> if rem(element,2) == 0 do acc else acc ++ [element] end end, []