Yann Moisan
Yann Moisan

Reputation: 8281

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

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.

Upvotes: 1

Views: 402

Answers (1)

Peter Neyens
Peter Neyens

Reputation: 9820

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

Upvotes: 2

Related Questions