genovich
08/09/2020, 6:29 AM(List<IO<T>>) -> IO<T>
which runs each IO in List<IO<T>>
in parallel and waiting for the first result?genovich
08/09/2020, 6:45 AMreduce { acc, io ->
coroutineContext.raceN(acc, io).map { it.fold(::identity, ::identity) }
}
but maybe there is may be better solutionsimon.vergauwen
08/09/2020, 6:10 PMnever
and fold
instead. It uses an IO
that never finishes as an empty value for the list. Such that emptyList().race()
would result in a race that never finishes, instead of blowing up like reduce
.
fun <A> List<IO<A>>.race(): IO<A> =
foldRight(never<A>()) { acc, io
IO.raceN(acc, io)
.map { it.fold(::identity, ::identity) }
}
Keep in mind that the `IO`s are scheduled in order, so the once scheduled first might have a slight advantage over the last onces being scheduled. Which might also depend on in which coroutineContext
you launch it etc.genovich
08/10/2020, 7:42 AMjulian
08/10/2020, 7:29 PMnever
@simon.vergauwengenovich
08/12/2020, 3:41 PMlist.fold(monoid)
in this case?simon.vergauwen
08/12/2020, 4:08 PMMonoid#empty
is never
finishingsimon.vergauwen
08/12/2020, 4:09 PM