gpt4 book ai didi

scala - Scalaz 中的 Future[List[Option[List[Double]]] 到 Future[Option[List[List[Double]]]

转载 作者:行者123 更新时间:2023-12-01 09:32:27 25 4
gpt4 key购买 nike

我想将 List[Option[List[Double]]] 转换为 Option[List[List[Double]]] 产生 None 如果其中一个选项失败(封装在 Future 中)。通常我应该能够使用 Scalaz 库中的 .sequence。但是,当我遇到以下问题时:

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

矩阵: future [Nothing]
listOfOptions: scala.List[Option[scala.List[Double]]]
optionOfList: 任何

我做错了什么?

最佳答案

您可能遗漏了一些导入,或者在仅使用 sequence 时手动推断类型。如果您希望编译器为您尝试,请使用 sequenceU。这对我有用:

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

完整示例:

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

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

关于scala - Scalaz 中的 Future[List[Option[List[Double]]] 到 Future[Option[List[List[Double]]],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39144623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com