-
Notifications
You must be signed in to change notification settings - Fork 529
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
Add raceN / firstCompletedOf recipe #3565
base: series/3.4.x
Are you sure you want to change the base?
Conversation
6e7a3c3
to
77cb807
Compare
|
||
```scala mdoc:silent | ||
def raceN[A](ios: List[IO[A]]): IO[Either[Throwable, A]] = | ||
Deferred[IO, Either[Throwable, A]].flatMap { d => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should stop using this type generally and prefer the use of Outcome. This ends in a lot of weird deadlocks if folks use this without considering cancelation. So generally I say outcome or not at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the example mentions that the hypothetical user only cares about either success or error.
We may add a third example using Outcome
or just being more explicit in the final note that another option is to consider cancelation (I prefer this one).
Assume you have a bunch of `IOs` that you want to run all in parallel and get the first success; | ||
canceling all the others. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I throw one more implementation into the ring?
def raceN[A](ios: List[IO[A]]): IO[A] = ios match {
case Nil => IO.raiseError[A](new NoSuchElementException)
case head :: tail => tail.foldLeft(head)(_.race(_).map(_.merge))
}
Edit: fixed 😇
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was OP's original implementation (or something similar)
It really doesn't feel natural, at least to me.
Deferred[IO, A].flatMap { d => | ||
ios.parTraverse_(io => io.flatMap(d.complete)).background.surround(d.get) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Playing more golf:
Deferred[IO, A].flatMap { d => | |
ios.parTraverse_(io => io.flatMap(d.complete)).background.surround(d.get) | |
} | |
IO.deferred[Outcome[IO, Throwable, A]] flatMap { d => | |
ios.traverse(io => io.guaranteeCase(d.complete(_).void).background) | |
.surround(d.get) | |
.flatMap(_.embedNever) | |
} |
Though maybe I'm putting the suggestion in the wrong spot, since this isn't in any way the simplest implementation.
Motivated by a recent discussion on Discord.
Since this method can have many variations and is not very common, I guess a recipe is the best place to add it.