A.Vector.map_reduce
You're seeing just the function
map_reduce
, go back to A.Vector module for more information.
Specs
map_reduce(t(val), acc, (val, acc -> {mapped_val, acc})) :: {t(mapped_val), acc} when val: value(), mapped_val: value(), acc: any()
Invokes the given fun
to each element in the vector
to reduce
it to a single element, while keeping an accumulator.
Returns a tuple where the first element is the mapped vector and the second one is the final accumulator.
The function, fun
, receives two arguments: the first one is the
element, and the second one is the accumulator. fun
must return
a tuple with two elements in the form of {result, accumulator}
.
Examples
iex> vector = A.Vector.new([1, 2, 3])
iex> {new_vec, 6} = A.Vector.map_reduce(vector, 0, fn x, acc -> {x * 2, x + acc} end)
iex> new_vec
vec([2, 4, 6])
For example, if with_index/2
was not implemented, you could implement it as follows:
iex> vector = A.Vector.new([1, 2, 3])
iex> A.Vector.map_reduce(vector, 0, fn x, i -> {{x, i}, i + 1} end) |> elem(0)
vec([{1, 0}, {2, 1}, {3, 2}])