altavir
04/26/2019, 10:50 AMn
subsequent elements and join them into one).marstran
04/26/2019, 10:59 AMfun <T> Flow<T>.chunked(chunkSize: Int) = flow {
val mutList = mutableListOf<T>()
collect { elem ->
mutList.add(elem)
if (mutList.size == chunkSize) {
emit(mutList.toList())
mutList.clear()
}
}
// Emit the final chunk if there are elements left
if (mutList.isNotEmpty()) {
emit(mutList.toList());
}
}
altavir
04/26/2019, 11:00 AM