Skip to content

Instantly share code, notes, and snippets.

@djspiewak
Last active December 31, 2022 19:46
Show Gist options
  • Save djspiewak/d2c18448d6379d09c6eb57e7cb73c57d to your computer and use it in GitHub Desktop.
Save djspiewak/d2c18448d6379d09c6eb57e7cb73c57d to your computer and use it in GitHub Desktop.
def raceAll[F[_], E, A](fs: List[F[A]])(implicit F: GenConcurrent[F, E]): F[A] =
F.deferred[Outcome[F, E, A]] flatMap { d =>
F uncancelable { poll =>
val fibersF = fs traverse { fa =>
fa.guaranteeCase(d.complete(_).void).start
}
fibersF flatMap { fibers =>
poll(d.get.guarantee(fibers.parTraverse_(_.cancel)).flatMap(_.embedNever))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment