any

suspend fun <T> Flow<T>.any(predicate: suspend (T) -> Boolean): Boolean(source)

A terminal operator that returns true and immediately cancels the flow if at least one element matches the given predicate.

If the flow does not emit any elements or no element matches the predicate, the function returns false.

Equivalent to !all { !predicate(it) } (see Flow.all) and !none { predicate(it) } (see Flow.none).

Example:

val myFlow = flow {
repeat(10) {
emit(it)
}
throw RuntimeException("You still didn't find the required number? I gave you ten!")
}
println(myFlow.any { it 5 }) // true
println(flowOf(1, 2, 3).any { it 5 }) // false

See also