Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map on Scalaz Validation failure

Tags:

scala

scalaz

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]?

like image 616
huynhjl Avatar asked Sep 22 '11 14:09

huynhjl


1 Answers

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.

like image 68
Apocalisp Avatar answered Oct 10 '22 08:10

Apocalisp