In Python, if I want to do a fold over the operation xor, I can write:
reduce(operator.xor, my_things, 0)
rather than the more cumbersome
reduce(lambda x, y: x^y, my_things, 0)
Is there anything like this in the new Java 8 functional features? e.g. to write something like this
myThings.reduce(0, Integer::xor)
rather than
myThings.reduce(0, (x, y) -> x ^ y)
There's Integer#sum(int, int)
which is used as you suggest in the package private IntPipeline
, but no similar methods for other numerical operators.
@Override
public final int sum() {
return reduce(0, Integer::sum);
}
You can define them yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With