In Java 8, Stream has a method reduce:
T reduce(T identity, BinaryOperator<T> accumulator);
Is the accumulator operator allowed to modify either of its arguments? I presume not since the JavaDoc says the accumulator should be NonInterfering, though all examples talk of modifying the collection, rather than modifying the elements of the collection.
So, for a concrete example, if we have
integers.reduce(0, Integer::sum);
and suppose for a moment that Integer
was mutable, would sum
be allowed to modify its first parameter by adding to it (in place) the value of its second parameter?
I presume not, but I would also like an example of where this Interfering causes a problem.
identity
is provided to thereduce
method, it seems that a mutating accumulator produces the good result, even when run in parallel. I understand that it is not good practice but it seems to "work". – Napkin