Den*_*VDB 1 scala filter either scalaz
假设我们有EitherT[F, String, A].withFilterScalaz 的函数使用了String's Monoid 的零,以便Left在过滤器失败时填写.
因此,没有有意义的错误消息.
我怎么能实现左边的"不积极"形式的东西.
val a: Either[Future, String, Int] = -1.point[EitherT[Future, String, ?]]
val foo = for {
aa <- a
if aa >= 0
} yield aa
Run Code Online (Sandbox Code Playgroud)
只是hacks的方法filter和withFilter方法是否EitherT满足for-comprehension要求?
你可以使用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)))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
227 次 |
| 最近记录: |