Stephan van Hulst wrote:That won't work Winston. max() is a terminal operation. After calling max(), filter() will throw an exception. Either your input needs to be a collection, or you need to collect the stream before you process it again.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:Streams can't be Cloneable, because their data source might not be used more than once, e.g. a Stream based on an Iterator.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:You can still keep your method argument a Stream, but that just means you'll have to collect it before you perform the max() operation:
...
Seeing as that is a bit of a roundabout way of doing things, you might as well just accept a Collection.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:I don't quite understand that. In fact, it suggests to me that it's eminently Cloneable - ie, I can go halfway through a Stream and then clone it based on its current position.
The only reason why I might not be able to do that would have to do with implementation - which I thought was something we OO bods don't have to worry about.
Furthermore, cloning a Stream before it's been used makes perfect sense, because then I don't need to worry about its source.
Winston Gutkowski wrote:You know what I'd like to see? A complete description of a Stream in Java terms, not just an API.
Stephan van Hulst wrote:What's the position of the following Iterator?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:And for a true RNG. it's "clone" is either itself, or - from your example - a new InfiniteRandoms object with a new RNG, created with whatever seed (or none) that was supplied to the original object.
Let me put the question back to you: I understand that one of the bases of Streams is "lazy" instantiation; but that aside, why shouldn't ANY Stream be able to supply a clone() method, with the caveat that, once used, it may take longer to clone?
Streams are dynamic Lists with Iterator-like APIs, that the "functionally inclined" designers of Java have given new powers; not some esoteric new form of life.
Stephan van Hulst wrote:
Winston Gutkowski wrote:You know what I'd like to see? A complete description of a Stream in Java terms, not just an API.
I'm not sure what you mean, but have you already read the package summary? It's chock full of information: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
There are three kinds of actuaries: those who can count, and those who can't.
Stephan van Hulst wrote:Right, but how would the Stream know how to do that, if you called clone() on it?
What are you going to clone? A Stream can clone the operations that will occur before it, but it can't clone the source of data, because the source of data isn't cloneable in general.
Asking me to call clone() on the result of map(i -> i+2) is like me asking you to somehow 'clone' the following piece of code from the first snippet:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Piet Souris wrote:my advice: just practise these streams as much as you can, then see if you think your objections are still relevant.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:I think I'm completely misunderstanding you. What does it mean to you to clone a stream?
How could you possibly clone words if you didn't know how to clone readAndBurnIterator?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
There are three kinds of actuaries: those who can count, and those who can't.
Paul Clapham wrote:Winston, it seems to me that everything you have said so far about the utility of cloning a Stream applies equally well to cloning an InputStream. To me those two use cases look pretty similar. Am I right?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Piet Souris wrote:Well, you can clone a Stream (ehh, sort of...), but it is actually too ugly to look at:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
There are three kinds of actuaries: those who can count, and those who can't.
Stephan van Hulst wrote:Winston and Piet, both your examples ignore infinite streams.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:IYou talked about the state of a stream for a while now, but what is the 'initial state' of a stream?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Streams can tell when they're exhausted; why shouldn't they be able to tell when they're "unused"?
Mike Simmons wrote:I think the idea of initial state of a stream is very dependent on what a given stream represents, and how it is generated. Only the underlying implementation would know how to clone that state
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:However, I concede that perhaps it doesn't have wide enough use to be practical...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Afraid that would not work. As Stephan has said (and this is a very interesting discussion) a Stream does not “have” any contents. It passes the contents on and then finishes with them. Once you have traversed the Stream, it has no means to reset itself at the beginning, so I think you will suffer some sort of Exception in line 5.Winston Gutkowski wrote:. . . the original requirement can be done without "cloning", . .
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
which tells me that you get the Strings n ascending order of maximum length to date, and also that I have the longest name in my family.The List [, Campbell] has length 2
Campbell Ritchie wrote:Moving the length variable to a field
which tells me that you get the Strings n ascending order of maximum length to date
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:No exception will be thrown, since no terminal operations are called yet, but the result will yield not the longest strings, but empty strings instead.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:Yes, the peek() call is never used because there is nothing to pull data through the Stream...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Consider Paul's rocket mass heater. |