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

☆樱花仙子☆ 提交于 2019-12-13 01:22:10

问题


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


来源:https://stackoverflow.com/questions/36568726/kleislifuture-context-to-kleislieithert-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!