Skip to content

Commit

Permalink
Clean Up Predicate A Bit (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-lazaro authored Feb 14, 2020
1 parent 574419b commit e1b06cc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
16 changes: 12 additions & 4 deletions core/src/main/scala/cats/collections/Predicate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class Predicate[-A] extends scala.Function1[A, Boolean] { self =>
/**
* returns a predicate which is the union of this predicate and another
*/
def |[B <: A](other: Predicate[B]): Predicate[B] = Predicate(a => apply(a) || other(a))
def |[B <: A](other: Predicate[B]): Predicate[B] = self union other

/**
* returns a predicate which is the intersection of this predicate and another
Expand All @@ -26,7 +26,7 @@ abstract class Predicate[-A] extends scala.Function1[A, Boolean] { self =>
/**
* returns a predicate which is the intersection of this predicate and another
*/
def &[B <: A](other: Predicate[B]): Predicate[B] = Predicate(a => apply(a) && other(a))
def &[B <: A](other: Predicate[B]): Predicate[B] = self intersection other

/**
* Returns true if the value satisfies the predicate.
Expand All @@ -41,7 +41,7 @@ abstract class Predicate[-A] extends scala.Function1[A, Boolean] { self =>
/**
* Returns the predicate which is the the difference of another predicate removed from this predicate
*/
def -[B <: A](remove: Predicate[B]): Predicate[B] = Predicate(a => apply(a) && !remove(a))
def -[B <: A](remove: Predicate[B]): Predicate[B] = self diff remove

/**
* Return the opposite predicate
Expand All @@ -63,12 +63,20 @@ object Predicate extends PredicateInstances {
}

trait PredicateInstances {
implicit def predicateContravariantMonoidal: ContravariantMonoidal[Predicate] = new ContravariantMonoidal[Predicate] {
override def contramap[A, B](fb: Predicate[A])(f: B => A): Predicate[B] =
Predicate(f andThen fb.apply)
override def product[A, B](fa: Predicate[A], fb: Predicate[B]): Predicate[(A, B)] =
Predicate(v => fa(v._1) || fb(v._2))
override def unit: Predicate[Unit] = Predicate.empty
}

implicit def predicateMonoid[A]: Monoid[Predicate[A]] = new Monoid[Predicate[A]] {
override def empty: Predicate[A] = Predicate.empty
override def combine(l: Predicate[A], r: Predicate[A]): Predicate[A] = l union r
}

implicit val predicateInstance: MonoidK[Predicate] = new MonoidK[Predicate] {
implicit val predicateMonoidK: MonoidK[Predicate] = new MonoidK[Predicate] {
override def empty[A]: Predicate[A] = Predicate.empty
override def combineK[A](l: Predicate[A], r: Predicate[A]): Predicate[A] = l union r
}
Expand Down
17 changes: 15 additions & 2 deletions tests/src/test/scala/cats/collections/PredicateSpec.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package cats.collections
package tests

import cats.laws.discipline.SerializableTests
import cats.collections.arbitrary.predicate._
import cats.laws.discipline.{ContravariantMonoidalTests, SerializableTests}
import cats._
import cats.instances.int._
import cats.tests.CatsSuite

class PredicateSpec extends CatsSuite {

checkAll("Monoid[Predicate[Int]]", SerializableTests.serializable(Monoid[Predicate[Int]]))
checkAll("MonoidK[Predicate]", SerializableTests.serializable(MonoidK[Predicate]))
checkAll("Serializable[ContravariantMonoidal[Predicate]]", SerializableTests.serializable(ContravariantMonoidal[Predicate]))

{
implicit val eqForPredicateInt: Eq[Predicate[Int]] = new Eq[Predicate[Int]] {
override def eqv(x: Predicate[Int], y: Predicate[Int]): Boolean = x(0) === y(0)
}
implicit val eqForPredicateTripleInt: Eq[Predicate[(Int, Int, Int)]] = new Eq[Predicate[(Int, Int, Int)]] {
override def eqv(x: Predicate[(Int, Int, Int)], y: Predicate[(Int, Int, Int)]): Boolean = x((0, 0, 0)) === y((0, 0, 0))
}
checkAll("ContravariantMonoidal[Predicate]", ContravariantMonoidalTests[Predicate].contravariantMonoidal[Int, Int, Int])
}

test("intersection works")(
forAll { (as: List[Int], bs: List[Int]) =>
Expand Down

0 comments on commit e1b06cc

Please sign in to comment.