-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Introduce Flow.all/any/none operators #4212
Comments
Simplified implementation: suspend fun <T> Flow<T>.any(predicate: (T) -> Boolean) = this
.filter { predicate(it) }
.map { true }
.onEmpty { emit(false) }
.first() |
We had a Slack discussion about the use case which prompted this (https://kotlinlang.slack.com/archives/C1CFAFJSK/p1723480063142319). What I took away from that discussion surprised me, but I now believe that The use case is completely linear, with few hints at asynchronous behavior. A list is taken, converted to a Conceptually,
It seems to me like |
That's a really nice angle! Originally, we thought of a Yet the reality begs to differ -- among other things, There are not really many upsides of keeping the status quo, I think |
Use case
I have a complex
Flow
that possesses many elements. I have a business rule that is, literally, to do something if any of them satisfies a condition.If I were using
List
orSet
, I would useany { theCondition(it) }
. However,Flow
doesn't seem to haveany
.There is an old issue (#2239) that asks for this feature, and is closed because of alternative implementations:
I dislike this solution because:
Flow<Foo?>
becausefirstOrNull
is only available on non-nullable types.I believe it is worth having
any
/all
/none
directly in the library because the proposed implementation have downsides.The Shape of the API
Prior Art
These three functions are already available on
Iterable
andSequence
. The behavior should be the same.all
andnone
can use thecount
operator under the hood, since it already shortcuts. I believe this is a possible implementation ofany
, though I haven't tested it yet:I can submit a PR if you think this is worth pursuing.
The text was updated successfully, but these errors were encountered: