与猫平行拉开两个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

sumRight(30)均为a值时,我希望bRight。此外,与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))))