【问题标题】:Kleisli[Future, Context, \/] to Kleisli[EitherT, Context, …]Kleisli[Future, Context, \/] 到 Kleisli[EitherT, Context, ...]
【发布时间】:2016-04-12 18:13:00
【问题描述】:

因为我想结合Kleisli 可以在可能失败Future 的长方法Either 上工作,所以我需要堆叠效果。这是在 Kleisli 中堆叠效果的结果代码。 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

【问题讨论】:

    标签: scala scalaz monad-transformers


    【解决方案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>)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多