Created
July 8, 2016 12:48
-
-
Save psttf/fbfb30637778e7fe82bd30162f4f637f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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