Skip to content

Instantly share code, notes, and snippets.

@mmenestret
Created January 30, 2020 16:16
Show Gist options
  • Save mmenestret/9b6aa45c0c94c9e82ecbcc922f3ff9f9 to your computer and use it in GitHub Desktop.
Save mmenestret/9b6aa45c0c94c9e82ecbcc922f3ff9f9 to your computer and use it in GitHub Desktop.
Short circuit List of Future based on predicate
def shortCircuitFutureList[A](fs: List[() => Future[A]], cond: A => Boolean)(
implicit executionContext: ExecutionContext): Future[A] = fs match {
case tail :: Nil => tail()
case head :: _ =>
head()
.filter(cond)
.recoverWith {
case _ => shortCircuitFutureList(fs.tail, cond)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment