package com.lagradost.cloudstream3 import com.lagradost.cloudstream3.mvvm.logError import kotlinx.coroutines.async import kotlinx.coroutines.runBlocking //https://stackoverflow.com/questions/34697828/parallel-operations-on-kotlin-collections /* fun Iterable.pmap( numThreads: Int = maxOf(Runtime.getRuntime().availableProcessors() - 2, 1), exec: ExecutorService = Executors.newFixedThreadPool(numThreads), transform: (T) -> R, ): List { // default size is just an inlined version of kotlin.collections.collectionSizeOrDefault val defaultSize = if (this is Collection<*>) this.size else 10 val destination = Collections.synchronizedList(ArrayList(defaultSize)) for (item in this) { exec.submit { destination.add(transform(item)) } } exec.shutdown() exec.awaitTermination(1, TimeUnit.DAYS) return ArrayList(destination) }*/ fun Map.apmap(f: suspend (Map.Entry) -> R): List = runBlocking { map { async { f(it) } }.map { it.await() } } fun List.apmap(f: suspend (A) -> B): List = runBlocking { map { async { f(it) } }.map { it.await() } } fun List.apmapIndexed(f: suspend (index: Int, A) -> B): List = runBlocking { mapIndexed { index, a -> async { f(index, a) } }.map { it.await() } } // run code in parallel /*fun argpmap( vararg transforms: () -> R, numThreads: Int = maxOf(Runtime.getRuntime().availableProcessors() - 2, 1), exec: ExecutorService = Executors.newFixedThreadPool(numThreads) ) { for (item in transforms) { exec.submit { item.invoke() } } exec.shutdown() exec.awaitTermination(1, TimeUnit.DAYS) }*/ // built in try catch fun argamap( vararg transforms: suspend () -> R, ) = runBlocking { transforms.map { async { try { it.invoke() } catch (e: Exception) { logError(e) } } }.map { it.await() } }