Scalaz 上的地图验证失败
import scalaz._
import Scalaz._
"abc".parseInt
这将返回一个 Validation[NumberFormatException, Int]
。 有没有办法可以在失败端应用函数(例如 toString
)来获取 Validation[String, Int]
?
import scalaz._
import Scalaz._
"abc".parseInt
This will return a Validation[NumberFormatException, Int]
.
Is there a way I can apply a function on the failure side (such as toString
) to get a Validation[String, Int]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MAB[M[_,_], A, B]
<-: 和:->
> 只要存在Bifunctor[M]
,该映射就位于任何M[A, B]
的左侧和右侧。Validation
恰好是一个双函子,所以你可以这样做:Scala 的类型推断通常从左到右流动,所以这实际上更短:
并且需要更少的注释。
There is a pair of methods
<-:
and:->
defined onMAB[M[_,_], A, B]
that map on the left and right side of anyM[A, B]
as long as there is aBifunctor[M]
.Validation
happens to be a bifunctor, so you can do this:Scala's type inference generally flows from left to right, so this is actually shorter:
And requires less annotation.
FailProjection 上有一个函子。所以你可以这样做
(未能输入 FailProjection,验证以摆脱它)
或者
两者都有点冗长。也许更熟悉 scalaz 的人可以想出更好的东西
There is a functor on FailProjection. So you could do
(fail to type as FailProjection, validation to get out of it)
Alternatively
Both a bit verbose. Maybe someone more familiar with scalaz can come up with something better