All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fuookami.ospf.kotlin.utils.parallel.Collection.kt Maven / Gradle / Ivy

There is a newer version: 1.0.29
Show newest version
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 [email protected]()) {
                promises.add(async(Dispatchers.Default) {
                    extractor(index, 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.mapIndexedNotNullParallelly(
    crossinline extractor: SuspendIndexedExtractor
): List {
    return this.mapIndexedNotNullToParallelly(ArrayList(), extractor)
}

suspend inline fun  Iterable.tryMapIndexedNotNullParallelly(
    crossinline extractor: SuspendTryIndexedExtractor
): Ret> {
    return this.tryMapIndexedNotNullToParallelly(ArrayList(), extractor)
}

suspend inline fun > Iterable.mapIndexedNotNullToParallelly(
    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.mapNotNullTo(destination) { it.await() }
    }
}

suspend inline fun > Iterable.tryMapIndexedNotNullToParallelly(
    destination: C,
    crossinline extractor: SuspendTryIndexedExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>>()
            for ((index, element) in [email protected]()) {
                promises.add(async(Dispatchers.Default) {
                    extractor(index, 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.associateParallelly(
    crossinline extractor: SuspendExtractor, T>
): Map {
    return this.associateToParallelly(LinkedHashMap(), extractor)
}

suspend inline fun  Iterable.tryAssociateToParallelly(
    crossinline extractor: SuspendTryExtractor, T>
): Ret> {
    return this.tryAssociateToParallelly(LinkedHashMap(), extractor)
}

suspend inline fun > Iterable.associateToParallelly(
    destination: M,
    crossinline extractor: SuspendExtractor, T>
): M {
    return coroutineScope {
        val promises = ArrayList>>()
        for (element in [email protected]()) {
            promises.add(async(Dispatchers.Default) {
                extractor(element)
            })
        }
        promises.associateTo(destination) { it.await() }
    }
}

suspend inline fun > Iterable.tryAssociateToParallelly(
    destination: M,
    crossinline extractor: SuspendTryExtractor, T>
): 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.putAll(result)
            Ok(destination)
        }
    } catch (e: CancellationException) {
        error?.let { Failed(it) }
            ?: Ok(destination)
    }
}

suspend inline fun  Iterable.associateByParallelly(
    crossinline keyExtractor: SuspendExtractor
): Map {
    return this.associateByToParallelly(LinkedHashMap(), keyExtractor)
}

suspend inline fun  Iterable.tryAssociateByToParallelly(
    crossinline keyExtractor: SuspendTryExtractor
): Ret> {
    return this.tryAssociateByToParallelly(LinkedHashMap(), keyExtractor)
}

suspend inline fun > Iterable.associateByToParallelly(
    destination: M,
    crossinline keyExtractor: SuspendExtractor
): M {
    return coroutineScope {
        val promises = ArrayList>>()
        for (element in [email protected]()) {
            promises.add(async(Dispatchers.Default) {
                keyExtractor(element) to element
            })
        }
        promises.associateTo(destination) { it.await() }
    }
}

suspend inline fun > Iterable.tryAssociateByToParallelly(
    destination: M,
    crossinline keyExtractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>>>()
            for (element in [email protected]()) {
                promises.add(async(Dispatchers.Default) {
                    keyExtractor(element).map {
                        it to element
                    }
                })
            }
            val result = promises.mapNotNull {
                when (val ret = it.await()) {
                    is Ok -> {
                        ret.value
                    }

                    is Failed -> {
                        error = ret.error
                        cancel()
                        null
                    }
                }
            }
            destination.putAll(result)
            Ok(destination)
        }
    } catch (e: CancellationException) {
        error?.let { Failed(it) }
            ?: Ok(destination)
    }
}

suspend inline fun  Iterable.associateWithParallelly(
    crossinline valueExtractor: SuspendExtractor
): Map {
    return this.associateWithToParallelly(LinkedHashMap(), valueExtractor)
}

suspend inline fun  Iterable.tryAssociateWithToParallelly(
    crossinline valueExtractor: SuspendTryExtractor
): Ret> {
    return this.tryAssociateWithToParallelly(LinkedHashMap(), valueExtractor)
}

suspend inline fun > Iterable.associateWithToParallelly(
    destination: M,
    crossinline valueExtractor: SuspendExtractor
): M {
    return coroutineScope {
        val promises = ArrayList>>()
        for (element in [email protected]()) {
            promises.add(async(Dispatchers.Default) {
                element to valueExtractor(element)
            })
        }
        promises.associateTo(destination) { it.await() }
    }
}

suspend inline fun > Iterable.tryAssociateWithToParallelly(
    destination: M,
    crossinline valueExtractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>>>()
            for (element in [email protected]()) {
                promises.add(async(Dispatchers.Default) {
                    valueExtractor(element).map {
                        element to it
                    }
                })
            }
            val result = promises.mapNotNull {
                when (val ret = it.await()) {
                    is Ok -> {
                        ret.value
                    }

                    is Failed -> {
                        error = ret.error
                        cancel()
                        null
                    }
                }
            }
            destination.putAll(result)
            Ok(destination)
        }
    } catch (e: CancellationException) {
        error?.let { Failed(it) }
            ?: Ok(destination)
    }
}

suspend inline fun  Iterable.flatMapParallelly(
    crossinline extractor: SuspendExtractor, T>
): List {
    return this.flatMapToParallelly(ArrayList(), extractor)
}

suspend inline fun  Iterable.tryFlatMapToParallelly(
    crossinline extractor: SuspendTryExtractor, T>
): Ret> {
    return this.tryFlatMapToParallelly(ArrayList(), extractor)
}

suspend inline fun > Iterable.flatMapToParallelly(
    destination: C,
    crossinline extractor: SuspendExtractor, T>
): C {
    return coroutineScope {
        val promises = ArrayList>>()
        for (element in [email protected]()) {
            promises.add(async(Dispatchers.Default) {
                extractor(element)
            })
        }
        promises.flatMapTo(destination) { it.await() }
    }
}

suspend inline fun > Iterable.tryFlatMapToParallelly(
    destination: C,
    crossinline extractor: SuspendTryExtractor, T>
): 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.flatten())
            Ok(destination)
        }
    } catch (e: CancellationException) {
        error?.let { Failed(it) }
            ?: Ok(destination)
    }
}

suspend inline fun > Iterable.flatMapIndexedParallelly(
    crossinline extractor: SuspendIndexedExtractor, T>
): List {
    return this.flatMapIndexedToParallelly(ArrayList(), extractor)
}

suspend inline fun > Iterable.tryFlatMapIndexedToParallelly(
    crossinline extractor: SuspendTryIndexedExtractor, T>
): Ret> {
    return this.tryFlatMapIndexedToParallelly(ArrayList(), extractor)
}

suspend inline fun > Iterable.flatMapIndexedToParallelly(
    destination: C,
    crossinline extractor: SuspendIndexedExtractor, T>
): C {
    return coroutineScope {
        val promises = ArrayList>>()
        for ((index, element) in [email protected]()) {
            promises.add(async(Dispatchers.Default) {
                extractor(index, element)
            })
        }
        promises.flatMapTo(destination) { it.await() }
    }
}

suspend inline fun > Iterable.tryFlatMapIndexedToParallelly(
    destination: C,
    crossinline extractor: SuspendTryIndexedExtractor, T>
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>>>()
            for ((index, element) in [email protected]()) {
                promises.add(async(Dispatchers.Default) {
                    extractor(index, element)
                })
            }
            val result = promises.mapNotNull {
                when (val ret = it.await()) {
                    is Ok -> {
                        ret.value
                    }

                    is Failed -> {
                        error = ret.error
                        cancel()
                        null
                    }
                }
            }
            destination.addAll(result.flatten())
            Ok(destination)
        }
    } catch (e: CancellationException) {
        error?.let { Failed(it) }
            ?: Ok(destination)
    }
}

suspend inline fun > Iterable.flatMapIndexedNotNullParallelly(
    crossinline extractor: SuspendIndexedExtractor, T>
): List {
    return this.flatMapIndexedNotNullToParallelly(ArrayList(), extractor)
}


suspend inline fun > Iterable.tryFlatMapIndexedNotNullToParallelly(
    crossinline extractor: SuspendTryIndexedExtractor, T>
): Ret> {
    return this.tryFlatMapIndexedNotNullToParallelly(ArrayList(), extractor)
}

suspend inline fun > Iterable.flatMapIndexedNotNullToParallelly(
    destination: C,
    crossinline extractor: SuspendIndexedExtractor, T>
): C {
    return coroutineScope {
        val promises = ArrayList>>()
        for ((index, element) in [email protected]()) {
            promises.add(async(Dispatchers.Default) {
                extractor(index, element)
            })
        }
        for (promise in promises) {
            destination.addAll(promise.await().filterNotNull())
        }
        destination
    }
}

suspend inline fun > Iterable.tryFlatMapIndexedNotNullToParallelly(
    destination: C,
    crossinline extractor: SuspendTryIndexedExtractor, T>
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>>>()
            for ((index, element) in [email protected]()) {
                promises.add(async(Dispatchers.Default) {
                    extractor(index, element)
                })
            }
            val result = promises.mapNotNull {
                when (val ret = it.await()) {
                    is Ok -> {
                        ret.value
                    }

                    is Failed -> {
                        error = ret.error
                        cancel()
                        null
                    }
                }
            }
            for (iterable in result) {
                destination.addAll(iterable.filterNotNull())
            }
            Ok(destination)
        }
    } catch (e: CancellationException) {
        error?.let { Failed(it) }
            ?: Ok(destination)
    }
}

@Suppress("UNCHECKED_CAST")
suspend inline fun  Iterable.sumOfParallelly(
    crossinline extractor: SuspendExtractor
): U where U : Arithmetic, U : Plus {
    return coroutineScope {
        val promises = ArrayList>()
        for (element in [email protected]()) {
            promises.add(async(Dispatchers.Default) {
                extractor(element)
            })
        }
        var sum = (U::class.companionObjectInstance!! as ArithmeticConstants).zero
        for (promise in promises) {
            sum += promise.await()
        }
        sum
    }
}

@Suppress("UNCHECKED_CAST")
suspend inline fun  Iterable.trySumOfParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret where U : Arithmetic, U : Plus {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>>()
            for (element in [email protected]()) {
                promises.add(async(Dispatchers.Default) {
                    extractor(element)
                })
            }
            var sum = (U::class.companionObjectInstance!! as ArithmeticConstants).zero
            for (promise in promises) {
                sum += when (val result = promise.await()) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        (U::class.companionObjectInstance!! as ArithmeticConstants).zero
                    }
                }
            }
            Ok(sum)
        }
    } catch (e: CancellationException) {
        error?.let { Failed(it) }
            ?: Ok((U::class.companionObjectInstance!! as ArithmeticConstants).zero)
    }
}

suspend inline fun  Iterable.foldParallelly(
    initial: T,
    crossinline operation: suspend (acc: T, T) -> T
): T {
    return this.foldParallelly(UInt64.ten, initial, operation)
}

suspend inline fun  Iterable.foldParallelly(
    segment: UInt64,
    initial: T,
    crossinline operation: suspend (acc: T, T) -> T
): T {
    var exception: Exception? = null
    var accumulator = initial

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.fold(initial) { acc, value -> operation(acc, value) }
                })
            }

            for ((index, promise) in promises.withIndex()) {
                val result = promise.await()
                accumulator = operation(accumulator, result)
                if (index == 0 && accumulator != result) {
                    exception = IllegalArgumentException("operation is order dependent")
                    cancel()
                    return@coroutineScope accumulator
                }
            }

            accumulator
        }
    } catch (e: CancellationException) {
        exception?.let { throw it }
        accumulator
    }
}

suspend inline fun  Iterable.tryFoldParallelly(
    initial: T,
    crossinline operation: (acc: T, T) -> Ret
): Ret {
    return this.tryFoldParallelly(UInt64.ten, initial, operation)
}

suspend inline fun  Iterable.tryFoldParallelly(
    segment: UInt64,
    initial: T,
    crossinline operation: (acc: T, T) -> Ret
): Ret {
    var error: Error? = null
    var accumulator = initial

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.fold(initial) { lhs, rhs ->
                        if (error != null) {
                            lhs
                        } else {
                            when (val ret = operation(lhs, rhs)) {
                                is Ok -> {
                                    ret.value
                                }

                                is Failed -> {
                                    error = ret.error
                                    lhs
                                }
                            }
                        }
                    }
                })
            }

            for ((index, promise) in promises.withIndex()) {
                val result = promise.await()
                accumulator = when (val ret = operation(accumulator, result)) {
                    is Ok -> {
                        ret.value
                    }

                    is Failed -> {
                        error = ret.error
                        cancel()
                        return@coroutineScope Failed(error as Error)
                    }
                }
                if (index == 0 && accumulator != result) {
                    error = Err(ErrorCode.ApplicationException, "operation is order dependent")
                    cancel()
                    return@coroutineScope Failed(error as Error)
                }
            }

            Ok(accumulator)
        }
    } catch (e: CancellationException) {
        error?.let { Failed(it) }
            ?: Ok(accumulator)
    }
}

suspend inline fun  Iterable.foldIndexedParallelly(
    initial: T,
    crossinline operation: (index: Int, acc: T, T) -> T
): T {
    return this.foldIndexedParallelly(UInt64.ten, initial, operation)
}

suspend inline fun  Iterable.foldIndexedParallelly(
    segment: UInt64,
    initial: T,
    crossinline operation: (index: Int, acc: T, T) -> T
): T {
    var exception: Exception? = null
    var accumulator = initial

    return try {
        coroutineScope {
            val promises = ArrayList>>()
            val iterator = [email protected]()
            var i = 0
            while (iterator.hasNext()) {
                val thisSegment = ArrayList>()
                var j = UInt64.zero
                while (iterator.hasNext() && j != segment) {
                    thisSegment.add(i to iterator.next())
                    ++i
                    ++j
                }
                if (thisSegment.isNotEmpty()) {
                    promises.add(Pair(
                        thisSegment.first().first,
                        async(Dispatchers.Default) {
                            thisSegment.fold(initial) { lhs, rhs ->
                                operation(rhs.first, lhs, rhs.second)
                            }
                        }
                    ))
                }
            }

            for ((index, promise) in promises.withIndex()) {
                val result = promise.second.await()
                accumulator = operation(promise.first, accumulator, result)
                if (index == 0 && accumulator != result) {
                    exception = IllegalArgumentException("operation is order dependent")
                    cancel()
                    return@coroutineScope accumulator
                }
            }

            accumulator
        }
    } catch (e: CancellationException) {
        exception?.let { throw it }
        accumulator
    }
}

suspend inline fun  Iterable.tryFoldIndexedParallelly(
    initial: T,
    crossinline operation: (index: Int, acc: T, T) -> Ret
): Ret {
    return this.tryFoldIndexedParallelly(UInt64.ten, initial, operation)
}

suspend inline fun  Iterable.tryFoldIndexedParallelly(
    segment: UInt64,
    initial: T,
    crossinline operation: (index: Int, acc: T, T) -> Ret
): Ret {
    var error: Error? = null
    var accumulator = initial

    return try {
        coroutineScope {
            val promises = ArrayList>>()
            val iterator = [email protected]()
            var i = 0
            while (iterator.hasNext()) {
                val thisSegment = ArrayList>()
                var j = UInt64.zero
                while (iterator.hasNext() && j != segment) {
                    thisSegment.add(i to iterator.next())
                    ++i
                    ++j
                }
                if (thisSegment.isNotEmpty()) {
                    promises.add(Pair(
                        thisSegment.first().first,
                        async(Dispatchers.Default) {
                            thisSegment.fold(initial) { lhs, rhs ->
                                if (error != null) {
                                    lhs
                                } else {
                                    when (val ret = operation(rhs.first, lhs, rhs.second)) {
                                        is Ok -> {
                                            ret.value
                                        }

                                        is Failed -> {
                                            error = ret.error
                                            lhs
                                        }
                                    }
                                }
                            }
                        }
                    ))
                }
            }

            for ((index, promise) in promises.withIndex()) {
                val result = promise.second.await()
                accumulator = when (val ret = operation(promise.first, accumulator, result)) {
                    is Ok -> {
                        ret.value
                    }

                    is Failed -> {
                        error = ret.error
                        cancel()
                        return@coroutineScope Failed(error as Error)
                    }
                }
                if (index == 0 && accumulator != result) {
                    error = Err(ErrorCode.ApplicationException, "operation is order dependent")
                    cancel()
                    return@coroutineScope Failed(error as Error)
                }
            }

            Ok(accumulator)
        }
    } catch (e: CancellationException) {
        error?.let { Failed(it) }
            ?: Ok(accumulator)
    }
}

suspend inline fun  Iterable.foldRightParallelly(
    initial: T,
    crossinline operation: suspend (acc: T, T) -> T
): T {
    return this.foldRightParallelly(UInt64.ten, initial, operation)
}

suspend inline fun  Iterable.foldRightParallelly(
    segment: UInt64,
    initial: T,
    crossinline operation: suspend (acc: T, T) -> T
): T {
    var exception: Exception? = null
    var accumulator = initial

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]().iterator()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.fold(initial) { acc, value -> operation(acc, value) }
                })
            }

            for ((index, promise) in promises.withIndex()) {
                val result = promise.await()
                accumulator = operation(accumulator, result)
                if (index == 0 && accumulator != result) {
                    exception = IllegalArgumentException("operation is order dependent")
                    cancel()
                    return@coroutineScope accumulator
                }
            }

            accumulator
        }
    } catch (e: CancellationException) {
        exception?.let { throw it }
        accumulator
    }
}

suspend inline fun  Iterable.tryFoldRightParallelly(
    initial: T,
    crossinline operation: (acc: T, T) -> Ret
): Ret {
    return this.tryFoldRightParallelly(UInt64.ten, initial, operation)
}

suspend inline fun  Iterable.tryFoldRightParallelly(
    segment: UInt64,
    initial: T,
    crossinline operation: (acc: T, T) -> Ret
): Ret {
    var error: Error? = null
    var accumulator = initial

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]().iterator()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.fold(initial) { lhs, rhs ->
                        if (error != null) {
                            lhs
                        } else {
                            when (val ret = operation(lhs, rhs)) {
                                is Ok -> {
                                    ret.value
                                }

                                is Failed -> {
                                    error = ret.error
                                    lhs
                                }
                            }
                        }
                    }
                })
            }

            for ((index, promise) in promises.withIndex()) {
                val result = promise.await()
                accumulator = when (val ret = operation(accumulator, result)) {
                    is Ok -> {
                        ret.value
                    }

                    is Failed -> {
                        error = ret.error
                        cancel()
                        return@coroutineScope Failed(error as Error)
                    }
                }
                if (index == 0 && accumulator != result) {
                    error = Err(ErrorCode.ApplicationException, "operation is order dependent")
                    cancel()
                    return@coroutineScope Failed(error as Error)
                }
            }

            Ok(accumulator)
        }
    } catch (e: CancellationException) {
        error?.let { Failed(it) }
            ?: Ok(accumulator)
    }
}

suspend inline fun  Iterable.foldRightIndexedParallelly(
    initial: T,
    crossinline operation: (index: Int, acc: T, T) -> T
): T {
    return this.foldRightIndexedParallelly(UInt64.ten, initial, operation)
}

suspend inline fun  Iterable.foldRightIndexedParallelly(
    segment: UInt64,
    initial: T,
    crossinline operation: (index: Int, acc: T, T) -> T
): T {
    var exception: Exception? = null
    var accumulator = initial

    return try {
        coroutineScope {
            val promises = ArrayList>>()
            val iterator = [email protected]().iterator()
            var i = 0
            while (iterator.hasNext()) {
                val thisSegment = ArrayList>()
                var j = UInt64.zero
                while (iterator.hasNext() && j != segment) {
                    thisSegment.add(i to iterator.next())
                    ++i
                    ++j
                }
                if (thisSegment.isNotEmpty()) {
                    promises.add(Pair(
                        thisSegment.first().first,
                        async(Dispatchers.Default) {
                            thisSegment.fold(initial) { lhs, rhs ->
                                operation(rhs.first, lhs, rhs.second)
                            }
                        }
                    ))
                }
            }

            for ((index, promise) in promises.withIndex()) {
                val result = promise.second.await()
                accumulator = operation(promise.first, accumulator, result)
                if (index == 0 && accumulator != result) {
                    exception = IllegalArgumentException("operation is order dependent")
                    cancel()
                    return@coroutineScope accumulator
                }
            }

            accumulator
        }
    } catch (e: CancellationException) {
        exception?.let { throw it }
        accumulator
    }
}

suspend inline fun  Iterable.tryFoldRightIndexedParallelly(
    initial: T,
    crossinline operation: (index: Int, acc: T, T) -> Ret
): Ret {
    return this.tryFoldRightIndexedParallelly(UInt64.ten, initial, operation)
}

suspend inline fun  Iterable.tryFoldRightIndexedParallelly(
    segment: UInt64,
    initial: T,
    crossinline operation: (index: Int, acc: T, T) -> Ret
): Ret {
    var error: Error? = null
    var accumulator = initial

    return try {
        coroutineScope {
            val promises = ArrayList>>()
            val iterator = [email protected]().reversed().iterator()
            var i = 0
            while (iterator.hasNext()) {
                val thisSegment = ArrayList>()
                var j = UInt64.zero
                while (iterator.hasNext() && j != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                    ++j
                }
                if (thisSegment.isNotEmpty()) {
                    promises.add(Pair(
                        thisSegment.first().index,
                        async(Dispatchers.Default) {
                            thisSegment.fold(initial) { lhs, rhs ->
                                if (error != null) {
                                    lhs
                                } else {
                                    when (val ret = operation(rhs.index, lhs, rhs.value)) {
                                        is Ok -> {
                                            ret.value
                                        }

                                        is Failed -> {
                                            error = ret.error
                                            lhs
                                        }
                                    }
                                }
                            }
                        }
                    ))
                }
            }

            for ((index, promise) in promises.withIndex()) {
                val result = promise.second.await()
                accumulator = when (val ret = operation(promise.first, accumulator, result)) {
                    is Ok -> {
                        ret.value
                    }

                    is Failed -> {
                        error = ret.error
                        cancel()
                        return@coroutineScope Failed(error as Error)
                    }
                }
                if (index == 0 && accumulator != result) {
                    error = Err(ErrorCode.ApplicationException, "operation is order dependent")
                    cancel()
                    return@coroutineScope Failed(error as Error)
                }
            }

            Ok(accumulator)
        }
    } catch (e: CancellationException) {
        error?.let { Failed(it) }
            ?: Ok(accumulator)
    }
}

suspend inline fun > Iterable.maxParallelly(): T {
    return this.maxParallelly(UInt64.ten)
}

suspend inline fun > Iterable.maxParallelly(
    segment: UInt64
): T {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.max()
            })
        }

        promises.maxOf { it.await() }
    }
}

suspend inline fun > Iterable.maxOrNullParallelly(): T? {
    return this.maxOrNullParallelly(UInt64.ten)
}

suspend inline fun > Iterable.maxOrNullParallelly(
    segment: UInt64
): T? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxOrNull()
            })
        }

        promises.mapNotNull { it.await() }.maxOrNull()
    }
}

suspend inline fun > Iterable.maxByParallelly(
    crossinline extractor: SuspendExtractor
): T {
    return this.maxByParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.maxByParallelly(
    segment: UInt64,
    crossinline extractor: SuspendExtractor
): T {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { it to extractor(it) }.maxBy { it.second }
            })
        }

        promises.map { it.await() }.maxBy { it.second }.first
    }
}

suspend inline fun > Iterable.tryMaxByParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMaxByParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.tryMaxByParallelly(
    segment: UInt64,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList?>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                it to result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.maxByOrNull { it.second }
                })
            }

            promises.mapNotNull { it.await() }.maxByOrNull { it.second }?.let { Ok(it.first) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the extracting."))
        }
    } catch (e: CancellationException) {
        Failed(error!!)
    }
}

suspend inline fun > Iterable.maxByOrNullParallelly(
    crossinline extractor: SuspendExtractor
): T? {
    return this.maxByOrNullParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.maxByOrNullParallelly(
    segment: UInt64,
    crossinline extractor: SuspendExtractor
): T? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { it to extractor(it) }.maxByOrNull { it.second }
            })
        }

        promises.mapNotNull { it.await() }.maxByOrNull { it.second }?.first
    }
}

suspend inline fun > Iterable.tryMaxByOrNullParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMaxByOrNullParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.tryMaxByOrNullParallelly(
    segment: UInt64,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList?>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                it to result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.maxByOrNull { it.second }
                })
            }

            promises.mapNotNull { it.await() }.maxByOrNull { it.second }?.let { Ok(it.first) }
                ?: Ok(null)
        }
    } catch (e: CancellationException) {
        Failed(error!!)
    }
}

suspend inline fun > Iterable.maxOfParallelly(
    crossinline extractor: SuspendExtractor
): R {
    return this.maxOfParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.maxOfParallelly(
    segment: UInt64,
    crossinline extractor: SuspendExtractor
): R {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxOfOrNull { extractor(it) }
            })
        }

        promises.mapNotNull { it.await() }.max()
    }
}

suspend inline fun > Iterable.tryMaxOfParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMaxOfParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.tryMaxOfParallelly(
    segment: UInt64,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.maxOrNull()
                })
            }

            promises.mapNotNull { it.await() }.maxOrNull()?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the extracting."))
        }
    } catch (e: CancellationException) {
        Failed(error!!)
    }
}

suspend inline fun > Iterable.maxOfOrNullParallelly(
    crossinline extractor: SuspendExtractor
): R? {
    return this.maxOfOrNullParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.maxOfOrNullParallelly(
    segment: UInt64,
    crossinline extractor: SuspendExtractor
): R? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxOfOrNull { extractor(it) }
            })
        }

        promises.mapNotNull { it.await() }.maxOrNull()
    }
}

suspend inline fun > Iterable.tryMaxOfOrNullParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMaxOfOrNullParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.tryMaxOfOrNullParallelly(
    segment: UInt64,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.maxOrNull()
                })
            }

            promises.mapNotNull { it.await() }.maxOrNull()?.let { Ok(it) }
                ?: Ok(null)
        }
    } catch (e: CancellationException) {
        Failed(error!!)
    }
}

suspend inline fun  Iterable.maxWithParallelly(
    comparator: KComparator
): T {
    return this.maxWithParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.maxWithParallelly(
    segment: UInt64,
    comparator: KComparator
): T {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxWith(comparator)
            })
        }

        promises.map { it.await() }.maxWith(comparator)
    }
}

suspend inline fun  Iterable.maxWithComparatorParallelly(
    crossinline comparator: Comparator
): T {
    return this.maxWithComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.maxWithComparatorParallelly(
    segment: UInt64,
    crossinline comparator: Comparator
): T {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxWithComparator(comparator)
            })
        }

        promises.map { it.await() }.maxWithComparator(comparator)
    }
}

suspend inline fun  Iterable.maxWithPartialComparatorParallelly(
    crossinline comparator: PartialComparator
): T {
    return this.maxWithPartialComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.maxWithPartialComparatorParallelly(
    segment: UInt64,
    crossinline comparator: PartialComparator
): T {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxWithPartialComparator(comparator)
            })
        }

        promises.map { it.await() }.maxWithPartialComparator(comparator)
    }
}

suspend inline fun  Iterable.maxWithThreeWayComparatorParallelly(
    crossinline comparator: ThreeWayComparator
): T {
    return this.maxWithThreeWayComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.maxWithThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: ThreeWayComparator
): T {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxWithThreeWayComparator(comparator)
            })
        }

        promises.map { it.await() }.maxWithThreeWayComparator(comparator)
    }
}

suspend inline fun  Iterable.maxWithPartialThreeWayComparatorParallelly(
    crossinline comparator: PartialThreeWayComparator
): T {
    return this.maxWithPartialThreeWayComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.maxWithPartialThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: PartialThreeWayComparator
): T {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxWithPartialThreeWayComparator(comparator)
            })
        }

        promises.map { it.await() }.maxWithPartialThreeWayComparator(comparator)
    }
}

suspend inline fun  Iterable.tryMaxWithComparatorParallelly(
    crossinline comparator: TryComparator
): Ret {
    return this.tryMaxWithComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.tryMaxWithComparatorParallelly(
    segment: UInt64,
    crossinline comparator: TryComparator
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.maxWithComparator { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                false
                            }
                        }
                    }
                })
            }

            Ok(promises.mapNotNull { it.await() }.maxWithComparator { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        false
                    }
                }
            })
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.tryMaxWithThreeWayComparatorParallelly(
    crossinline comparator: TryThreeWayComparator
): Ret {
    return this.tryMaxWithThreeWayComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.tryMaxWithThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: TryThreeWayComparator
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.maxWithThreeWayComparator { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                Order.Equal
                            }
                        }
                    }
                })
            }

            Ok(promises.mapNotNull { it.await() }.maxWithThreeWayComparator { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        Order.Equal
                    }
                }
            })
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.maxWithOrNullParallelly(
    comparator: KComparator
): T? {
    return this.maxWithOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.maxWithOrNullParallelly(
    segment: UInt64,
    comparator: KComparator
): T? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxWithOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithOrNull(comparator)
    }
}

suspend inline fun  Iterable.maxWithComparatorOrNullParallelly(
    crossinline comparator: Comparator
): T? {
    return this.maxWithComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.maxWithComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: Comparator
): T? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxWithComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.maxWithPartialComparatorOrNullParallelly(
    crossinline comparator: PartialComparator
): T? {
    return this.maxWithPartialComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.maxWithPartialComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: PartialComparator
): T? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxWithPartialComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithPartialComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.maxWithThreeWayComparatorOrNullParallelly(
    crossinline comparator: ThreeWayComparator
): T? {
    return this.maxWithThreeWayComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.maxWithThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: ThreeWayComparator
): T? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxWithThreeWayComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithThreeWayComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.maxWithPartialThreeWayComparatorOrNullParallelly(
    crossinline comparator: PartialThreeWayComparator
): T? {
    return this.maxWithPartialThreeWayComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.maxWithPartialThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: PartialThreeWayComparator
): T? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.maxWithPartialThreeWayComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithPartialThreeWayComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.tryMaxWithComparatorOrNullParallelly(
    crossinline comparator: TryComparator
): Ret {
    return this.tryMaxWithComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.tryMaxWithComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: TryComparator
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.maxWithComparatorOrNull { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                false
                            }
                        }
                    }
                })
            }

            Ok(promises.mapNotNull { it.await() }.maxWithComparatorOrNull { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        false
                    }
                }
            })
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.tryMaxWithThreeWayComparatorOrNullParallelly(
    crossinline comparator: TryThreeWayComparator
): Ret {
    return this.tryMaxWithThreeWayComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.tryMaxWithThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: TryThreeWayComparator
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.maxWithThreeWayComparatorOrNull { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                Order.Equal
                            }
                        }
                    }
                })
            }

            Ok(promises.mapNotNull { it.await() }.maxWithThreeWayComparatorOrNull { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        Order.Equal
                    }
                }
            })
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.maxOfWithParallelly(
    comparator: KComparator,
    crossinline extractor: SuspendExtractor
): R {
    return this.maxOfWithParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.maxOfWithParallelly(
    segment: UInt64,
    comparator: KComparator,
    crossinline extractor: SuspendExtractor
): R {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.maxWith(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWith(comparator)
    }
}

suspend inline fun  Iterable.maxOfWithComparatorParallelly(
    crossinline comparator: Comparator,
    crossinline extractor: SuspendExtractor
): R {
    return this.maxOfWithComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.maxOfWithComparatorParallelly(
    segment: UInt64,
    crossinline comparator: Comparator,
    crossinline extractor: SuspendExtractor
): R {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.maxWithComparator(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithComparator(comparator)
    }
}

suspend inline fun  Iterable.maxOfWithPartialComparatorParallelly(
    crossinline comparator: PartialComparator,
    crossinline extractor: SuspendExtractor
): R {
    return this.maxOfWithPartialComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.maxOfWithPartialComparatorParallelly(
    segment: UInt64,
    crossinline comparator: PartialComparator,
    crossinline extractor: SuspendExtractor
): R {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.maxWithPartialComparator(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithPartialComparator(comparator)
    }
}

suspend inline fun  Iterable.maxOfWithThreeWayComparatorParallelly(
    crossinline comparator: ThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R {
    return this.maxOfWithThreeWayComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.maxOfWithThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: ThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.maxWithThreeWayComparator(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithThreeWayComparator(comparator)
    }
}

suspend inline fun  Iterable.maxOfWithPartialThreeWayComparatorParallelly(
    crossinline comparator: PartialThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R {
    return this.maxOfWithPartialThreeWayComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.maxOfWithPartialThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: PartialThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.maxWithPartialThreeWayComparator(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithPartialThreeWayComparator(comparator)
    }
}

suspend inline fun  Iterable.tryMaxOfWithComparatorParallelly(
    crossinline comparator: TryComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMaxOfWithComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.tryMaxOfWithComparatorParallelly(
    segment: UInt64,
    crossinline comparator: TryComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                null
                            }
                        }
                    }.maxWithComparator { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                false
                            }
                        }
                    }
                })
            }
            promises.mapNotNull { it.await() }.maxWithComparatorOrNull { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        false
                    }
                }
            }?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the extracting."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.tryMaxOfWithThreeWayComparatorParallelly(
    crossinline comparator: TryThreeWayComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMaxOfWithThreeWayComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.tryMaxOfWithThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: TryThreeWayComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                null
                            }
                        }
                    }.maxWithThreeWayComparator { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                Order.Equal
                            }
                        }
                    }
                })
            }
            promises.mapNotNull { it.await() }.maxWithThreeWayComparatorOrNull { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        Order.Equal
                    }
                }
            }?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the extracting."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.maxOfWithOrNullParallelly(
    comparator: KComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return this.maxOfWithParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.maxOfWithOrNullParallelly(
    segment: UInt64,
    comparator: KComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.maxWithOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithOrNull(comparator)
    }
}

suspend inline fun  Iterable.maxOfWithComparatorOrNullParallelly(
    crossinline comparator: Comparator,
    crossinline extractor: SuspendExtractor
): R? {
    return this.maxOfWithComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.maxOfWithComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: Comparator,
    crossinline extractor: SuspendExtractor
): R? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.maxWithComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.maxOfWithPartialComparatorOrNullParallelly(
    crossinline comparator: PartialComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return this.maxOfWithPartialComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.maxOfWithPartialComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: PartialComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.maxWithPartialComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithPartialComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.maxOfWithThreeWayComparatorOrNullParallelly(
    crossinline comparator: ThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return this.maxOfWithThreeWayComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.maxOfWithThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: ThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.maxWithThreeWayComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithThreeWayComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.maxOfWithPartialThreeWayComparatorOrNullParallelly(
    crossinline comparator: PartialThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return this.maxOfWithPartialThreeWayComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.maxOfWithPartialThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: PartialThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.maxWithPartialThreeWayComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.maxWithPartialThreeWayComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.tryMaxOfWithComparatorOrNullParallelly(
    crossinline comparator: TryComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMaxOfWithComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.tryMaxOfWithComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: TryComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                null
                            }
                        }
                    }.maxWithComparatorOrNull { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                false
                            }
                        }
                    }
                })
            }
            promises.mapNotNull { it.await() }.maxWithComparatorOrNull { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        false
                    }
                }
            }?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the extracting."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.tryMaxOfWithThreeWayComparatorOrNullParallelly(
    crossinline comparator: TryThreeWayComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMaxOfWithThreeWayComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.tryMaxOfWithThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: TryThreeWayComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                null
                            }
                        }
                    }.maxWithThreeWayComparatorOrNull { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                Order.Equal
                            }
                        }
                    }
                })
            }
            promises.mapNotNull { it.await() }.maxWithThreeWayComparatorOrNull { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        Order.Equal
                    }
                }
            }?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the extracting."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun > Iterable.minParallelly(): T {
    return this.minParallelly(UInt64.ten)
}

suspend inline fun > Iterable.minParallelly(
    segment: UInt64
): T {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.min()
            })
        }

        promises.minOfOrNull { it.await() }!!
    }
}

suspend inline fun > Iterable.minOrNullParallelly(): T? {
    return minOrNullParallelly(UInt64.ten)
}

suspend inline fun > Iterable.minOrNullParallelly(
    segment: UInt64
): T? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minOrNull()
            })
        }

        promises.mapNotNull { it.await() }.minOrNull()
    }
}

suspend inline fun > Iterable.minByParallelly(
    crossinline extractor: Extractor
): T {
    return this.minByParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.minByParallelly(
    segment: UInt64,
    crossinline extractor: Extractor
): T {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { it to extractor(it) }.minBy { it.second }
            })
        }

        promises.map { it.await() }.minBy { it.second }.first
    }
}

suspend inline fun > Iterable.tryMinByParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMinByParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.tryMinByParallelly(
    segment: UInt64,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList?>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                it to result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minByOrNull { it.second }
                })
            }

            promises.mapNotNull { it.await() }.minByOrNull { it.second }?.let { Ok(it.first) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the extracting."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun > Iterable.minByOrNullParallelly(
    crossinline extractor: Extractor
): T? {
    return this.minByOrNullParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.minByOrNullParallelly(
    segment: UInt64,
    crossinline extractor: Extractor
): T? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { it to extractor(it) }.minByOrNull { it.second }
            })
        }

        promises.mapNotNull { it.await() }.minByOrNull { it.second }?.first
    }
}

suspend inline fun > Iterable.tryMinByOrNullParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMinByOrNullParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.tryMinByOrNullParallelly(
    segment: UInt64,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList?>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                it to result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minByOrNull { it.second }
                })
            }

            promises.mapNotNull { it.await() }.minByOrNull { it.second }?.let { Ok(it.first) }
                ?: Ok(null)
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun > Iterable.minOfParallelly(
    crossinline extractor: Extractor
): R {
    return this.minOfParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.minOfParallelly(
    segment: UInt64,
    crossinline extractor: Extractor
): R {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minOf { extractor(it) }
            })
        }

        promises.minOf { it.await() }
    }
}

suspend inline fun > Iterable.tryMinOfParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret {
    return tryMinOfParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.tryMinOfParallelly(
    segment: UInt64,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minOrNull()
                })
            }

            promises.mapNotNull { it.await() }.minOrNull()?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the extracting."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun > Iterable.minOfOrNullParallelly(
    crossinline extractor: Extractor
): R? {
    return this.minOfOrNullParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.minOfOrNullParallelly(
    segment: UInt64,
    crossinline extractor: Extractor
): R? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minOfOrNull { extractor(it) }
            })
        }

        promises.mapNotNull { it.await() }.minOfOrNull { it }
    }
}

suspend inline fun > Iterable.tryMinOfOrNullParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret {
    return tryMinOfOrNullParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.tryMinOfOrNullParallelly(
    segment: UInt64,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                null
                            }
                        }
                    }.minOrNull()
                })
            }

            promises.mapNotNull { it.await() }.minOrNull()?.let { Ok(it) }
                ?: Ok(null)
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.minWithParallelly(
    comparator: KComparator
): T {
    return this.minWithParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minWithParallelly(
    segment: UInt64,
    comparator: KComparator
): T {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minWith(comparator)
            })
        }

        promises.map { it.await() }.minWith(comparator)
    }
}

suspend inline fun  Iterable.minWithComparatorParallelly(
    crossinline comparator: Comparator
): T {
    return this.minWithComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minWithComparatorParallelly(
    segment: UInt64,
    crossinline comparator: Comparator
): T {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minWithComparator(comparator)
            })
        }

        promises.map { it.await() }.minWithComparator(comparator)
    }
}

suspend inline fun  Iterable.minWithPartialComparatorParallelly(
    crossinline comparator: PartialComparator
): T {
    return this.minWithPartialComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minWithPartialComparatorParallelly(
    segment: UInt64,
    crossinline comparator: PartialComparator
): T {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minWithPartialComparator(comparator)
            })
        }

        promises.map { it.await() }.minWithPartialComparator(comparator)
    }
}

suspend inline fun  Iterable.minWithThreeWayComparatorParallelly(
    crossinline comparator: ThreeWayComparator
): T {
    return this.minWithThreeWayComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minWithThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: ThreeWayComparator
): T {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minWithThreeWayComparator(comparator)
            })
        }

        promises.map { it.await() }.minWithThreeWayComparator(comparator)
    }
}

suspend inline fun  Iterable.minWithPartialThreeWayComparatorParallelly(
    crossinline comparator: PartialThreeWayComparator
): T {
    return this.minWithPartialThreeWayComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minWithPartialThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: PartialThreeWayComparator
): T {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minWithPartialThreeWayComparator(comparator)
            })
        }

        promises.map { it.await() }.minWithPartialThreeWayComparator(comparator)
    }
}

suspend inline fun  Iterable.tryMinWithComparatorParallelly(
    crossinline comparator: TryComparator
): Ret {
    return this.tryMinWithComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.tryMinWithComparatorParallelly(
    segment: UInt64,
    crossinline comparator: TryComparator
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.minWithComparator { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                false
                            }
                        }
                    }
                })
            }

            promises.mapNotNull { it.await() }.minWithComparatorOrNull { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        false
                    }
                }
            }?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.tryMinWithThreeWayComparatorParallelly(
    crossinline comparator: TryThreeWayComparator
): Ret {
    return this.tryMinWithThreeWayComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.tryMinWithThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: TryThreeWayComparator
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.minWithThreeWayComparator { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                Order.Equal
                            }
                        }
                    }
                })
            }

            promises.mapNotNull { it.await() }.minWithThreeWayComparatorOrNull { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        Order.Equal
                    }
                }
            }?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.minWithOrNullParallelly(
    comparator: KComparator
): T? {
    return this.minWithOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minWithOrNullParallelly(
    segment: UInt64,
    comparator: KComparator
): T? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minWithOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithOrNull(comparator)
    }
}

suspend inline fun  Iterable.minWithComparatorOrNullParallelly(
    crossinline comparator: Comparator
): T? {
    return this.minWithComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minWithComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: Comparator
): T? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minWithComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.minWithPartialComparatorOrNullParallelly(
    crossinline comparator: PartialComparator
): T? {
    return this.minWithPartialComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minWithPartialComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: PartialComparator
): T? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minWithPartialComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithPartialComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.minWithThreeWayComparatorOrNullParallelly(
    crossinline comparator: ThreeWayComparator
): T? {
    return this.minWithThreeWayComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minWithThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: ThreeWayComparator
): T? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minWithThreeWayComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithThreeWayComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.minWithPartialThreeWayComparatorOrNullParallelly(
    crossinline comparator: PartialThreeWayComparator
): T? {
    return this.minWithPartialThreeWayComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minWithPartialThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: PartialThreeWayComparator
): T? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minWithPartialThreeWayComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithPartialThreeWayComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.tryMinWithComparatorOrNullParallelly(
    crossinline comparator: TryComparator
): Ret {
    return this.tryMinWithComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.tryMinWithComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: TryComparator
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.minWithComparatorOrNull { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                false
                            }
                        }
                    }
                })
            }

            promises.mapNotNull { it.await() }.minWithComparatorOrNull { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        false
                    }
                }
            }?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.tryMinWithThreeWayComparatorOrNullParallelly(
    crossinline comparator: TryThreeWayComparator
): Ret {
    return this.tryMinWithThreeWayComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.tryMinWithThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: TryThreeWayComparator
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.minWithThreeWayComparatorOrNull { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                Order.Equal
                            }
                        }
                    }
                })
            }

            promises.mapNotNull { it.await() }.minWithThreeWayComparatorOrNull { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        Order.Equal
                    }
                }
            }?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.minOfWithParallelly(
    comparator: KComparator,
    crossinline extractor: SuspendExtractor
): R {
    return this.minOfWithParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minOfWithParallelly(
    segment: UInt64,
    comparator: KComparator,
    crossinline extractor: SuspendExtractor
): R {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minWith(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWith(comparator)
    }
}

suspend inline fun  Iterable.minOfWithComparatorParallelly(
    crossinline comparator: Comparator,
    crossinline extractor: SuspendExtractor
): R {
    return this.minOfWithComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minOfWithComparatorParallelly(
    segment: UInt64,
    crossinline comparator: Comparator,
    crossinline extractor: SuspendExtractor
): R {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minWithComparator(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithComparator(comparator)
    }
}

suspend inline fun  Iterable.minOfWithPartialComparatorParallelly(
    crossinline comparator: PartialComparator,
    crossinline extractor: SuspendExtractor
): R {
    return this.minOfWithPartialComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minOfWithPartialComparatorParallelly(
    segment: UInt64,
    crossinline comparator: PartialComparator,
    crossinline extractor: SuspendExtractor
): R {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minWithPartialComparator(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithPartialComparator(comparator)
    }
}

suspend inline fun  Iterable.minOfWithThreeWayComparatorParallelly(
    crossinline comparator: ThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R {
    return this.minOfWithThreeWayComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minOfWithThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: ThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minWithThreeWayComparator(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithThreeWayComparator(comparator)
    }
}

suspend inline fun  Iterable.minOfWithPartialThreeWayComparatorParallelly(
    crossinline comparator: PartialThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R {
    return this.minOfWithPartialThreeWayComparatorParallelly(UInt64.ten, comparator, extractor)
}


suspend inline fun  Iterable.minOfWithPartialThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: PartialThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minWithPartialThreeWayComparator(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithPartialThreeWayComparator(comparator)
    }
}

suspend inline fun  Iterable.tryMinOfWithComparatorParallelly(
    crossinline comparator: TryComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMinOfWithComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.tryMinOfWithComparatorParallelly(
    segment: UInt64,
    crossinline comparator: TryComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minWithComparator { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                false
                            }
                        }
                    }
                })
            }

            promises.mapNotNull { it.await() }.minWithComparatorOrNull() { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        false
                    }
                }
            }?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.tryMinOfWithThreeWayComparatorParallelly(
    crossinline comparator: TryThreeWayComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMinOfWithThreeWayComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.tryMinOfWithThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: TryThreeWayComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minWithThreeWayComparator { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                Order.Equal
                            }
                        }
                    }
                })
            }

            promises.mapNotNull { it.await() }.minWithThreeWayComparatorOrNull() { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        Order.Equal
                    }
                }
            }?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.minOfWithComparatorOrNullParallelly(
    comparator: KComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return this.minOfWithComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minOfWithComparatorOrNullParallelly(
    segment: UInt64,
    comparator: KComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minWithOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithOrNull(comparator)
    }
}

