-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to Handle End of Stream #21
Comments
Hiya! Don't think there's a way to do this currently :-( If you can do all the processing inside foo then obviously you could just I'll have a think, I can't think of a similar method in other Tim
|
Thanks for the followup - yes, I'm not sure what the right solution is. TotallyLazy seems to have some functionality along these lines, but I can't see any examples of how it works - see: https://github.com/bodar/totallylazy/blob/master/src/com/googlecode/totallylazy/Closeables.java JDK8 streams seem to sidestep the issue by putting a "close()" method onto the Stream interface itself: https://docs.oracle.com/javase/8/docs/api/java/util/stream/BaseStream.html But that just means the consumer of the stream has to close it. If nobody else is doing it then It might be that what I want is an anti-pattern. But it seems intuitive to me that being able to functionally compose streams is a desirable thing. |
I didn't realise this was a thing with Java 8 streams :-) I'll take a look into adding onClose and close into Groovy streams... As Will need to make sure we don't kill auto-closeable AND grooy's Tim On 25 June 2015 at 13:38, Simon Sadedin [email protected] wrote:
|
So, I had some free time tonight, and took a stab at adding a It ended up being a bit of a beast Got all existing tests passing, and added a solitary test for the new close method But as the commit message says, this commit feels like it needs a lot of looking at until it can be trusted... I guess you were more looking for an which I guess is another commit, on another free night ;-) |
This looks like a great start, and certainly helps to manage the issue. I One minor note - this made it depend on Java 1.7 for me. Not a big deal, I A generalised "onFinished" type feature would definitely still be a great Thanks! On Mon, Jun 29, 2015 at 6:49 AM, Tim Yates [email protected] wrote:
|
Cool, thanks for trying it all out :-) Yeah, adding AutoCloseable (so it can be used in a trywithresources query), I'll have a look at onFinished :-D On 30 June 2015 at 14:07, Simon Sadedin [email protected] wrote:
|
I have a few common use cases that I'm finding hard to deal with. The most obvious one is I want to process lines of a file. It's all fine except that I don't know how to ensure the file is closed once the stream is finished. If I pass Stream objects around I have no way to ensure that. Intuitively, what I want is a syntax like this:
Then a client of this function can call:
And the key point is, the "end { }" closure is guaranteed to be called when the stream terminates, regardless of whether it's because the file was exhausted or whether it's because someone downstream imposed an "until".
Is there a way to handle this right now, or is there a possibility to add such an "end" function?
The text was updated successfully, but these errors were encountered: