Skip to content

Instantly share code, notes, and snippets.

@PeterPerhac
Last active June 29, 2017 10:50
Show Gist options
  • Save PeterPerhac/e3fbf849800c8b7659f890bb789f1a3a to your computer and use it in GitHub Desktop.
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
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