Skip to content

Introduce Flow.any, Flow.all, Flow.none #4249

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

Conversation

CLOVIS-AI
Copy link
Contributor

As discussed in #4212.

This is my first proper contribution to Coroutines, so please be nice :)

At this stage, my goals are:
• Optimal API surface
• Correctness

I have not made any attempts to improve performance. In particular, I wouldn't be surprised if any could be improved. I believe all and none are optimal.

@CLOVIS-AI
Copy link
Contributor Author

I took inspiration from other tests, which seem to use expect(Int) and finish(Int), but I'm not completely sure what they're for nor how to use them, so I didn't. Please let me know if they are necessary.

Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb left a comment

Choose a reason for hiding this comment

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

expect and finish are used to describe when exactly which code is run. If there are 100 steps, the chain of calls must be expect(1), expect(2), expect(3) ... finish(100) and nothing else. If the test is broken and expect(5) runs before expect(4) did, the test fails.

This isn't useful for the tests you've already written. It could potentially be useful for tests that are missing, those that show short-circuiting behavior: flow { emit(1); fail("unreached") }.any { it == 1 } should finish without an error. Or maybe not.

* This operation is *terminal*.
*
* @see Iterable.any
* @see Sequence.any
Copy link
Collaborator

Choose a reason for hiding this comment

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

Flow.all also seems relevant.

@@ -164,3 +164,46 @@ public fun <T> Flow<T>.chunked(size: Int): Flow<List<T>> {
result?.let { emit(it) }
}
}

/**
* Returns `true` if at least one element matches the given [predicate].
Copy link
Collaborator

Choose a reason for hiding this comment

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

Another important property is that it immediately stops collecting the flow. See https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/first.html

Also, this only returns false if the flow completes without emitting a value that matches the predicate. I'm not sure if it's worth spelling out (this does seem obvious).

Finally, please include a simple usage sample in the documentation for every new function.

*/
@ExperimentalCoroutinesApi
public suspend fun <T> Flow<T>.all(predicate: suspend (T) -> Boolean): Boolean =
count { !predicate(it) } == 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

This implementation will hang in this scenario: flow { while (true) { emit(5) } }.all { it == 4 }.

*/
@ExperimentalCoroutinesApi
public suspend fun <T> Flow<T>.none(predicate: suspend (T) -> Boolean): Boolean =
count { predicate(it) } == 0
Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb Oct 15, 2024

Choose a reason for hiding this comment

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

Likewise, it will hang for flow { while (true) { emit(5) } }.none { it == 5 }. Please include these scenarios (and a similar one for any) as tests.

@qwwdfsad
Copy link
Contributor

@CLOVIS-AI would you like to continue this PR or should we handle it from here?

@CLOVIS-AI
Copy link
Contributor Author

I would love to, but sadly my schedule doesn't allow me too… Feel free to make the changes you want if I'm too slow :)

@dkhalanskyjb
Copy link
Collaborator

Closed in favor of #4304

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.

3 participants