flows extensions

This commit is contained in:
InsanusMokrassar 2022-09-23 12:33:00 +06:00
parent dbfbeef90a
commit b7934cf357
2 changed files with 34 additions and 0 deletions

View File

@ -2,6 +2,9 @@
## 0.12.15
* `Coroutines`:
* Add `Flow` extensions `flatMap` and `flatten`
## 0.12.14
* `Versions`:

View File

@ -0,0 +1,31 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.flow.*
inline fun <T, R> Flow<Flow<T>>.flatMap(
crossinline mapper: suspend (T) -> R
) = flow {
collect {
it.collect {
emit(mapper(it))
}
}
}
inline fun <T, R> Flow<Iterable<T>>.flatMap(
crossinline mapper: suspend (T) -> R
) = map {
it.asFlow()
}.flatMap(mapper)
inline fun <T, R> Flow<Flow<T>>.flatMapNotNull(
crossinline mapper: suspend (T) -> R
) = flatMap(mapper).filterNot { it == null }
inline fun <T, R> Flow<Iterable<T>>.flatMapNotNull(
crossinline mapper: suspend (T) -> R
) = flatMap(mapper).filterNot { it == null }
fun <T> Flow<Iterable<T>>.flatten() = flatMap { it }
fun <T> Flow<Flow<T>>.flatten() = flatMap { it }