Skip to content

Instantly share code, notes, and snippets.

View lukas-mi's full-sized avatar
🥔
potato

Lukas Mikelionis lukas-mi

🥔
potato
View GitHub Profile
@lukas-mi
lukas-mi / list-of-futures-to-future-list.scala
Last active March 22, 2018 13:42
Convert list of futures to future of list of successes and future of list of failures (List[Future[T]] to (Future[List[T]], Future[List[Throwable]]))
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}
object FutureHelper {
def partition[T](futures: List[Future[T]])(implicit executor: ExecutionContext): (Future[List[T]], Future[List[Throwable]]) = {
val futuresOfTry: List[Future[Try[T]]] = futures.map(_.transform(Success(_)))
val successes = Future.sequence(futuresOfTry)
.map(_.collect { case Success(x) => x })
val failures = Future.sequence(futuresOfTry)