Skip to content

Instantly share code, notes, and snippets.

@eamelink
Created December 23, 2015 12:07

Revisions

  1. eamelink revised this gist Dec 23, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions DisjunctionFilter.scala
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,6 @@ object Test extends App{
    age <- 15.right[FailType] if age >= 18
    } yield age

    println(result)
    println(result) // Prints -\/(FailType(too bad buddy!))

    }
  2. eamelink created this gist Dec 23, 2015.
    26 changes: 26 additions & 0 deletions DisjunctionFilter.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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)

    }