flows extensions

This commit is contained in:
InsanusMokrassar 2022-09-23 12:41:07 +06:00
parent b7934cf357
commit be52871de8
3 changed files with 20 additions and 5 deletions

View File

@ -3,7 +3,8 @@
## 0.12.15 ## 0.12.15
* `Coroutines`: * `Coroutines`:
* Add `Flow` extensions `flatMap` and `flatten` * Add `Flow` extensions `flatMap`, `flatMapNotNull` and `flatten`
* Add `Flow` extensions `takeNotNull` and `filterNotNull`
## 0.12.14 ## 0.12.14

View File

@ -1,6 +1,8 @@
package dev.inmo.micro_utils.coroutines package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import kotlin.js.JsName
import kotlin.jvm.JvmName
inline fun <T, R> Flow<Flow<T>>.flatMap( inline fun <T, R> Flow<Flow<T>>.flatMap(
crossinline mapper: suspend (T) -> R crossinline mapper: suspend (T) -> R
@ -12,6 +14,8 @@ inline fun <T, R> Flow<Flow<T>>.flatMap(
} }
} }
@JsName("flatMapIterable")
@JvmName("flatMapIterable")
inline fun <T, R> Flow<Iterable<T>>.flatMap( inline fun <T, R> Flow<Iterable<T>>.flatMap(
crossinline mapper: suspend (T) -> R crossinline mapper: suspend (T) -> R
) = map { ) = map {
@ -20,12 +24,16 @@ inline fun <T, R> Flow<Iterable<T>>.flatMap(
inline fun <T, R> Flow<Flow<T>>.flatMapNotNull( inline fun <T, R> Flow<Flow<T>>.flatMapNotNull(
crossinline mapper: suspend (T) -> R crossinline mapper: suspend (T) -> R
) = flatMap(mapper).filterNot { it == null } ) = flatMap(mapper).takeNotNull()
@JsName("flatMapNotNullIterable")
@JvmName("flatMapNotNullIterable")
inline fun <T, R> Flow<Iterable<T>>.flatMapNotNull( inline fun <T, R> Flow<Iterable<T>>.flatMapNotNull(
crossinline mapper: suspend (T) -> R crossinline mapper: suspend (T) -> R
) = flatMap(mapper).filterNot { it == null } ) = flatMap(mapper).takeNotNull()
fun <T> Flow<Iterable<T>>.flatten() = flatMap { it }
fun <T> Flow<Flow<T>>.flatten() = flatMap { it } fun <T> Flow<Flow<T>>.flatten() = flatMap { it }
@JsName("flattenIterable")
@JvmName("flattenIterable")
fun <T> Flow<Iterable<T>>.flatten() = flatMap { it }

View File

@ -0,0 +1,6 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.flow.*
fun <T> Flow<T>.takeNotNull() = mapNotNull { it }
fun <T> Flow<T>.filterNotNull() = takeNotNull()