首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scala-Cats:组合具有应用效果的一元

Scala-Cats:组合具有应用效果的一元
EN

Stack Overflow用户
提问于 2019-03-27 19:32:38
回答 1查看 97关注 0票数 0

下面是返回ReaderT的函数定义:

代码语言:javascript
复制
  def f1:ReaderT[FailFast, Map[String,String], Boolean] = ReaderT(_ => Right(true))
  def f2:ReaderT[FailFast, Map[String,String], Boolean] = ReaderT(_ => Left(List("d")))
  def f3:ReaderT[FailFast, Map[String,String], Boolean] = ReaderT(_ => Right(true))
  def f4:ReaderT[FailFast, Map[String,String], Boolean] = ReaderT(_ => Right(true))

我想把它们结合起来。f1f2被用作一元效果。但f3f4的计算结果必须进行累加。我尝试实现的东西看起来像这样:

代码语言:javascript
复制
  def fc:ReaderT[FailFast, Map[String,String], Boolean] =
    f1.flatMap( b1 => {
      if (b1)
        for {
          b2 <- f2
          b3 <- Semigroupal.tuple2[FailSlow, Boolean, Boolean](
            f3, // how to convert it to validated here without run?
            f4  // how to convert it to validated here without run?
          ).toEither
        } yield b3
      else ReaderT(_ => Right(true))
    })

如果存在多个选项,请同时提供两个选项

EN

回答 1

Stack Overflow用户

发布于 2019-04-09 00:13:04

试一试

代码语言:javascript
复制
import cats.instances.either._
import cats.instances.list._

type FailSlow[A] = Validated[List[String], A]
type FailFast[A] = Either[List[String], A]

def fc:ReaderT[FailFast, Map[String,String], (Boolean, Boolean)] =
  f1.flatMap( b1 => {
    if (b1)
      for {
        b2 <- f2
        b3 <- Semigroupal.tuple2(
          f3.mapF(Validated.fromEither),
          f4.mapF(Validated.fromEither)
        ).mapF(_.toEither)
      } yield b3
    else ReaderT(_ => Right(true, true))
  })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55376183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档