Skip to content

Instantly share code, notes, and snippets.

@erikkaplun
Last active August 29, 2015 14:08
Show Gist options
  • Save erikkaplun/e37670c1dd35a839f83e to your computer and use it in GitHub Desktop.
Save erikkaplun/e37670c1dd35a839f83e to your computer and use it in GitHub Desktop.
type Va[A] = Validation[String, A]
type VaNel[A] = ValidationNel[String, A]
import shapeless._, contrib.scalaz._, syntax.std.tuple._
import syntax.std.function._, ops.function._
import shapeless.ops.hlist._
// for Kleisli and its >=>
implicit val vaBinding = new Bind[Va] {
def map[A, B](fa: Va[A])(f: A => B): Va[B] = fa.map(f)
def bind[A, B](fa: Va[A])(f: A => Va[B]): Va[B] = {
import scalaz.Validation.FlatMap._
fa.flatMap(f)
}
}
type Vali[A, B] = Kleisli[Va, A, B]
val isIntegral = (msg: String) => Vali[Option[String], Option[Int]] {
case None => none[Int].success
case Some(x: String) =>
Try(x.toInt) match {
case Success(i) => Some(i).success
case _ => msg.failure
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment