这是代码示例:
type FailFast[A] = Either[List[String], A]
import cats.instances.either._
def f1:ReaderT[FailFast, Map[String,String], Boolean] = ReaderT(_ => Right(true))
def f2:ReaderT[FailFast, Map[String,String], Boolean] = ReaderT(_ => Right(true))
def fc:ReaderT[FailFast, Map[String,String], Boolean] =
for {
b1 <- f1
if (b1)
b2 <- f2
} yield b2
错误是:
错误:(17, 13) 值 withFilter 不是 cat.data.ReaderT[TestQ.this.FailFast,Map[String,String],Boolean] b1 <- f1 的成员
如何用 f2 组合 f1。仅当 f1 返回 Right(true) 时才必须应用 f2。我通过以下方式解决了它:
def fc2:ReaderT[FailFast, Map[String,String], Boolean] =
f1.flatMap( b1 => {
if (b1)
f2
else ReaderT(_ => Right(true))
})
但我希望有一个更优雅的解决方案。