suspend inline fun  Iterable.minOfWithComparatorOrNullParallelly(
    crossinline comparator: Comparator,
    crossinline extractor: SuspendExtractor
): R? {
    return this.minOfWithComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minOfWithComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: Comparator,
    crossinline extractor: SuspendExtractor
): R? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minWithComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.minOfWithPartialComparatorOrNullParallelly(
    crossinline comparator: PartialComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return this.minOfWithPartialComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minOfWithPartialComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: PartialComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minWithPartialComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithPartialComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.minOfWithThreeWayComparatorOrNullParallelly(
    crossinline comparator: ThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return this.minOfWithThreeWayComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minOfWithThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: ThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minWithThreeWayComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithThreeWayComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.minOfWithPartialThreeWayComparatorOrNullParallelly(
    crossinline comparator: PartialThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return this.minOfWithPartialThreeWayComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minOfWithPartialThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: PartialThreeWayComparator,
    crossinline extractor: SuspendExtractor
): R? {
    return coroutineScope {
        val promises = ArrayList>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minWithPartialThreeWayComparatorOrNull(comparator)
            })
        }

        promises.mapNotNull { it.await() }.minWithPartialThreeWayComparatorOrNull(comparator)
    }
}

suspend inline fun  Iterable.tryMinOfWithComparatorOrNullParallelly(
    crossinline comparator: TryComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMinOfWithComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.tryMinOfWithComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: TryComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minWithComparatorOrNull { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                false
                            }
                        }
                    }
                })
            }

            promises.mapNotNull { it.await() }.minWithComparatorOrNull { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        false
                    }
                }
            }?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.tryMinOfWithThreeWayComparatorOrNullParallelly(
    crossinline comparator: TryThreeWayComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    return this.tryMinOfWithThreeWayComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.tryMinOfWithThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: TryThreeWayComparator,
    crossinline extractor: SuspendTryExtractor
): Ret {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minWithThreeWayComparatorOrNull { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                Order.Equal
                            }
                        }
                    }
                })
            }

            promises.mapNotNull { it.await() }.minWithThreeWayComparatorOrNull { lhs, rhs ->
                when (val result = comparator(lhs, rhs)) {
                    is Ok -> {
                        result.value
                    }

                    is Failed -> {
                        error = result.error
                        cancel()
                        Order.Equal
                    }
                }
            }?.let { Ok(it) }
                ?: Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun > Iterable.minMaxParallelly(): Pair {
    return minMaxParallelly(UInt64.ten)
}

suspend inline fun > Iterable.minMaxParallelly(
    segment: UInt64
): Pair {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minMax()
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minBy { it.first } }
        val maxPromise = async { segmentResults.maxBy { it.second } }
        Pair(minPromise.await().first, maxPromise.await().second)
    }
}

suspend inline fun > Iterable.minMaxOrNullParallelly(): Pair? {
    return minMaxOrNullParallelly(UInt64.ten)
}

suspend inline fun > Iterable.minMaxOrNullParallelly(
    segment: UInt64
): Pair? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minMaxOrNull()
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minBy { it.first } }
        val maxPromise = async { segmentResults.maxBy { it.second } }
        Pair(minPromise.await().first, maxPromise.await().second)
    }
}

suspend inline fun > Iterable.minMaxByParallelly(
    crossinline extractor: SuspendExtractor
): Pair {
    return minMaxByParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.minMaxByParallelly(
    segment: UInt64,
    crossinline extractor: SuspendExtractor
): Pair {
    return coroutineScope {
        val promises = ArrayList, Pair>>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { it to extractor(it) }.minMaxBy { it.second }
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minBy { it.first.second } }
        val maxPromise = async { segmentResults.maxBy { it.second.second } }
        Pair(minPromise.await().first.first, maxPromise.await().second.first)
    }
}

suspend inline fun > Iterable.tryMinMaxByParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret> {
    return tryMinMaxByParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.tryMinMaxByParallelly(
    segment: UInt64,
    crossinline extractor: SuspendTryExtractor
): Ret> {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList, Pair>>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                it to result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minMaxBy { it.second }
                })
            }

            val segmentResults = promises.map { it.await() }
            if (segmentResults.isEmpty()) {
                return@coroutineScope Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the extractor."))
            }

            val minPromise = async { segmentResults.minBy { it.first.second } }
            val maxPromise = async { segmentResults.maxBy { it.second.second } }
            Ok(Pair(minPromise.await().first.first, maxPromise.await().second.first))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun > Iterable.minMaxByOrNullParallelly(
    crossinline extractor: SuspendExtractor
): Pair? {
    return minMaxByOrNullParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.minMaxByOrNullParallelly(
    segment: UInt64,
    crossinline extractor: SuspendExtractor
): Pair? {
    return coroutineScope {
        val promises = ArrayList, Pair>?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { it to extractor(it) }.minMaxByOrNull { it.second }
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minBy { it.first.second } }
        val maxPromise = async { segmentResults.maxBy { it.second.second } }
        Pair(minPromise.await().first.first, maxPromise.await().second.first)
    }
}

suspend inline fun > Iterable.tryMinMaxByOrNullParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret?> {
    return tryMinMaxByOrNullParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.tryMinMaxByOrNullParallelly(
    segment: UInt64,
    crossinline extractor: SuspendTryExtractor
): Ret?> {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList, Pair>?>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                it to result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minMaxByOrNull { it.second }
                })
            }

            val segmentResults = promises.mapNotNull { it.await() }
            if (segmentResults.isEmpty()) {
                return@coroutineScope Ok(null)
            }

            val minPromise = async { segmentResults.minBy { it.first.second } }
            val maxPromise = async { segmentResults.maxBy { it.second.second } }
            Ok(Pair(minPromise.await().first.first, maxPromise.await().second.first))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun > Iterable.minMaxOfParallelly(
    crossinline extractor: SuspendExtractor
): Pair {
    return minMaxOfParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.minMaxOfParallelly(
    segment: UInt64,
    crossinline extractor: SuspendExtractor
): Pair {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minMax()
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minBy { it.first } }
        val maxPromise = async { segmentResults.maxBy { it.second } }
        Pair(minPromise.await().first, maxPromise.await().second)
    }
}

suspend inline fun > Iterable.tryMinMaxOfParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret> {
    return tryMinMaxOfParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.tryMinMaxOfParallelly(
    segment: UInt64,
    crossinline extractor: SuspendTryExtractor
): Ret> {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minMax()
                })
            }

            val segmentResults = promises.map { it.await() }
            if (segmentResults.isEmpty()) {
                return@coroutineScope Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the extractor."))
            }

            val minPromise = async { segmentResults.minBy { it.first } }
            val maxPromise = async { segmentResults.maxBy { it.second } }
            Ok(Pair(minPromise.await().first, maxPromise.await().second))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun > Iterable.minMaxOfOrNullParallelly(
    crossinline extractor: SuspendExtractor
): Pair? {
    return minMaxOfOrNullParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.minMaxOfOrNullParallelly(
    segment: UInt64,
    crossinline extractor: SuspendExtractor
): Pair? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minMaxOrNull()
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minBy { it.first } }
        val maxPromise = async { segmentResults.maxBy { it.second } }
        Pair(minPromise.await().first, maxPromise.await().second)
    }
}

suspend inline fun > Iterable.tryMinMaxOfOrNullParallelly(
    crossinline extractor: SuspendTryExtractor
): Ret?> {
    return tryMinMaxOfOrNullParallelly(UInt64.ten, extractor)
}

suspend inline fun > Iterable.tryMinMaxOfOrNullParallelly(
    segment: UInt64,
    crossinline extractor: SuspendTryExtractor
): Ret?> {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList?>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minMaxOrNull()
                })
            }

            val segmentResults = promises.mapNotNull { it.await() }
            if (segmentResults.isEmpty()) {
                return@coroutineScope Ok(null)
            }

            val minPromise = async { segmentResults.minBy { it.first } }
            val maxPromise = async { segmentResults.maxBy { it.second } }
            Ok(Pair(minPromise.await().first, maxPromise.await().second))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.minMaxWithParallelly(
    comparator: KComparator
): Pair {
    return minMaxWithParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minMaxWithParallelly(
    segment: UInt64,
    comparator: KComparator
): Pair {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minMaxWith(comparator)
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minOfWith(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWith(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxWithComparatorParallelly(
    crossinline comparator: Comparator
): Pair {
    return minMaxWithComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minMaxWithComparatorParallelly(
    segment: UInt64,
    crossinline comparator: Comparator
): Pair {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minMaxWithComparator(comparator)
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minOfWithComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxWithPartialComparatorParallelly(
    crossinline comparator: PartialComparator
): Pair {
    return minMaxWithPartialComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minMaxWithPartialComparatorParallelly(
    segment: UInt64,
    crossinline comparator: PartialComparator
): Pair {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minMaxWithPartialComparator(comparator)
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minOfWithPartialComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithPartialComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxWithThreeWayComparatorParallelly(
    crossinline comparator: ThreeWayComparator
): Pair {
    return minMaxWithThreeWayComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minMaxWithThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: ThreeWayComparator
): Pair {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minMaxWithThreeWayComparator(comparator)
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minOfWithThreeWayComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithThreeWayComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxWithPartialThreeWayComparatorParallelly(
    crossinline comparator: PartialThreeWayComparator
): Pair {
    return minMaxWithPartialThreeWayComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minMaxWithPartialThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: PartialThreeWayComparator
): Pair {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minMaxWithPartialThreeWayComparator(comparator)
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minOfWithPartialThreeWayComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithPartialThreeWayComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.tryMinMaxWithComparatorParallelly(
    crossinline comparator: TryComparator
): Ret> {
    return tryMinMaxWithComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.tryMinMaxWithComparatorParallelly(
    segment: UInt64,
    crossinline comparator: TryComparator
): Ret> {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.minMaxWithComparator { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                false
                            }
                        }
                    }
                })
            }

            val segmentResults = promises.map { it.await() }
            if (segmentResults.isEmpty()) {
                return@coroutineScope Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
            }

            val minPromise = async {
                segmentResults.minOfWithComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            false
                        }
                    }
                }) { it.first }
            }
            val maxPromise = async {
                segmentResults.maxOfWithComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            false
                        }
                    }
                }) { it.second }
            }
            Ok(Pair(minPromise.await(), maxPromise.await()))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.tryMinMaxWithThreeWayComparatorParallelly(
    crossinline comparator: TryThreeWayComparator
): Ret> {
    return tryMinMaxWithThreeWayComparatorParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.tryMinMaxWithThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: TryThreeWayComparator
): Ret> {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.minMaxWithThreeWayComparator { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                Order.Equal
                            }
                        }
                    }
                })
            }

            val segmentResults = promises.map { it.await() }
            if (segmentResults.isEmpty()) {
                return@coroutineScope Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
            }

            val minPromise = async {
                segmentResults.minOfWithThreeWayComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            Order.Equal
                        }
                    }
                }) { it.first }
            }
            val maxPromise = async {
                segmentResults.maxOfWithThreeWayComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            Order.Equal
                        }
                    }
                }) { it.second }
            }
            Ok(Pair(minPromise.await(), maxPromise.await()))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.minMaxWithOrNullParallelly(
    comparator: KComparator
): Pair? {
    return minMaxWithOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minMaxWithOrNullParallelly(
    segment: UInt64,
    comparator: KComparator
): Pair? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minMaxWithOrNull(comparator)
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minOfWith(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWith(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxWithComparatorOrNullParallelly(
    crossinline comparator: Comparator
): Pair? {
    return minMaxWithComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minMaxWithComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: Comparator
): Pair? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minMaxWithComparatorOrNull(comparator)
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minOfWithComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxWithPartialComparatorOrNullParallelly(
    crossinline comparator: PartialComparator
): Pair? {
    return minMaxWithPartialComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minMaxWithPartialComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: PartialComparator
): Pair? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minMaxWithPartialComparatorOrNull(comparator)
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minOfWithPartialComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithPartialComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxWithThreeWayComparatorOrNullParallelly(
    crossinline comparator: ThreeWayComparator
): Pair? {
    return minMaxWithThreeWayComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minMaxWithThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: ThreeWayComparator
): Pair? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minMaxWithThreeWayComparatorOrNull(comparator)
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minOfWithThreeWayComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithThreeWayComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxWithPartialThreeWayComparatorOrNullParallelly(
    crossinline comparator: PartialThreeWayComparator
): Pair? {
    return minMaxWithPartialThreeWayComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.minMaxWithPartialThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: PartialThreeWayComparator
): Pair? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = this@minMaxWithPartialThreeWayComparatorOrNullParallelly.iterator()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.minMaxWithPartialThreeWayComparatorOrNull(comparator)
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minOfWithPartialThreeWayComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithPartialThreeWayComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.tryMinMaxWithComparatorOrNullParallelly(
    crossinline comparator: TryComparator
): Ret?> {
    return tryMinMaxWithComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.tryMinMaxWithComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: TryComparator
): Ret?> {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList?>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.minMaxWithComparatorOrNull { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                false
                            }
                        }
                    }
                })
            }

            val segmentResults = promises.mapNotNull { it.await() }
            if (segmentResults.isEmpty()) {
                return@coroutineScope Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
            }

            val minPromise = async {
                segmentResults.minOfWithComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            false
                        }
                    }
                }) { it.first }
            }
            val maxPromise = async {
                segmentResults.maxOfWithComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            false
                        }
                    }
                }) { it.second }
            }
            Ok(Pair(minPromise.await(), maxPromise.await()))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.tryMinMaxWithThreeWayComparatorOrNullParallelly(
    crossinline comparator: TryThreeWayComparator
): Ret?> {
    return tryMinMaxWithThreeWayComparatorOrNullParallelly(UInt64.ten, comparator)
}

