Created
December 23, 2015 12:07
-
-
Save eamelink/340a13ff706080ff8b7c to your computer and use it in GitHub Desktop.
Custom filter on disjunction, avoid need for Monoid
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
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