Last active
June 29, 2017 10:50
-
-
Save PeterPerhac/e3fbf849800c8b7659f890bb789f1a3a to your computer and use it in GitHub Desktop.
Traversing OptionTs - how to get from List[OptionT[F,T]] to F[List[T]] by discarding None's
This file contains hidden or 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.data.OptionT | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration.Duration | |
import scala.concurrent.{Await, Future} | |
object CollectingOptionTs { | |
def main(args: Array[String]): Unit = { | |
import cats.instances.future._ | |
import cats.instances.list._ | |
import cats.syntax.traverse._ | |
val lots: List[OptionT[Future, Int]] = List(OptionT.some(4), OptionT.none, OptionT.some(2)) | |
val floints: Future[List[Int]] = lots.traverse(_.value).map(_.flatten) | |
println(Await.result(floints, Duration.Inf)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment