DennisVDB DennisVDB - 1 year ago 40
Scala Question

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

I want to transform a

to a
that yields
if one of the options fails (encapsulated in a
). Normally I should be able to use
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 Source

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