scala - 与猫并行压缩两个 EitherT[Future, _, _]
问题描述
scala 中的常规期货提供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)
whena
和b
are both Right
values。此外,与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, String, Int] = EitherT.right(Future(20))
val sum: EitherT[Future, String, Int] = (a, b).mapN(_ + _) // EitherT(Future(Success(Right(30))))
推荐阅读
- java - PDFBox 中的绝对位置
- java - 为什么在具有 struts 2 约定的 intellij 的 jsp 视图中 URL 不能解析
- mysql - 使用链接表和源表mysql创建子查询
- reinforcement-learning - 马尔科夫决策过程的状态转移与动作有关吗?
- sql - SQL如何按包含相同数据的两个不同列进行分组
- python - 从绘图屏幕上单击的两点画线,然后删除艺术家
- sql - 用分区联合 ALL
- python - Python 问题 | 在 https://www.testdome.com 上开发的代码的测试分数是否存在错误
- python-3.x - PUBLIC 属性和方法(适用于公共属性)是否可以被视为封装?
- html - 用户代理问题呈现不可读的标签 html 源数据