Skip to content
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

Open
wants to merge 2 commits into
base: series/3.4.x
Choose a base branch
from

Conversation

BalmungSan
Copy link
Contributor

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.

@BalmungSan BalmungSan changed the base branch from series/3.x to series/3.4.x April 26, 2023 16:54
docs/recipes.md Outdated Show resolved Hide resolved
docs/recipes.md Outdated Show resolved Hide resolved

```scala mdoc:silent
def raceN[A](ios: List[IO[A]]): IO[Either[Throwable, A]] =
Deferred[IO, Either[Throwable, A]].flatMap { d =>

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.

Copy link
Contributor Author

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).

Comment on lines +163 to +164
Assume you have a bunch of `IOs` that you want to run all in parallel and get the first success;
canceling all the others.
Copy link
Member

@armanbilge armanbilge Apr 27, 2023

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 😇

Copy link
Contributor Author

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.

Comment on lines +168 to +170
Deferred[IO, A].flatMap { d =>
ios.parTraverse_(io => io.flatMap(d.complete)).background.surround(d.get)
}
Copy link
Member

@djspiewak djspiewak Apr 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Playing more golf:

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants