【发布时间】:2014-03-23 17:42:56
【问题描述】:
在 Scala/Scalaz 中链接 Either 或 \/ 类型以从左侧的“失败”值中恢复是相当容易的。
除了函数(T1, T2) => Future[A \/ B],如何获得相同的行为?
我已经能够将其分解为两个不同的方向,但无法组合解决方案。
只需利用 Scalaz 的 EitherT 作为 type FutureEither[A, B] = EitherT[Future, A, B] 和 Future 的 monad 实例,就可以轻松链接 Future[A \/ B]。
我还可以使用 EitherT 链接 (T1, T2) => A \/ B 类型:
implicit def wrapTransform[T1, T2, A, B](f: Function2[T1, T2, A \/ B]) = EitherT[({ type λ[α] = Function2[T1, T2, α] })#λ, A, B](f)
implicit def unwrapTransform[T1, T2, A, B](e: EitherT[({ type λ[α] = Function2[T1, T2, α] })#λ, A, B]): Function2[T1, T2, A \/ B] = e.run
// some dummy functions
def times2(fail: Boolean, v: Int): String \/ Int = if (fail) "times2:FAILED".left else (v * 2).right
def fail(v1: Boolean, v: Int): String \/ Int = "fail:FAILED".left
def alwaysPlus1(ignore: Boolean, v: Int): String \/ Int = (v + 1).right
val times2_times2 = (times2 _) orElse (times2 _)
val alwaysPlus1_times2 = (alwaysPlus1 _) orElse (times2 _)
Console println times2_times2(false, 10) // prints '\/-(20)'
Console println times2_times2(true, 10) // prints '-\/(times2:FAILED)'
Console println alwaysPlus1_times2(false, 10) // prints '\/-(11)''
Console println alwaysPlus1_times2(true, 10) // prints '\/-(11)'
您如何编写适用于(T1, T2) => Future[A \/ B] 类型的两级嵌套的转换器?
我的函数式编程知识仍在学习中,因此对于措辞和术语使用不当,我深表歉意。
【问题讨论】:
标签: scala functional-programming monads scalaz