Skip to content

Instantly share code, notes, and snippets.

@krasserm
Created December 17, 2010 09:49
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 krasserm/744723 to your computer and use it in GitHub Desktop.
Save krasserm/744723 to your computer and use it in GitHub Desktop.
import scalaz._
object ValidationApplicative extends Application {
import Scalaz._
case class Message(b: String)
// special-case sufficient for scalaz-camel
implicit def ExceptionSemigroup: Semigroup[Exception] = semigroup((e1, e2) => e1)
val f = (s1: Message) => (s2: Message) => Message("%s %s" format (s1.b, s2.b))
val x: Validation[Exception, Message] = Message("a").success
val y: Validation[Exception, Message] = Message("b").success
val z: Validation[Exception, Message] = Failure(new Exception("blah"))
println(x <*> y ∘ f) // Success(Message(b a))
println(x <*> z ∘ f) // Failure(java.lang.Exception: blah)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment