Skip to content

Instantly share code, notes, and snippets.

@channingwalton
Created August 2, 2012 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save channingwalton/3240547 to your computer and use it in GitHub Desktop.
Save channingwalton/3240547 to your computer and use it in GitHub Desktop.
Kleisli problems
object KleisliValidation extends App {
import scalaz._
import Scalaz._
import scala.util.control.Exception._
implicit def vm = Validation.validationMonad[String]
type EValidation[+T] = Validation[String, T]
def toDouble(s: String): EValidation[Double] = allCatch.either(s.toDouble).fold(_.toString.fail, _.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
println(composed("2"))
println(composed("-2"))
println(composed("hi"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment