2016-12-30 80 views
1

假设我们有EitherT[F, String, A]。 斯卡拉兹的withFilter函数使用String的Monoid的零来填充Left,如果过滤器失败。EitherT过滤器错误斯卡拉

因此,没有有意义的错误消息。

我怎么能实现一些形式的左“不积极”的形式。

val a: Either[Future, String, Int] = -1.point[EitherT[Future, String, ?]] 

val foo = for { 
    aa <- a 
    if aa >= 0 
} yield aa 

是在EitherTfilterwithFilter方法只是黑客以满足换理解的需求?

回答

4

您可以使用EitherT#ensure

import scalaz._, Scalaz._ 

val xs: List[String \/ Int] = 
    List(1.right, 2.right, -5.right, "other error".left, 3.right, -4.right) 

EitherT(xs).ensure("not positive")(_ > 0) 

// EitherT(List(\/-(1), \/-(2), -\/(not positive), -\/(other error), \/-(3), -\/(not positive)))