【问题标题】:EitherT filter with error in ScalazScalaz中的EitherT过滤器有错误
【发布时间】:2016-12-30 11:37:47
【问题描述】:

假设我们有EitherT[F, String, A]。 Scalaz 的withFilter 函数使用String 的 Monoid 的零,以便在过滤器失败时填充Left。

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

我怎样才能实现左为“不积极”的形式。

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

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

EitherT 上的 filter 和 withFilter 方法是否只是为了满足理解需求?

【问题讨论】:

    标签: scala filter scalaz either


    【解决方案1】:

    您可以使用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)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      • 2016-05-18
      相关资源
      最近更新 更多