Kleisli[Future, Context, \\/] to Kleisli[EitherT,

2019-08-10 02:47发布

问题:

As I want to combine Kleisli that works on long methods Future that can fail Either, I need to stack the effect. Here is the resulting code to stack the effect in the Kleisli. Is there an existing combinator in scalaz ?

type FutureEitherT[A] = EitherT[Future, String, A]

def toKleisliEitherTFromDisjunction[A](f: Kleisli[Future, Context,String \/ A]) = 
  Kleisli[FutureEitherT, Context, A] { ctx => EitherT(f(ctx)) }

I've tried without success f.liftMK[FutureEitherT], but unfortunately, the third types in the Kleisli type constructor is still an Either.

回答1:

You could use mapK:

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scalaz.{\/, EitherT, Kleisli}
import scalaz.std.scalaFuture

type FutureEitherT[A] = EitherT[Future, String, A]

val futureK: Kleisli[Future, Int, String \/ Int] = 
  Kleisli.ask[Future, Int] map (i => \/.right[String, Int](i)))

futureK mapK[FutureEitherT, Int]( EitherT.eitherT )
// scalaz.Kleisli[FutureEitherT,Int,Int] = Kleisli(<function1>)