Skip to content
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

Flow.transformWhile operator #2066

Merged
merged 7 commits into from Jul 16, 2020
Merged

Flow.transformWhile operator #2066

merged 7 commits into from Jul 16, 2020

Conversation

elizarov
Copy link
Contributor

Also, all flow-truncating operators are refactored via a common internal collectWhile operator that properly uses AbortFlowException and checks for its ownership, so that we don't have to look for bugs in interactions between all those operators (and zip, too, which is also flow-truncating).

Fixes #2065

@elizarov elizarov requested a review from qwwdfsad May 27, 2020 17:42
Copy link
Contributor

@EdwarDDay EdwarDDay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a small documentation mistake.

kotlinx-coroutines-core/common/src/flow/operators/Limit.kt Outdated Show resolved Hide resolved
kotlinx-coroutines-core/common/src/flow/operators/Limit.kt Outdated Show resolved Hide resolved
internal suspend inline fun <T> Flow<T>.collectWhile(crossinline predicate: suspend (value: T) -> Boolean) {
val collector = object : FlowCollector<T> {
override suspend fun emit(value: T) {
if (!predicate(value)) throw AbortFlowException(this)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move throw to the separate line:

if (!predicate(value)) {
    throw AbortFlowException(this)
}

It would make other people debugging experience a bit better (they can put a breakpoint on throw)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

elizarov and others added 6 commits June 22, 2020 11:45
Also, all flow-truncating operators are refactored via a common internal collectWhile operator that properly uses AbortFlowException and checks for its ownership, so that we don't have to look for bugs in interactions between all those operators (and zip, too, which is also flow-truncating).

Fixes #2065
Co-authored-by: EdwarDDay <4127904+EdwarDDay@users.noreply.github.com>
* Restore fast tail-call impl for take
* Benchmark for takeWhile
* Test flow invariants for takeWhile
(0L..Long.MAX_VALUE).asFlow().takeWhileViaCollectWhile { it < size }.consume()
}

// Direct implemenatation by checking predicate and throwing AbortFlowException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Co-authored-by: Louis CAD <louis.cognault@gmail.com>
@elizarov elizarov merged commit b42f986 into develop Jul 16, 2020
@elizarov elizarov deleted the flow-transform-while branch July 16, 2020 14:46
recheej pushed a commit to recheej/kotlinx.coroutines that referenced this pull request Dec 28, 2020
Also, most flow-truncating operators are refactored via a common internal collectWhile operator that properly uses AbortFlowException and checks for its ownership, so that we don't have to look for bugs in interactions between all those operators (and zip, too, which is also flow-truncating). But `take` operator still users a custom highly-tuned implementation.

Fixes Kotlin#2065

Co-authored-by: EdwarDDay <4127904+EdwarDDay@users.noreply.github.com>
Co-authored-by: Louis CAD <louis.cognault@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants