Map on Scalaz Validation failure

元气小坏坏 提交于 2019-12-05 00:18:30

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's type inference generally flows from left to right, so this is actually shorter:

"123".parseInt.<-:(_.toString)

And requires less annotation.

Didier Dupont

There is a functor on FailProjection. So you could do

v.fail.map(f).validation

(fail to type as FailProjection, validation to get out of it)

Alternatively

v.fold(f(_).failure, _.success)

Both a bit verbose. Maybe someone more familiar with scalaz can come up with something better

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!