Skip to content

Instantly share code, notes, and snippets.

@eamelink
Created December 23, 2015 12:07
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 eamelink/340a13ff706080ff8b7c to your computer and use it in GitHub Desktop.
Save eamelink/340a13ff706080ff8b7c to your computer and use it in GitHub Desktop.
Custom filter on disjunction, avoid need for Monoid
import scalaz._
import scalaz.Scalaz._
object Test extends App{
trait Zero[A] {
def zero: A
}
implicit class RichDisjunction[A, B](value: A \/ B) {
def withFilter(p: B => Boolean)(implicit za: Zero[A]): A \/ B = value match {
case \/-(b) if !p(b) => -\/(za.zero)
case _ => value
}
}
case class FailType(msg: String)
implicit val zeroFailType = new Zero[FailType] { val zero = FailType("too bad buddy!") }
val result = for {
age <- 15.right[FailType] if age >= 18
} yield age
println(result) // Prints -\/(FailType(too bad buddy!))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment