[英]Lift \/[A, B] into EitherT[Future, A, B]
如何使用point/liftM
語法將\\/[Error, Int]
提升為EitherT[Future, Error, Int]
,以便提升在右側?
我有以下場景
for {
r1 <- f1: EitherT[Future, Error, Int]
r2 <- v: \/[Error, Int]
r3 <- f2: EitherT[Future, Error, Int]
} yield r3
我可以讓v
通過應用適合EitherT.fromDisjunction[Future]
像這樣
for {
r1 <- f1
r2 <- EitherT.fromDisjunction[Future](v)
r3 <- f2
} yield r3
或者干脆
for {
r1 <- f1
r2 <- EitherT(Future(v))
r3 <- f2
} yield r3
但是我試圖將提升魔法移動到v
的右側,就像這樣
for {
r1 <- f1
r2 <- v.point[Future].liftM[EitherT] // something approximately like this
r3 <- f2
} yield r3
我試過了
type Result[F[_], A] = EitherT[F, Error, A]
v.point[Future].liftM[Result]
和
v.point[({ type L[x] = EitherT[Future, Error, x] })#L]
EitherT[Future, Error, Error \/ Int]
雖然我需要
EitherT[Future, Error, Int]
將提升移到右側只是為了美觀,因為EitherT.fromDisjunction[Future]
工作得很好。
只是
r2 <- v.eitherT[Future, Error, Int]
同
import scalaz.{EitherT, \/}
import scalaz.std.scalaFuture._
import scalaz.syntax.eithert._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.language.higherKinds
我不確定scalaz中是否存在這種情況。 toEitherT[F]
沒有任何toEitherT[F]
。 但是添加新語法是微不足道的:
implicit class EitherLiftSyntax[A, B](val self: A \/ B) extends AnyVal {
def liftT[M[_]]: EitherT[M, A, B] = EitherT.fromDisjunction[M](self)
}
只要你有這個范圍,你可以說v.liftT[Future]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.