-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce awaitAll and joinAll extensions for collections for Deferreds/Jobs #171
Comments
|
Related: I wrote this extension function which I use all the time:
edit: perhaps a more appropriate name would be |
@luisrayas3 It would be also useful if concurrent/parallel map supported limited parallism. The idea is leave it named |
Let me also add an idea by Brian Parma from public slack to have a something like |
Some possible implementations:
|
I made simple implementation for |
This is great! It is possible though to implement this preserving arity of the different operations and the type information they may return without coallescing their types to a list. It's not pretty how it can be done https://github.com/arrow-kt/arrow/blob/master/modules/core/arrow-syntax/src/main/kotlin/arrow/syntax/applicative/applicative.kt#L256 but Arrow users can already do without casting : import arrow.effects.*
data class Joined<A, B, C>(a: A, b: B, c: C)
val op1: DeferredK<A> = async { ... }.k()
val op2: DeferredK<B> = async { ... }.k()
val op3: DeferredK<C> = async { ... }.k()
val results: DeferredK<Joined> =
DeferredK.applicative().map(op1, op2, op3, { (a, b, c) ->
Joined(a, b, c)
}) While traversing and sequencing over |
Here's an implementation for waiting on lists of Jobs: |
@raulraja We try to keep
|
I landed here while searching for a way to get the first in a collection of deferreds to complete with a non-null value. I settled on: deferreds.map { it::await.asFlow() }.merge().filterNotNull().firstOrNull() Not sure if this warrants a shortcut or a new issue, e.g. /**
* Creates a flow from a collection of deferreds. Elements are emitted in the order they complete.
*/
fun <T> Iterable<Deferred<T>>.merge(): Flow<T> = map { it::await.asFlow() }.merge() or fun <T> Iterable<Deferred<T>>.asCompleted(): Flow<T> = map { it::await.asFlow() }.merge() or fun <T> Iterable<Deferred<T>>.race(): Flow<T> = map { it::await.asFlow() }.merge() or if a better way already exists? |
joinAll
is simplemap { it.join() }
, whileawaitAll
needs a slightly more involved implementation than a simplemap { it.await() }
. It shall actually wait on all of the deferreds from collection and crash as soon as any one of them crashes.The text was updated successfully, but these errors were encountered: