>function par_map(iterable $it, callable $c) { > $result = []; > async { > foreach ($it as $val) { > $result[] = $c($val); > } > } >return $result; >}
If the assumption is that each call can be asynchronous and all elements need to be processed, the only proper tool is a concurrent iterator. Manually using a foreach loop is not the best idea because the iterator does not necessarily create a coroutine for each iteration. And, of course, such an iterator should have a getFuture method that allows waiting for the result. Yes, Kotlin has an explicit blocking Scope, but I don’t see much need for it. So far, all the cases we’re considering fit neatly into a framework: 1. I want to launch a coroutine and wait: await spawn 2. I want to launch a coroutine and not wait: spawn 3. I want to launch a group of coroutines and wait: await CoroutineScope 4. I want to launch a group of coroutines and not wait: spawn 5. I want a concurrent iteration: special iterator. What else are we missing?