https://kotlinlang.org logo
#coroutines
Title
# coroutines
g

Gopal S Akshintala

08/02/2020, 1:09 PM
If I have a
List<A>
and a fn
suspend (A) -> B
, how can I apply this fn on the list in parallel?
t

tseisel

08/02/2020, 1:31 PM
Copy code
coroutineScope {
  list.map {
    async { transform(it) }
  }.awaitAll()
}
Is one way to do it.
n

Nick Johnson

08/02/2020, 6:48 PM
I would agree with Thibault in high latency operations (file reading, network) but I'm a simple transform, if try to avoid coroutines altogether
a

Andrea Giuliano

08/02/2020, 7:45 PM
would a
Copy code
list.asFlow().collect { mySuspendingFunction(it) }
work in this case?
j

Joffrey

08/03/2020, 8:51 AM
@Andrea Giuliano that would be processed sequentially, though, not in parallel
👍 1
4 Views