Scala: for-comprehension with guard inside reader

681 Views Asked by At

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.

1

There are 1 best solutions below

3
On BEST ANSWER
  1. The huge ReaderT[FailFast, Map[String, String], Boolean] type is annoying. I replaced it by ConfFF-shortcut ("map-configured fail-fast"); You probably can find a better name for that.
  2. You can still use the for-comprehension syntax, if you want.
  3. No need to write out all the _ => and Right(...) every time, just use appropriate pure from applicative.

Thus, your fc2 becomes:

  def fc3: ConfFF[Boolean] =
    for {
      b1 <- f1
      b2 <- if (b1) f2 else true.pure[ConfFF]
    } yield b2

Full code:

import scala.util.{Either, Left, Right}
import cats.instances.either._
import cats.data.ReaderT
import cats.syntax.applicative._

object ReaderTEitherListExample {

  type FailFast[A] = Either[List[String], A]
  /** Shortcut "configured fail-fast" */
  type ConfFF[A] = ReaderT[FailFast, Map[String, String], A]

  def f1: ConfFF[Boolean] = ReaderT(_ => Right(true))
  def f2: ConfFF[Boolean] = ReaderT(_ => Right(true))

  def fc3: ConfFF[Boolean] =
    for {
      b1 <- f1
      b2 <- if (b1) f2 else true.pure[ConfFF]
    } yield b2
}