All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
fuookami.ospf.kotlin.utils.parallel.Collection.kt Maven / Gradle / Ivy
package fuookami.ospf.kotlin.utils.parallel
import kotlin.reflect.full.*
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import fuookami.ospf.kotlin.utils.math.*
import fuookami.ospf.kotlin.utils.math.ordinary.minMaxWith
import fuookami.ospf.kotlin.utils.math.ordinary.minMaxWithOrNull
import fuookami.ospf.kotlin.utils.error.*
import fuookami.ospf.kotlin.utils.operator.*
import fuookami.ospf.kotlin.utils.functional.*
val Collection<*>.defaultConcurrentAmount: UInt64
get() = UInt64(
maxOf(
minOf(
Flt64(this.size).log(Flt64.two)!!.toFlt64().floor().toUInt64().toInt(),
Runtime.getRuntime().availableProcessors()
),
1
)
)
suspend inline fun Iterable.allParallelly(
crossinline predicate: SuspendPredicate
): Boolean {
return try {
coroutineScope {
val channel = Channel()
for (element in [email protected] ()) {
launch(Dispatchers.Default) {
channel.send(predicate(element))
}
}
for (value in channel) {
if (!value) {
cancel()
}
}
true
}
} catch (e: CancellationException) {
false
}
}
suspend inline fun Iterable.tryAllParallelly(
crossinline predicate: SuspendTryPredicate
): Ret {
var error: Error? = null
return try {
coroutineScope {
val channel = Channel>()
for (element in [email protected] ()) {
launch(Dispatchers.Default) {
channel.send(predicate(element))
}
}
for (result in channel) {
when (result) {
is Ok -> {
if (!result.value) {
cancel()
}
}
is Failed -> {
error = result.error
cancel()
}
}
}
Ok(true)
}
} catch (e: CancellationException) {
error?.let { Failed(it) }
?: Ok(false)
}
}
suspend inline fun Iterable.anyParallelly(
crossinline predicate: SuspendPredicate
): Boolean {
return try {
coroutineScope {
val channel = Channel()
for (element in [email protected] ()) {
launch(Dispatchers.Default) {
channel.send(predicate(element))
}
}
for (value in channel) {
if (value) {
cancel()
}
}
false
}
} catch (e: CancellationException) {
true
}
}
suspend inline fun Iterable.tryAnyParallelly(
crossinline predicate: SuspendTryPredicate
): Ret {
var error: Error? = null
return try {
coroutineScope {
val channel = Channel>()
for (element in [email protected] ()) {
launch(Dispatchers.Default) {
channel.send(predicate(element))
}
}
for (result in channel) {
when (result) {
is Ok -> {
if (result.value) {
cancel()
}
}
is Failed -> {
error = result.error
cancel()
}
}
}
Ok(false)
}
} catch (e: CancellationException) {
error?.let { Failed(it) }
?: Ok(true)
}
}
suspend inline fun Iterable.noneParallelly(
crossinline predicate: SuspendPredicate
): Boolean {
return try {
coroutineScope {
val channel = Channel()
for (element in [email protected] ()) {
launch(Dispatchers.Default) {
channel.send(predicate(element))
}
}
for (value in channel) {
if (value) {
cancel()
}
}
true
}
} catch (e: CancellationException) {
false
}
}
suspend inline fun Iterable.tryNoneParallelly(
crossinline predicate: SuspendTryPredicate
): Ret {
var error: Error? = null
return try {
coroutineScope {
val channel = Channel>()
for (element in [email protected] ()) {
launch(Dispatchers.Default) {
channel.send(predicate(element))
}
}
for (result in channel) {
when (result) {
is Ok -> {
if (result.value) {
cancel()
}
}
is Failed -> {
error = result.error
cancel()
}
}
}
Ok(true)
}
} catch (e: CancellationException) {
error?.let { Failed(it) }
?: Ok(false)
}
}
suspend inline fun Iterable.countParallelly(
crossinline predicate: SuspendPredicate
): Int {
return coroutineScope {
val promises = ArrayList>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) { predicate(element) })
}
promises.count { it.await() }
}
}
suspend inline fun Iterable.tryCountParallelly(
crossinline predicate: SuspendTryPredicate
): Ret {
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) { predicate(element) })
}
Ok(promises.count {
when (val result = it.await()) {
is Ok -> {
result.value
}
is Failed -> {
error = result.error
cancel()
false
}
}
})
}
} catch (e: CancellationException) {
error?.let { Failed(it) }
?: Ok(0)
}
}
suspend inline fun Iterable.firstParallelly(
crossinline predicate: SuspendPredicate
): T {
return this.firstOrNullParallelly(predicate)
?: throw NoSuchElementException("Collection contains no element matching the predicate.")
}
suspend inline fun Iterable.tryFirstParallelly(
crossinline predicate: SuspendTryPredicate
): Ret {
return when (val result = this.tryFirstOrNullParallelly(predicate)) {
is Ok -> {
result.value
?.let { Ok(it) }
?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the predicate."))
}
is Failed -> {
Failed(result.error)
}
}
}
suspend inline fun Iterable.firstOrNullParallelly(
crossinline predicate: SuspendPredicate
): T? {
var result: T? = null
return try {
coroutineScope {
val promises = ArrayList>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
if (predicate(element)) {
element
} else {
null
}
})
}
for (promise in promises) {
result = promise.await()
if (result != null) {
cancel()
}
}
null
}
} catch (e: CancellationException) {
result
}
}
suspend inline fun Iterable.tryFirstOrNullParallelly(
crossinline predicate: SuspendTryPredicate
): Ret {
var result: T? = null
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
when (val ret = predicate(element)) {
is Ok -> {
if (ret.value) {
Ok(element)
} else {
Ok(null)
}
}
is Failed -> {
Failed(ret.error)
}
}
})
for (promise in promises) {
when (val ret = promise.await()) {
is Ok -> {
result = ret.value
if (result != null) {
cancel()
}
}
is Failed -> {
error = ret.error
cancel()
}
}
}
}
Ok(null)
}
} catch (e: CancellationException) {
result?.let { Ok(it) }
?: error?.let { Failed(it) }
?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the predicate."))
}
}
suspend inline fun Iterable.firstNotNullOfParallelly(
crossinline extractor: SuspendExtractor
): R {
return this.firstNotNullOfOrNullParallelly(extractor)
?: throw NoSuchElementException("No element of the collection was transformed to a non-null value.")
}
suspend inline fun Iterable.tryFirstNotNullOfParallelly(
crossinline extractor: SuspendTryExtractor
): Ret {
return when (val result = this.tryFirstNotNullOfOrNullParallelly(extractor)) {
is Ok -> {
result.value
?.let { Ok(it) }
?: Failed(
Err(
ErrorCode.ApplicationException,
"No element of the collection was transformed to a non-null value."
)
)
}
is Failed -> {
Failed(result.error)
}
}
}
suspend inline fun Iterable.firstNotNullOfOrNullParallelly(
crossinline extractor: SuspendExtractor
): R? {
var result: R? = null
return try {
coroutineScope {
val promises = ArrayList>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
extractor(element)
})
}
for (promise in promises) {
result = promise.await()
if (result != null) {
cancel()
}
}
null
}
} catch (e: CancellationException) {
result
}
}
suspend inline fun Iterable.tryFirstNotNullOfOrNullParallelly(
crossinline extractor: SuspendTryExtractor
): Ret {
var result: R? = null
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
extractor(element)
})
}
for (promise in promises) {
when (val ret = promise.await()) {
is Ok -> {
result = ret.value
if (result != null) {
cancel()
}
}
is Failed -> {
error = ret.error
cancel()
}
}
}
Ok(null)
}
} catch (e: CancellationException) {
result?.let { Ok(it) }
?: error?.let { Failed(it) }
?: Failed(
Err(
ErrorCode.ApplicationException,
"No element of the collection was transformed to a non-null value."
)
)
}
}
suspend inline fun Iterable.lastParallelly(
crossinline predicate: SuspendPredicate
): T {
return this.lastOrNullParallelly(predicate)
?: throw NoSuchElementException("Collection contains no element matching the predicate.")
}
suspend inline fun Iterable.tryLastParallelly(
crossinline predicate: SuspendTryPredicate
): Ret {
return when (val result = this.tryLastOrNullParallelly(predicate)) {
is Ok -> {
result.value
?.let { Ok(it) }
?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the predicate."))
}
is Failed -> {
Failed(result.error)
}
}
}
suspend inline fun Iterable.lastOrNullParallelly(
crossinline predicate: SuspendPredicate
): T? {
var result: T? = null
return try {
coroutineScope {
val promises = ArrayList>()
for (element in [email protected] ().iterator()) {
promises.add(async(Dispatchers.Default) {
if (predicate(element)) {
element
} else {
null
}
})
}
for (promise in promises) {
result = promise.await()
if (result != null) {
cancel()
}
}
null
}
} catch (e: CancellationException) {
result
}
}
suspend inline fun Iterable.tryLastOrNullParallelly(
crossinline predicate: SuspendTryPredicate
): Ret {
var result: T? = null
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for (element in [email protected] ().iterator()) {
promises.add(async(Dispatchers.Default) {
when (val ret = predicate(element)) {
is Ok -> {
if (ret.value) {
Ok(element)
} else {
Ok(null)
}
}
is Failed -> {
Failed(ret.error)
}
}
})
}
for (promise in promises) {
when (val ret = promise.await()) {
is Ok -> {
result = ret.value
if (result != null) {
cancel()
}
}
is Failed -> {
error = ret.error
cancel()
}
}
}
Ok(null)
}
} catch (e: CancellationException) {
result?.let { Ok(it) }
?: error?.let { Failed(it) }
?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the predicate."))
}
}
suspend inline fun Iterable.lastNotNullOfParallelly(
crossinline extractor: SuspendExtractor
): R {
return this.lastNotNullOfOrNullParallelly(extractor)
?: throw NoSuchElementException("No element of the collection was transformed to a non-null value.")
}
suspend inline fun Iterable.tryLastNotNullOfParallelly(
crossinline extractor: SuspendTryExtractor
): Ret {
return when (val result = this.tryLastNotNullOfOrNullParallelly(extractor)) {
is Ok -> {
result.value
?.let { Ok(it) }
?: Failed(
Err(
ErrorCode.ApplicationException,
"No element of the collection was transformed to a non-null value."
)
)
}
is Failed -> {
Failed(result.error)
}
}
}
suspend inline fun Iterable.lastNotNullOfOrNullParallelly(
crossinline extractor: SuspendExtractor
): R? {
var result: R? = null
return try {
coroutineScope {
val promises = ArrayList>()
for (element in [email protected] ().iterator()) {
promises.add(async(Dispatchers.Default) {
extractor(element)
})
}
for (promise in promises) {
result = promise.await()
if (result != null) {
cancel()
}
}
null
}
} catch (e: CancellationException) {
result
}
}
suspend inline fun Iterable.tryLastNotNullOfOrNullParallelly(
crossinline extractor: SuspendTryExtractor
): Ret {
var result: R? = null
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for (element in [email protected] ().iterator()) {
promises.add(async(Dispatchers.Default) {
extractor(element)
})
}
for (promise in promises) {
when (val ret = promise.await()) {
is Ok -> {
result = ret.value
if (result != null) {
cancel()
}
}
is Failed -> {
error = ret.error
cancel()
}
}
}
Ok(null)
}
} catch (e: CancellationException) {
result?.let { Ok(it) }
?: error?.let { Failed(it) }
?: Failed(
Err(
ErrorCode.ApplicationException,
"No element of the collection was transformed to a non-null value."
)
)
}
}
suspend inline fun Iterable.findParallelly(
crossinline predicate: SuspendPredicate
): T? {
return this.firstOrNullParallelly(predicate)
}
suspend inline fun Iterable.tryFindParallelly(
crossinline predicate: SuspendTryPredicate
): Ret {
return this.tryFirstOrNullParallelly(predicate)
}
suspend inline fun Iterable.findLastParallelly(
crossinline predicate: SuspendPredicate
): T? {
return this.lastOrNullParallelly(predicate)
}
suspend inline fun Iterable.tryFindLastParallelly(
crossinline predicate: SuspendTryPredicate
): Ret {
return this.tryLastOrNullParallelly(predicate)
}
suspend inline fun Iterable.filterParallelly(
crossinline predicate: SuspendPredicate
): List {
return this.filterToParallelly(ArrayList(), predicate)
}
suspend inline fun Iterable.tryFilterParallelly(
crossinline predicate: SuspendTryPredicate
): Ret> {
return this.tryFilterToParallelly(ArrayList(), predicate)
}
suspend inline fun > Iterable.filterToParallelly(
destination: C,
crossinline predicate: SuspendPredicate
): C {
return coroutineScope {
val promises = ArrayList>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
if (predicate(element)) {
element
} else {
null
}
})
}
promises.mapNotNullTo(destination) { it.await() }
}
}
suspend inline fun > Iterable.tryFilterToParallelly(
destination: C,
crossinline predicate: SuspendTryPredicate
): Ret {
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
when (val ret = predicate(element)) {
is Ok -> {
if (ret.value) {
Ok(element)
} else {
Ok(null)
}
}
is Failed -> {
Failed(ret.error)
}
}
})
}
val result = promises.mapNotNull {
when (val ret = it.await()) {
is Ok -> {
ret.value
}
is Failed -> {
error = ret.error
cancel()
null
}
}
}
destination.addAll(result)
Ok(destination)
}
} catch (e: CancellationException) {
error?.let { Failed(it) }
?: Ok(destination)
}
}
suspend inline fun Iterable.filterNotNullParallelly(
crossinline predicate: SuspendPredicate
): List {
return this.filterNotNullToParallelly(ArrayList(), predicate)
}
suspend inline fun Iterable.tryFilterNotNullParallelly(
crossinline predicate: SuspendTryPredicate
): Ret> {
return this.tryFilterNotNullToParallelly(ArrayList(), predicate)
}
suspend inline fun > Iterable.filterNotNullToParallelly(
destination: C,
crossinline predicate: SuspendPredicate
): C {
return coroutineScope {
val promises = ArrayList>()
for (element in [email protected] ()) {
if (element != null) {
promises.add(async(Dispatchers.Default) {
if (predicate(element)) {
element
} else {
null
}
})
}
}
promises.mapNotNullTo(destination) { it.await() }
}
}
suspend inline fun > Iterable.tryFilterNotNullToParallelly(
destination: C,
crossinline predicate: SuspendTryPredicate
): Ret {
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for (element in [email protected] ()) {
if (element != null) {
promises.add(async(Dispatchers.Default) {
when (val ret = predicate(element)) {
is Ok -> {
if (ret.value) {
Ok(element)
} else {
Ok(null)
}
}
is Failed -> {
Failed(ret.error)
}
}
})
}
}
val result = promises.mapNotNull {
when (val ret = it.await()) {
is Ok -> {
ret.value
}
is Failed -> {
error = ret.error
cancel()
null
}
}
}
destination.addAll(result)
Ok(destination)
}
} catch (e: CancellationException) {
error?.let { Failed(it) }
?: Ok(destination)
}
}
suspend inline fun Iterable.filterNotParallelly(
crossinline predicate: SuspendPredicate
): List {
return this.filterNotToParallelly(ArrayList(), predicate)
}
suspend inline fun Iterable.tryFilterNotParallelly(
crossinline predicate: SuspendTryPredicate
): Ret> {
return this.tryFilterNotToParallelly(ArrayList(), predicate)
}
suspend inline fun > Iterable.filterNotToParallelly(
destination: C,
crossinline predicate: SuspendPredicate
): C {
return coroutineScope {
val promises = ArrayList>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
if (predicate(element)) {
null
} else {
element
}
})
}
promises.mapNotNullTo(destination) { it.await() }
}
}
suspend inline fun > Iterable.tryFilterNotToParallelly(
destination: C,
crossinline predicate: SuspendTryPredicate
): Ret {
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
when (val ret = predicate(element)) {
is Ok -> {
if (ret.value) {
Ok(null)
} else {
Ok(element)
}
}
is Failed -> {
Failed(ret.error)
}
}
})
}
val result = promises.mapNotNull {
when (val ret = it.await()) {
is Ok -> {
ret.value
}
is Failed -> {
error = ret.error
cancel()
null
}
}
}
destination.addAll(result)
Ok(destination)
}
} catch (e: CancellationException) {
error?.let { Failed(it) }
?: Ok(destination)
}
}
suspend inline fun Iterable.filterIndexedParallelly(
crossinline predicate: SuspendIndexedPredicate
): List {
return this.filterIndexedToParallelly(ArrayList(), predicate)
}
suspend inline fun Iterable.tryFilterIndexedParallelly(
crossinline predicate: SuspendTryIndexedPredicate
): Ret> {
return this.tryFilterIndexedToParallelly(ArrayList(), predicate)
}
suspend inline fun > Iterable.filterIndexedToParallelly(
destination: C,
crossinline predicate: SuspendIndexedPredicate
): C {
return coroutineScope {
val promises = ArrayList>()
for ((index, element) in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
if (predicate(index, element)) {
element
} else {
null
}
})
}
promises.mapNotNullTo(destination) { it.await() }
}
}
suspend inline fun > Iterable.tryFilterIndexedToParallelly(
destination: C,
crossinline predicate: SuspendTryIndexedPredicate
): Ret {
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for ((index, element) in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
when (val ret = predicate(index, element)) {
is Ok -> {
if (ret.value) {
Ok(element)
} else {
Ok(null)
}
}
is Failed -> {
Failed(ret.error)
}
}
})
}
val result = promises.mapNotNull {
when (val ret = it.await()) {
is Ok -> {
ret.value
}
is Failed -> {
error = ret.error
cancel()
null
}
}
}
destination.addAll(result)
Ok(destination)
}
} catch (e: CancellationException) {
error?.let { Failed(it) }
?: Ok(destination)
}
}
suspend inline fun Iterable.filterIsInstanceParallelly(
crossinline predicate: SuspendPredicate
): List {
return this.filterIsInstanceToParallelly(ArrayList(), predicate)
}
suspend inline fun Iterable.tryFilterIsInstanceParallelly(
crossinline predicate: SuspendTryPredicate
): Ret> {
return this.tryFilterIsInstanceToParallelly(ArrayList(), predicate)
}
suspend inline fun > Iterable.filterIsInstanceToParallelly(
destination: C,
crossinline predicate: SuspendPredicate
): C {
return coroutineScope {
val promises = ArrayList>()
for (element in [email protected] ()) {
if (element is U) {
promises.add(async(Dispatchers.Default) {
if (predicate(element)) {
element
} else {
null
}
})
}
}
promises.mapNotNullTo(destination) { it.await() }
}
}
suspend inline fun > Iterable.tryFilterIsInstanceToParallelly(
destination: C,
crossinline predicate: SuspendTryPredicate
): Ret {
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for (element in [email protected] ()) {
if (element is U) {
promises.add(async(Dispatchers.Default) {
when (val ret = predicate(element)) {
is Ok -> {
if (ret.value) {
Ok(element)
} else {
Ok(null)
}
}
is Failed -> {
Failed(ret.error)
}
}
})
}
}
val result = promises.mapNotNull {
when (val ret = it.await()) {
is Ok -> {
ret.value
}
is Failed -> {
error = ret.error
cancel()
null
}
}
}
destination.addAll(result)
Ok(destination)
}
} catch (e: CancellationException) {
error?.let { Failed(it) }
?: Ok(destination)
}
}
suspend inline fun Iterable.mapParallelly(
crossinline extractor: SuspendExtractor
): List {
return this.mapToParallelly(ArrayList(), extractor)
}
suspend inline fun Iterable.tryMapParallelly(
crossinline extractor: SuspendTryExtractor
): Ret> {
return this.tryMapToParallelly(ArrayList(), extractor)
}
suspend inline fun > Iterable.mapToParallelly(
destination: C,
crossinline extractor: SuspendExtractor
): C {
return coroutineScope {
val promises = ArrayList>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
extractor(element)
})
}
promises.mapTo(destination) { it.await() }
}
}
suspend inline fun > Iterable.tryMapToParallelly(
destination: C,
crossinline extractor: SuspendTryExtractor
): Ret {
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
extractor(element)
})
}
val result = promises.mapNotNull {
when (val ret = it.await()) {
is Ok -> {
ret.value
}
is Failed -> {
error = ret.error
cancel()
null
}
}
}
destination.addAll(result)
Ok(destination)
}
} catch (e: CancellationException) {
error?.let { Failed(it) }
?: Ok(destination)
}
}
suspend inline fun Iterable.mapNotNullParallelly(
crossinline extractor: SuspendExtractor
): List {
return this.mapNotNullToParallelly(ArrayList(), extractor)
}
suspend inline fun Iterable.tryMapNotNullParallelly(
crossinline extractor: SuspendTryExtractor
): Ret> {
return this.tryMapNotNullToParallelly(ArrayList(), extractor)
}
suspend inline fun > Iterable.mapNotNullToParallelly(
destination: C,
crossinline extractor: SuspendExtractor
): C {
return coroutineScope {
val promises = ArrayList>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
extractor(element)
})
}
promises.mapNotNullTo(destination) { it.await() }
}
}
suspend inline fun > Iterable.tryMapNotNullToParallelly(
destination: C,
crossinline extractor: SuspendTryExtractor
): Ret {
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for (element in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
extractor(element)
})
}
val result = promises.mapNotNull {
when (val ret = it.await()) {
is Ok -> {
ret.value
}
is Failed -> {
error = ret.error
cancel()
null
}
}
}
destination.addAll(result)
Ok(destination)
}
} catch (e: CancellationException) {
error?.let { Failed(it) }
?: Ok(destination)
}
}
suspend inline fun Iterable.mapIndexedParallelly(
crossinline extractor: SuspendIndexedExtractor
): List {
return this.mapIndexedToParallelly(ArrayList(), extractor)
}
suspend inline fun Iterable.tryMapIndexedParallelly(
crossinline extractor: SuspendTryIndexedExtractor
): Ret> {
return this.tryMapIndexedToParallelly(ArrayList(), extractor)
}
suspend inline fun > Iterable.mapIndexedToParallelly(
destination: C,
crossinline extractor: SuspendIndexedExtractor
): C {
return coroutineScope {
val promises = ArrayList>()
for ((index, element) in [email protected] ()) {
promises.add(async(Dispatchers.Default) {
extractor(index, element)
})
}
promises.mapTo(destination) { it.await() }
}
}
suspend inline fun > Iterable.tryMapIndexedToParallelly(
destination: C,
crossinline extractor: SuspendTryIndexedExtractor
): Ret {
var error: Error? = null
return try {
coroutineScope {
val promises = ArrayList>>()
for ((index, element) in