【发布时间】:2019-11-24 18:40:31
【问题描述】:
我在执行foldLeft 的以下函数中使用EitherT[Future, Failure, Option[B]]。如何在下面的代码中设置foldLeft的初始值?
def doWork[A, B](seq: Set[A])(f: A => EitherT[Future, Failure, Option[B]]): EitherT[Future, Failure, Set[Option[B]]] =
seq.foldLeft(????? how to set this initial value here ????) {
case (acc, nxt) => acc.flatMap(bs => f(nxt).map(b => bs :+ b))
}
【问题讨论】:
-
我想你想以
Right(Set.empty)开头? -
使用 Right(Set.empty),acc 没有可调用的 flatMap 函数
-
如果使用 EitherT.right(Future(Set.empty)),则发现:cats.data.EitherT[Future,Failure,Seq[Option[B]]] [error] required: 猫。 data.EitherT[scala.concurrent.Future,Nothing,Seq[Nothing]]
-
您似乎混合了
Either和Try单子。您希望Either的左侧参数而不是Failure的类型是什么?也许Throwable? -
或
Failure只是您的自定义类型在某处实现了吗?
标签: scala scalaz scala-cats