Skip to content

Instantly share code, notes, and snippets.

@psttf
Created July 8, 2016 12:48
Show Gist options
  • Save psttf/fbfb30637778e7fe82bd30162f4f637f to your computer and use it in GitHub Desktop.
Save psttf/fbfb30637778e7fe82bd30162f4f637f to your computer and use it in GitHub Desktop.
import cats.Monad
import scala.concurrent.Future
import scala.language.higherKinds
import scala.concurrent.ExecutionContext.Implicits.global
object Swap {
def apply[F[_], A](l: List[F[A]])(implicit m: Monad[F]): F[List[A]] =
l.foldLeft(m pure List.empty[A])((futureList, futureNext) =>
m.flatMap(futureList)(list => m.map(futureNext)(_ :: list))
)
def apply[A](l: List[Future[A]]): Future[List[A]] =
l.foldLeft(Future successful List.empty[A])((futureList, futureNext) =>
futureList flatMap (list => futureNext map (_ :: list))
)
}
Swap(List(Future successful 1, Future successful 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment