Scalaz 验证失败的地图 [英] Map on Scalaz Validation failure
问题描述
import scalaz._
import Scalaz._
"abc".parseInt
这将返回一个 Validation[NumberFormatException, Int]
.有没有办法可以在失败端应用函数(例如 toString
)来获得 Validation[String, Int]
?
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]
?
推荐答案
<-:
和 :->
定义了一对方法>MAB[M[_,_], A, B] 任何M[A, B]
的左右两边只要有的映射双函子[M]
.Validation
恰好是一个双函子,所以你可以这样做:
There is a pair of methods <-:
and :->
defined on MAB[M[_,_], A, B]
that map on the left and right side of any M[A, B]
as long as there is a Bifunctor[M]
. Validation
happens to be a bifunctor, so you can do this:
((_:NumberFormatException).toString) <-: "123".parseInt
Scala 的类型推断一般从左到右流动,所以这实际上更短:
Scala's type inference generally flows from left to right, so this is actually shorter:
"123".parseInt.<-:(_.toString)
并且需要更少的注释.
这篇关于Scalaz 验证失败的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!