Skip to content

java.util.Stream.consumeAsFlow() operator #1601

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

Closed
josephlbarnett opened this issue Oct 7, 2019 · 1 comment
Closed

java.util.Stream.consumeAsFlow() operator #1601

josephlbarnett opened this issue Oct 7, 2019 · 1 comment

Comments

@josephlbarnett
Copy link

josephlbarnett commented Oct 7, 2019

Given a java.util.Stream, it can be converted to a coroutine Flow in a way that ensures the stream is closed after the flow is finished (consumed or cancelled) like follows:

result.iterator().asFlow().onCompletion { result.close() }

or, alternatively:

flow {
    result.use {
        for (edge in it) {
            emit(edge)
        }
    }
}

Would be slightly more convenient to have a Stream.consumeAsFlow() implementation in the coroutines libraries that does so even less verbosely.

@qwwdfsad qwwdfsad added the flow label Oct 8, 2019
@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Oct 8, 2019

It could be nice addition to the jdk8 module.
But we cannot name it asFlow for the same reasons we don't have ReceiveChannel.asFlow (we have consumeAsFlow instead): resulting Flow can be used only once

@josephlbarnett josephlbarnett changed the title java.util.Stream.asFlow() operator java.util.Stream.consumeAsFlow() operator Oct 8, 2019
qwwdfsad added a commit that referenced this issue Oct 9, 2019
    * Even though java.lang.Stream is collected rather than consumed, collectAsFlow will clash with Flow terminology where collect is a terminal operator
    * Close the stream in the end of collection despite the fact that regular terminal operations don't do that. We are already in suspending world (empty close() call won't make any difference in a common case) and "consume" implies closing the underlying resource (note that we already do it for channels)
    * Remove obsolete examples from the module

Fixes #1601
qwwdfsad added a commit that referenced this issue Oct 9, 2019
    * Even though java.lang.Stream is collected rather than consumed, collectAsFlow will clash with Flow terminology where collect is a terminal operator
    * Close the stream in the end of collection despite the fact that regular terminal operations don't do that. We are already in suspending world (empty close() call won't make any difference in a common case) and "consume" implies closing the underlying resource (note that we already do it for channels)
    * Remove obsolete examples from the module

Fixes #1601
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants