Identification of flow reduction versus idempotent value

The package documentation java.util.streamgives this definition for identification in the context of the abbreviation:

The value identitymust be an identity for the combiner function. This means that for everyone u, it’s combiner.apply(identity, u)equal u.

Stream.reduce(), and its primitive copies offer a similar definition.

As I understand it, this definition is necessary to support parallel threads. For example, a non-zero initial value for decreasing the sum can be multiplied by the number of parallel processors, which unpredictably distorts the final result.

But this definition seems stricter than necessary to support parallelism. Why not require an idempotent value x, which combiner.apply(x, x)is equal x? This will protect functions, such as Integer::sum, (a, b) -> a * band String::concatfrom skewing over several processes, while allowing you to use any seed with idempotent functions, such as Math::max, (a, b) -> a | band Sets::intersection.

Is there any unique advantage to the identity value that I am missing?

+4
source share
2 answers

" ?". , . , .

2- . 2 (a, b). i, - op.

(i op a) op b. (i op a) op (i op b) ( , ). , op b

, , , . , i, , op = i, , (i op a) op (i op b) b op a b.

-, , - : (x, y) -> (x + y).replaceAll(" +", " "). " " , . .

+4

combiner.apply(x, x) == x ?

, max(). , max(x, x) == x . x, , identity max().

, , , max() MIN_VALUE ( " ", null NaN, ).

0

Source: https://habr.com/ru/post/1687340/


All Articles