代码之家  ›  专栏  ›  技术社区  ›  Aki

与猫平行的两个未来

  •  1
  • Aki  · 技术社区  · 5 年前

    zip 接线员。当两者都成功并并行运行时,它结合了它们的价值观。 猫有两个孩子的时候有没有类似的东西 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
    

    我希望 sum Right(30) 什么时候 a b 两者都是 Right Future.zip

    2 回复  |  直到 5 年前
        1
  •  4
  •   Dmytro Mitin    5 年前

    我猜你在找一个有应用价值的人 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))))