0

Here are function definitions that return ReaderT:

  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))

I want to combine them. f1 and f2 are used as monadic effects. But result of f3 and f4 must be accumulated. I try to implement something that looks like:

  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))
    })

If more then one option exists, please offer both

Alexandr
  • 9,213
  • 12
  • 62
  • 102

1 Answers1

0

Try

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))
  })
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66