suspend inline fun  Iterable.tryMinMaxWithThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: TryThreeWayComparator
): Ret?> {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList?>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.minMaxWithThreeWayComparatorOrNull { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                Order.Equal
                            }
                        }
                    }
                })
            }

            val segmentResults = promises.mapNotNull { it.await() }
            if (segmentResults.isEmpty()) {
                return@coroutineScope Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
            }

            val minPromise = async {
                segmentResults.minOfWithThreeWayComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            Order.Equal
                        }
                    }
                }) { it.first }
            }
            val maxPromise = async {
                segmentResults.maxOfWithThreeWayComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            Order.Equal
                        }
                    }
                }) { it.second }
            }
            Ok(Pair(minPromise.await(), maxPromise.await()))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.minMaxOfWithParallelly(
    comparator: KComparator,
    crossinline extractor: SuspendExtractor
): Pair {
    return minMaxOfWithParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minMaxOfWithParallelly(
    segment: UInt64,
    comparator: KComparator,
    crossinline extractor: SuspendExtractor
): Pair {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minMaxWith(comparator)
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minOfWith(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWith(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxOfWithComparatorParallelly(
    crossinline comparator: Comparator,
    crossinline extractor: SuspendExtractor
): Pair {
    return minMaxOfWithComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minMaxOfWithComparatorParallelly(
    segment: UInt64,
    crossinline comparator: Comparator,
    crossinline extractor: SuspendExtractor
): Pair {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minMaxWithComparator(comparator)
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minOfWithComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxOfWithPartialComparatorParallelly(
    crossinline comparator: PartialComparator,
    crossinline extractor: SuspendExtractor
): Pair {
    return minMaxOfWithPartialComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minMaxOfWithPartialComparatorParallelly(
    segment: UInt64,
    crossinline comparator: PartialComparator,
    crossinline extractor: SuspendExtractor
): Pair {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minMaxWithPartialComparator(comparator)
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minOfWithPartialComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithPartialComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxOfWithThreeWayComparatorParallelly(
    crossinline comparator: ThreeWayComparator,
    crossinline extractor: SuspendExtractor
): Pair {
    return minMaxOfWithThreeWayComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minMaxOfWithThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: ThreeWayComparator,
    crossinline extractor: SuspendExtractor
): Pair {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minMaxWithThreeWayComparator(comparator)
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minOfWithThreeWayComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithThreeWayComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxOfWithPartialThreeWayComparatorParallelly(
    crossinline comparator: PartialThreeWayComparator,
    crossinline extractor: SuspendExtractor
): Pair {
    return minMaxOfWithPartialThreeWayComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minMaxOfWithPartialThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: PartialThreeWayComparator,
    crossinline extractor: SuspendExtractor
): Pair {
    return coroutineScope {
        val promises = ArrayList>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minMaxWithPartialThreeWayComparator(comparator)
            })
        }

        val segmentResults = promises.map { it.await() }
        val minPromise = async { segmentResults.minOfWithPartialThreeWayComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithPartialThreeWayComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.tryMinMaxOfWithComparatorParallelly(
    crossinline comparator: TryComparator,
    crossinline extractor: SuspendTryExtractor
): Ret> {
    return tryMinMaxOfWithComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.tryMinMaxOfWithComparatorParallelly(
    segment: UInt64,
    crossinline comparator: TryComparator,
    crossinline extractor: SuspendTryExtractor
): Ret> {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList?>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minMaxWithComparator { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                false
                            }
                        }
                    }
                })
            }

            val segmentResults = promises.mapNotNull { it.await() }
            if (segmentResults.isEmpty()) {
                return@coroutineScope Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
            }

            val minPromise = async {
                segmentResults.minOfWithComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            false
                        }
                    }
                }) { it.first }
            }
            val maxPromise = async {
                segmentResults.maxOfWithComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            false
                        }
                    }
                }) { it.second }
            }
            Ok(Pair(minPromise.await(), maxPromise.await()))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.tryMinMaxOfWithThreeWayComparatorParallelly(
    crossinline comparator: TryThreeWayComparator,
    crossinline extractor: SuspendTryExtractor
): Ret> {
    return tryMinMaxOfWithThreeWayComparatorParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.tryMinMaxOfWithThreeWayComparatorParallelly(
    segment: UInt64,
    crossinline comparator: TryThreeWayComparator,
    crossinline extractor: SuspendTryExtractor
): Ret> {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList?>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minMaxWithThreeWayComparator { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                Order.Equal
                            }
                        }
                    }
                })
            }

            val segmentResults = promises.mapNotNull { it.await() }
            if (segmentResults.isEmpty()) {
                return@coroutineScope Failed(Err(ErrorCode.ApplicationException, "Collection contains no element matching the comparator."))
            }

            val minPromise = async {
                segmentResults.minOfWithThreeWayComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            Order.Equal
                        }
                    }
                }) { it.first }
            }
            val maxPromise = async {
                segmentResults.maxOfWithThreeWayComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            Order.Equal
                        }
                    }
                }) { it.second }
            }
            Ok(Pair(minPromise.await(), maxPromise.await()))
        }
    } catch (e: CancellationException) {
        return Failed(error!!)
    }
}

suspend inline fun  Iterable.minMaxOfWithOrNullParallelly(
    comparator: KComparator,
    crossinline extractor: SuspendExtractor
): Pair? {
    return minMaxOfWithOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minMaxOfWithOrNullParallelly(
    segment: UInt64,
    comparator: KComparator,
    crossinline extractor: SuspendExtractor
): Pair? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minMaxWithOrNull(comparator)
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minOfWith(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWith(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxOfWithComparatorOrNullParallelly(
    crossinline comparator: Comparator,
    crossinline extractor: SuspendExtractor
): Pair? {
    return minMaxOfWithComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minMaxOfWithComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: Comparator,
    crossinline extractor: SuspendExtractor
): Pair? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minMaxWithComparatorOrNull(comparator)
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minOfWithComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxOfWithPartialComparatorOrNullParallelly(
    crossinline comparator: PartialComparator,
    crossinline extractor: SuspendExtractor
): Pair? {
    return minMaxOfWithPartialComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minMaxOfWithPartialComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: PartialComparator,
    crossinline extractor: SuspendExtractor
): Pair? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minMaxWithPartialComparatorOrNull(comparator)
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minOfWithPartialComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithPartialComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxOfWithThreeWayComparatorOrNullParallelly(
    crossinline comparator: ThreeWayComparator,
    crossinline extractor: SuspendExtractor
): Pair? {
    return minMaxOfWithThreeWayComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minMaxOfWithThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: ThreeWayComparator,
    crossinline extractor: SuspendExtractor
): Pair? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = [email protected]()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minMaxWithThreeWayComparatorOrNull(comparator)
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minOfWithThreeWayComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithThreeWayComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.minMaxOfWithPartialThreeWayComparatorOrNullParallelly(
    crossinline comparator: PartialThreeWayComparator,
    crossinline extractor: SuspendExtractor
): Pair? {
    return minMaxOfWithPartialThreeWayComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.minMaxOfWithPartialThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: PartialThreeWayComparator,
    crossinline extractor: SuspendExtractor
): Pair? {
    return coroutineScope {
        val promises = ArrayList?>>()
        val iterator = this@minMaxOfWithPartialThreeWayComparatorOrNullParallelly.iterator()
        while (iterator.hasNext()) {
            val thisSegment = ArrayList()
            var i = UInt64.zero
            while (iterator.hasNext() && i != segment) {
                thisSegment.add(iterator.next())
                ++i
            }
            promises.add(async(Dispatchers.Default) {
                thisSegment.map { extractor(it) }.minMaxWithPartialThreeWayComparatorOrNull(comparator)
            })
        }

        val segmentResults = promises.mapNotNull { it.await() }
        if (segmentResults.isEmpty()) {
            return@coroutineScope null
        }

        val minPromise = async { segmentResults.minOfWithPartialThreeWayComparator(comparator) { it.first } }
        val maxPromise = async { segmentResults.maxOfWithPartialThreeWayComparator(comparator) { it.second } }
        Pair(minPromise.await(), maxPromise.await())
    }
}

suspend inline fun  Iterable.tryMinMaxOfWithComparatorOrNullParallelly(
    crossinline comparator: TryComparator,
    crossinline extractor: SuspendTryExtractor
): Ret?> {
    return tryMinMaxOfWithComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.tryMinMaxOfWithComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: TryComparator,
    crossinline extractor: SuspendTryExtractor
): Ret?> {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList?>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minMaxWithComparatorOrNull { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                false
                            }
                        }
                    }
                })
            }

            val segmentResults = promises.mapNotNull { it.await() }
            val maxPromise = async {
                segmentResults.minOfWithComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            false
                        }
                    }
                }) { it.first }
            }
            val minPromise = async {
                segmentResults.maxOfWithComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            false
                        }
                    }
                }) { it.second }
            }
            Ok(Pair(minPromise.await(), maxPromise.await()))
        }
    } catch (e: CancellationException) {
        Failed(error!!)
    }
}

suspend inline fun  Iterable.tryMinMaxOfWithThreeWayComparatorOrNullParallelly(
    crossinline comparator: TryThreeWayComparator,
    crossinline extractor: SuspendTryExtractor
): Ret?> {
    return tryMinMaxOfWithThreeWayComparatorOrNullParallelly(UInt64.ten, comparator, extractor)
}

suspend inline fun  Iterable.tryMinMaxOfWithThreeWayComparatorOrNullParallelly(
    segment: UInt64,
    crossinline comparator: TryThreeWayComparator,
    crossinline extractor: SuspendTryExtractor
): Ret?> {
    var error: Error? = null

    return try {
        coroutineScope {
            val promises = ArrayList?>>()
            val iterator = [email protected]()
            while (iterator.hasNext()) {
                val thisSegment = ArrayList()
                var i = UInt64.zero
                while (iterator.hasNext() && i != segment) {
                    thisSegment.add(iterator.next())
                    ++i
                }
                promises.add(async(Dispatchers.Default) {
                    thisSegment.mapNotNull {
                        when (val result = extractor(it)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                null
                            }
                        }
                    }.minMaxWithThreeWayComparatorOrNull { lhs, rhs ->
                        when (val result = comparator(lhs, rhs)) {
                            is Ok -> {
                                result.value
                            }

                            is Failed -> {
                                error = result.error
                                cancel()
                                Order.Equal
                            }
                        }
                    }
                })
            }

            val segmentResults = promises.mapNotNull { it.await() }
            val maxPromise = async {
                segmentResults.minOfWithThreeWayComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            Order.Equal
                        }
                    }
                }) { it.first }
            }
            val minPromise = async {
                segmentResults.maxOfWithThreeWayComparator({ lhs, rhs ->
                    when (val result = comparator(lhs, rhs)) {
                        is Ok -> {
                            result.value
                        }

                        is Failed -> {
                            error = result.error
                            cancel()
                            Order.Equal
                        }
                    }
                }) { it.second }
            }
            Ok(Pair(minPromise.await(), maxPromise.await()))
        }
    } catch (e: CancellationException) {
        Failed(error!!)
    }
}