2016-04-12 51 views
1

因为我想合并Kleisli,对长方法Future工作,可能会失败Either,我需要叠加效果。下面是在Kleisli中叠加效果的结果代码。 scalaz中是否存在combinator?Kleisli [Future,Context, /] to Kleisli [EitherT,Context,...]

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

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

我已经尝试没有成功f.liftMK[FutureEitherT],但不幸的是,第三类型的Kleisli类型构造仍是一个Either

回答

1

你可以使用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>)