Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example of Kleisli composition of Validation
object KleisliValidation extends App {
import scalaz._
import Scalaz._
import scala.util.control.Exception._
type EValidation[+T] = Validation[String, T]
implicit val binding = new Bind[EValidation] {
def map[A, B](fa: EValidation[A])(f: A => B): EValidation[B] =
def bind[A, B](fa: EValidation[A])(f: A => EValidation[B]): EValidation[B] = fa.flatMap(f)
def toDouble(s: String):EValidation[Double] = allCatch.either(s.toDouble).fold(, _.success)
def sqrt(d: Double):EValidation[Double] = if (d >= 0) math.sqrt(d).success else "sqrt(%s) is too complex for me".format(d).fail
val composed = Kleisli(toDouble _) >==> (sqrt(_))
Failure(sqrt(-2.0) is too complex for me)
Failure(java.lang.NumberFormatException: For input string: "hi")

This comment has been minimized.

Show comment Hide comment

channingwalton Aug 1, 2012

BTW this requires scalaz 7


channingwalton commented Aug 1, 2012

BTW this requires scalaz 7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment