与猫并行压缩两个 EitherT[Future, _, _]

Zip two EitherT[Future, _, _] in parallel with cats

scala 中的常规期货提供 zip 运算符。当两者都成功并且 运行s 它们并行时,它会合并它们的值。 有两个 EitherT[Future, _, _].

的猫是否有类似的东西
val a: EitherT[Future, String, Int] = EitherT.right(10)
val b: EitherT[Future, String, Int] = EitherT.right(20)
val sum: EitherT[Future, String, Int] = for ((a, b) <- a zip b) yield a + b

ab 都是 Right 值时,我希望 sumRight(30)。此外,与 Future.zip 函数一样,两个 futures 应该 运行 并行:

我猜你正在寻找应用程序 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, String, Int] = EitherT.right(Future(20))

val sum: EitherT[Future, String, Int] = (a, b).mapN(_ + _) // EitherT(Future(Success(Right(30))))