EitherT 过滤器在 Scalaz 中有错误

EitherT filter with error in Scalaz

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

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

我如何实现 Left 的形式 "Not positive"。

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

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

EitherT 上的 filterwithFilter 方法是否只是为了满足理解需求而进行的黑客攻击?

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