xref: /aosp_15_r20/external/kotlinx.coroutines/kotlinx-coroutines-core/common/src/flow/terminal/Count.kt (revision 7a7160fed73afa6648ef8aa100d4a336fe921d9a)

<lambda>null1 @file:JvmMultifileClass
2 @file:JvmName("FlowKt")
3 
4 package kotlinx.coroutines.flow
5 
6 import kotlin.jvm.*
7 
8 /**
9  * Returns the number of elements in this flow.
10  */
11 public suspend fun <T> Flow<T>.count(): Int  {
12     var i = 0
13     collect {
14         ++i
15     }
16 
17     return i
18 }
19 
20 /**
21  * Returns the number of elements matching the given predicate.
22  */
countnull23 public suspend fun <T> Flow<T>.count(predicate: suspend (T) -> Boolean): Int {
24     var i = 0
25     collect { value ->
26         if (predicate(value)) {
27             ++i
28         }
29     }
30 
31     return i
32 }
33