Here is code example:
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
The error is:
Error:(17, 13) value withFilter is not a member of cats.data.ReaderT[TestQ.this.FailFast,Map[String,String],Boolean] b1 <- f1
How can I compose f1 with f2. f2 must be applied only if f1 returns Right(true). I solved it via:
def fc2:ReaderT[FailFast, Map[String,String], Boolean] =
f1.flatMap( b1 => {
if (b1)
f2
else ReaderT(_ => Right(true))
})
But I hope there is a more elegant solution.