DennisVDB DennisVDB - 3 months ago 14
Scala Question

Future[List[Option[List[Double]]] to Future[Option[List[List[Double]]] in Scalaz

I want to transform a

List[Option[List[Double]]]
to a
Option[List[List[Double]]]
that yields
None
if one of the options fails (encapsulated in a
Future
). Normally I should be able to use
.sequence
from the Scalaz lib. However, when I have the following problem:

val matrix = for {
listOfOptions <- futureListOfOptions
optionOfList <- listOfOptions.sequence
} yield optionOfList


matrix: Future[Nothing]


listOfOptions: scala.List[Option[scala.List[Double]]]


optionOfList: Any


What am I doing wrong?

Answer

You were probably missing some import, or inferring type manually when using just sequence. Use sequenceU if you want compiler to try it for you. This works for me:

val start: List[Option[List[Double]]] = ???
  import scalaz.std.option.optionInstance
  import scalaz.std.list.listInstance
  import scalaz.syntax.traverse._

  val end: Option[List[List[Double]]] = start.sequenceU

Full example:

  val fstart: Future[List[Option[List[Double]]]] = ???
  import scalaz.std.scalaFuture.futureInstance

  val matrix = for {
    lo ← fstart
    ol = lo.sequenceU
  } yield ol
Comments