Created
September 16, 2012 16:49
-
-
Save tmyymmt/3733172 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compute1(maybeFoo: Option[Foo]): Option[Int] = | |
for { | |
foo <- maybeFoo | |
bar <- foo.bar | |
baz <- bar.baz | |
} yield baz.compute | |
// for compute2 | |
import scalaz._ | |
import Scalaz._ | |
sealed trait Validation[E, A] { | |
def map[B](f: A => B): Validation[E, B] | |
def flatMap[B](f: A => Validation[E, B]): Validation[E, B] | |
def liftFail[F](f: E => F): Validation[F, A] // unrelated to monads! | |
} | |
case class Success[E, A](a: A) extends Validation[E, A] { | |
def map[B](f: A => B): Validation[E, B] = new Success(f(a)) | |
def flatMap[B](f: A => Validation[E, B]): Validation[E, B] = f(a) | |
def liftFail[F](f: E => F): Validation[F, A] = new Success(a) | |
} | |
case class Failure[E, A](e: E) extends Validation[E, A] { | |
def map[B](f: A => B): Validation[E, B] = new Failure(e) | |
def flatMap[B](f: A => Validation[E, B]): Validation[E, B] = new Failure(e) | |
def liftFail[F](f: E => F): Validation[F, A] = new Failure(f(e)) | |
} | |
def compute2(foo: Foo): Validation[ComputeException, Int] = | |
for { | |
bar <- foo.bar.liftFail { new ComputeException(_) } | |
baz <- bar.baz.liftFail { new ComputeException(_) } | |
result <- baz.compute | |
} yield result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
There is no liftFail method in Scalaz Validation today.. :(
How i can use this feature?