1
Optional.empty
This means that for all t, accumulator.apply(identity, t) is equal to t.
Paul Clapham wrote:I don't see why returning the identity value of the operator is incorrect. Maybe you could explain this?
Anil Philip wrote:
Paul Clapham wrote:I don't see why returning the identity value of the operator is incorrect. Maybe you could explain this?
nothing times nothing is not equal to 1
unless I am missing something.
In mathematics, an identity element or neutral element of a binary operation is an element that leaves unchanged every element when the operation is applied.
Paul Clapham wrote: (This means that the sum of an empty set of integers is 0 and the product of an empty set of integers is 1.)
Mike Simmons wrote: If there are no values in the stream, then no multiplication occurs at all. There's nothing to multiply.
Anil Philip wrote:Then there is no result.
Campbell Ritchie wrote:The whole idea of Streams was to provide code that would run normally to completion. Why would you want to throw an exception?
Mike Simmons wrote:Just so you know, Paul C got his PhD in mathematics from Cal Tech.
Mike Simmons wrote:
It might help to spend less time reflexively disagreeing, and more time absorbing.
Mike Simmons wrote:
Also, your own source, wikipedia, is telling you in the article that you quoted, that the identity for the addition operation is 0, and that for multiplication is 1. Maybe Paul is onto something here...
Mike Simmons wrote:
Well, you're using a method that documents pretty clearly how it works. Have you looked at the API for Stream.reduce() with two arguments? It says the result should be equivalent to:
In this code, if you have a stream with no elements, what is the result?
Anil Philip wrote:Perhaps he can give us a good explanation?
Anil Philip wrote:That is only telling me that Java is using the term 'identity' in a different way that Math uses it.
Anil Philip wrote:That is awesome... to have such great pedigree. Perhaps he can give us a good explanation?
Anil Philip wrote:But I don't see where it says that when you have no elements to perform the operation on, Identity is still the result.
Anil Philip wrote:That is only telling me that Java is using the term 'identity' in a different way that Math uses it.
Paul Clapham wrote:
I already provided a good explanation.
Anil Philip wrote:But I don't see where it says that when you have no elements to perform the operation on, Identity is still the result.
Well, it doesn't specifically say that. It doesn't have to. When you take the product of a set of integers, you get an integer as the answer. And since the empty set is a set, the product of the empty set of integers has an integer value too. ...
No, you have that backwards. You came here complaining that Java should throw an exception when asked to provide the product of an empty set of integers. But Java doesn't do that. It does what Math says it should do.
Anil Philip wrote:no point in continuing this discussion since you cannot provide me with any accepted source that backs up what you are saying.
(because I never heard this before in any Math class),
but instead say that I am "complaining" and "reflexively disagreeing".
What makes you think 0 is a wrong result for a sum or 1 for a product?Anil Philip wrote:. . . At least it's better than providing an incorrect result. . . .
Like this, you mean?What you are doing is reducing the Stream to a single value, which you can see clearly in IntStream#reduce(). When you reduce a Stream, you are losing information, including how many elements there were. That is normal behaviour for reducing; it does not care about any other information. The result would be the same for my IntStreams if you add more of the same elements. The limit to which the result tends as the Stream lengthens (with more of the same) is the same as the identity shown as the first argument in lines 2 and 4. If you shorten the Stream towards length 0, the limit towards which the result tends is also the identity (sometimes called the unit).There could have been zero to many elements in the stream, resulting in identity. . . .
If you multiply x by itself n times, you will get xⁿ, so when you multiply it zero times, you should get x⁰, and for all finite values of x, that comes to 1, except in some cases if x = 0.Stephan van Hulst wrote:. . . multiplying zero integers would return anything other than 1. . . .
Stephan van Hulst wrote:here's a link for you: https://en.m.wikipedia.org/wiki/Empty_product
Are you sure about that?Paul Clapham wrote:. . . there are no crocodiles in my bathroom.
Campbell Ritchie wrote:
Are you sure about that?Paul Clapham wrote:. . . there are no crocodiles in my bathroom.
Campbell Ritchie wrote:
Are you sure about that?Paul Clapham wrote:. . . there are no crocodiles in my bathroom.
Paul Clapham wrote:For example, "all of the crocodiles in my bathroom are blue" is a true statement because there are no crocodiles in my bathroom.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
"all of the crocodiles in my bathroom are blue"
"none of the crocodiles in my bathroom are blue"
Paul Clapham wrote:And since the empty set is a set, the product of the empty set of integers has an integer value too.
Stephan van Hulst wrote:Man, I would be so upset if multiplying zero integers would return anything other than 1. Throwing an exception is what I would call "an insidious bug".
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
That is the correct mathematical abstraction, but programmers have always bent the rules. For example, the double datatype supports the non‑existent number -0.0. Generics might allow a set containing no monkeys to be equal to a set containing no integers, but you can't interconvert the set by adding an integer to it. This reduce() method requires the user to supply an identity, 1 for multiplication or 0 for addition, but is there an identity appropriate for subtraction?Paul Anilprem wrote:. . . there is exactly one empty set and that two empty sets are really the same set. . . . an empty set is as much a set of Integers as it is a set of monkeys. . . .
Campbell Ritchie wrote:
The reason for not throwing an exception is that the designers didn't want Streams to fail to complete their runs.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Isn't that what happens if you use the method without the identity parameter?Paul Anilprem wrote:. . . returning an empty Optional is more appropriate.
Agree there. . . there already are a buttload of methods, what's one more?
That never stopped them making StringBuilder implement Comparable even though it doesn't override equals().Unless it is doesn't make sense logically.
Campbell Ritchie wrote:Isn't that what happens if you use the method without the identity parameter?
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:
Campbell Ritchie wrote:Isn't that what happens if you use the method without the identity parameter?
No, the one that doesn't take identity parameter takes one parameter of type BinaryOperator. A BinaryOperator restricts you to returning the same type as the type of the stream.
In fact, now I wonder why did they not design this method to accept a BiFunction like the other reduce method.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Campbell Ritchie wrote:
Another reason, maybe, for not throwing exceptions from an empty Stream is that exceptions are supposed to respond to something abnormal, and an empty Stream is regarded as a normal occurrence.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:but not having:
Optional<V> reduce(BiFunction<V,? super T, V> accumulator, BinaryOperator combiner)
Campbell Ritchie wrote:This reduce() method requires the user to supply an identity, 1 for multiplication or 0 for addition, but is there an identity appropriate for subtraction?
I thought not; I would have been sure if I had bothered to read the documentation.Mike Simmons wrote:. . . No. . . . .
Paul Clapham wrote:Not all operators have an identity -- you'll find that subtraction doesn't have one, for example. That's why the documentation mentions that operators must be associative if you're providing their identity. Also, providing an incorrect identity will produce weird results.
Don't get me started about those stupid light bulbs. |