Enum.zip_reduce
You're seeing just the function
zip_reduce
, go back to Enum module for more information.
Specs
Reduces a over all of the given enums, halting as soon as any enumerable is empty.
The reducer will receive 2 args, a list of elements (one from each enum) and the accumulator.
In practice, the behaviour provided by this function can be achieved with:
Enum.reduce(Stream.zip(enums), acc, reducer)
But zip_reduce/4
exists for convenience purposes.
Examples
iex> enums = [[1, 1], [2, 2], [3, 3]]
...> Enum.zip_reduce(enums, [], fn elements, acc ->
...> [List.to_tuple(elements) | acc]
...> end)
[{1, 2, 3}, {1, 2, 3}]
iex> enums = [[1, 2], %{a: 3, b: 4}, [5, 6]]
...> Enum.zip_reduce(enums, [], fn elements, acc ->
...> [List.to_tuple(elements) | acc]
...> end)
[{2, {:b, 4}, 6}, {1, {:a, 3}, 5}]
Specs
zip_reduce( t(), t(), acc, (enum1_elem :: term(), enum2_elem :: term(), acc -> acc) ) :: acc when acc: term()
Reduces over two enumerables halting as soon as either enumerable is empty.
In practice, the behaviour provided by this function can be achieved with:
Enum.reduce(Stream.zip(left, right), acc, reducer)
But zip_reduce/4
exists for convenience purposes.
Examples
iex> Enum.zip_reduce([1, 2], [3, 4], 0, fn x, y, acc -> x + y + acc end)
10
iex> Enum.zip_reduce([1, 2], [3, 4], [], fn x, y, acc -> [x + y | acc] end)
[6, 4]