mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-18 14:47:15 +00:00
flows extensions
This commit is contained in:
parent
b7934cf357
commit
be52871de8
@ -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
|
||||||
|
|
||||||
|
@ -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 }
|
||||||
|
@ -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()
|
Loading…
Reference in New Issue
Block a user