Kleisli[Future, Context, \/] 到 Kleisli[EitherT, Context, ...] [英] Kleisli[Future, Context, \/] to Kleisli[EitherT, Context, …]
本文介绍了Kleisli[Future, Context, \/] 到 Kleisli[EitherT, Context, ...]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因为我想结合适用于长方法 Future
的 Kleisli
可能会失败 Either
,所以我需要叠加效果.这是在 Kleisli 中堆叠效果的结果代码.scalaz 中是否有现有的组合器?
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)) }
我试过没有成功 f.liftMK[FutureEitherT]
,但不幸的是,Kleisli
类型构造函数中的第三个类型仍然是一个 Either代码>.
I've tried without success f.liftMK[FutureEitherT]
, but unfortunately, the third types in the Kleisli
type constructor is still an Either
.
推荐答案
你可以使用 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>)
这篇关于Kleisli[Future, Context, \/] 到 Kleisli[EitherT, Context, ...]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文