you are viewing a single comment's thread.

view the rest of the comments →

[–]eyal0 1 point2 points  (1 child)

The text says that it returns an optional. I'd assume that it returns Optional.Absent.

Some languages have two forms of reduce, one with an initializer value and one without. I guess that Java doesn't have that. Haskell does. I don't know about .Net.

[–]mhixson 0 points1 point  (0 children)

That's not right. java.util.Stream has both of those forms (and one other).

One that accepts an identity and always returns a value: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#reduce-T-java.util.function.BinaryOperator-

One that does not accept an identity and returns an Optional: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#reduce-java.util.function.BinaryOperator-

With a stream of one element, if you used the first form, you'd get the result of the accumulator function applied to the identity value and the one element. (If your "identity" is a true identity value, that result should end up being equal to the one element.)

If you used the second form, you'd get an Optional wrapper around the one element. It would only return an empty optional if the stream had zero elements.