Skip to content
This repository has been archived by the owner on Nov 2, 2021. It is now read-only.

Commit

Permalink
coroutines fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sokomishalov committed Dec 11, 2019
1 parent bd48ffa commit 75fce4a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ suspend inline fun <T, R> Iterable<T>.aMap(
suspend inline fun <T, R> Iterable<T>.aFlatMap(
scope: CoroutineScope? = null,
context: CoroutineContext = scope?.coroutineContext ?: EmptyCoroutineContext,
crossinline transform: suspend CoroutineScope.(T) -> Iterable<R>
noinline transform: suspend CoroutineScope.(T) -> Iterable<R>
): List<R> = withContext(context) {
val destination = mutableListOf<R>()
map { async { destination.addAll(transform(it)) } }.awaitAll()
Expand All @@ -59,7 +59,7 @@ suspend inline fun <T, R> Iterable<T>.aFlatMap(
suspend inline fun <T> Iterable<T>.aFilter(
scope: CoroutineScope? = null,
context: CoroutineContext = scope?.coroutineContext ?: EmptyCoroutineContext,
crossinline predicate: suspend CoroutineScope.(T) -> Boolean
noinline predicate: suspend CoroutineScope.(T) -> Boolean
): List<T> = withContext(context) {
val destination = mutableListOf<T>()
map { async { if (predicate(it)) destination.add(it) } }.awaitAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@

package ru.sokomishalov.commons.core.collections

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.withContext
import ru.sokomishalov.commons.core.common.unit
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext


/**
* @author sokomishalov
*/

suspend inline fun <K, V> Map<out K, V>.aForEach(
scope: CoroutineScope? = null,
context: CoroutineContext = scope?.coroutineContext ?: EmptyCoroutineContext,
noinline block: suspend CoroutineScope.(Map.Entry<K, V>) -> Unit
): Unit = withContext(context) {
map { async { block(it) } }.awaitAll().unit()
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ package ru.sokomishalov.commons.core.collections
*/

inline fun <reified K, V> Iterable<Map<K, V>>.toMap(): Map<K, V> {
return fold(mutableMapOf(), { acc, map ->
acc.putAll(map).let { acc }
})
return fold(mutableMapOf(), { acc, map -> acc.putAll(map).let { acc } })
}

0 comments on commit 75fce4a

Please sign in to comment.