如何解决与猫平行拉开两个EitherT [Future,_,_]
scala中的常规期货提供zip
运算符。当它们都成功时,它将它们的值合并并并行运行它们。
猫有两个EitherT[Future,_,_]
时,猫身上有类似的东西吗?
val a: EitherT[Future,String,Int] = EitherT.right(10)
val b: EitherT[Future,Int] = EitherT.right(20)
val sum: EitherT[Future,Int] = for ((a,b) <- a zip b) yield a + b
当sum
和Right(30)
均为a
值时,我希望b
是Right
。此外,与Future.zip
函数一样,两个期货应并行运行:
解决方法
我想您正在寻找适用的mapN
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits._
import cats.data.EitherT
import cats.instances.future._
import cats.syntax.apply._
val a: EitherT[Future,String,Int] = EitherT.right(Future(10))
val b: EitherT[Future,Int] = EitherT.right(Future(20))
val sum: EitherT[Future,Int] = (a,b).mapN(_ + _) // EitherT(Future(Success(Right(30))))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。