nwh
05/04/2019, 12:58 AMFlow
could use a chunked
method, just like List
has:
fun <T> Flow<T>.chunked(size: Int) = flow {
val list = mutableListOf<T>()
collect {
list.add(it)
if (list.size == size) {
emit(list.toList())
list.clear()
}
}
if (list.isNotEmpty())
emit(list)
}
Might be good to include in the standard library. Loving the API 👍🏻louiscad
05/04/2019, 10:26 AMnwh
05/04/2019, 3:38 PMfun <T> Flow<T>.chunked(size: Int): Flow<List<T>> = flow {
val list = mutableListOf<T>()
collect {
list.add(it)
if (list.size == size) {
emit(list.toList())
list.clear()
}
}
if (list.isNotEmpty())
emit(list)
}