Skip to content

Commit

Permalink
Merge pull request #3158 from gagandeepkalra/functorFilter/addFilterN…
Browse files Browse the repository at this point in the history
…otFunction

add `filterNot` to FunctorFilter
  • Loading branch information
travisbrown authored Dec 9, 2019
2 parents b3bc539 + bd9e60b commit cc0ba4b
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions core/src/main/scala-2.12/cats/instances/stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ private[instances] trait StreamInstancesBinCompat0 {

override def filter[A](fa: Stream[A])(f: (A) => Boolean): Stream[A] = fa.filter(f)

override def filterNot[A](fa: Stream[A])(f: A => Boolean): Stream[A] = fa.filterNot(f)

override def collect[A, B](fa: Stream[A])(f: PartialFunction[A, B]): Stream[B] = fa.collect(f)

override def flattenOption[A](fa: Stream[Option[A]]): Stream[A] = fa.flatten
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala-2.13+/cats/instances/lazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ trait LazyListInstances extends cats.kernel.instances.LazyListInstances {

override def filter[A](fa: LazyList[A])(f: (A) => Boolean): LazyList[A] = fa.filter(f)

override def filterNot[A](fa: LazyList[A])(f: (A) => Boolean): LazyList[A] = fa.filterNot(f)

override def collect[A, B](fa: LazyList[A])(f: PartialFunction[A, B]): LazyList[B] = fa.collect(f)

override def flattenOption[A](fa: LazyList[Option[A]]): LazyList[A] = fa.flatten
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/scala/cats/FunctorFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@ trait FunctorFilter[F[_]] extends Serializable {
*/
def filter[A](fa: F[A])(f: A => Boolean): F[A] =
mapFilter(fa)(a => if (f(a)) Some(a) else None)

/**
* Apply a filter to a structure such that the output structure contains all
* `A` elements in the input structure that do not satisfy the predicate `f`.
*/
def filterNot[A](fa: F[A])(f: A => Boolean): F[A] =
mapFilter(fa)(Some(_).filterNot(f))
}
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/data/Chain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,8 @@ sealed abstract private[data] class ChainInstances extends ChainInstances1 {

override def filter[A](fa: Chain[A])(f: A => Boolean): Chain[A] = fa.filter(f)

override def filterNot[A](fa: Chain[A])(f: A => Boolean): Chain[A] = fa.filterNot(f)

override def collect[A, B](fa: Chain[A])(f: PartialFunction[A, B]): Chain[B] = fa.collect(f)

override def mapFilter[A, B](fa: Chain[A])(f: A => Option[B]): Chain[B] = fa.collect(Function.unlift(f))
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/data/Const.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ sealed abstract private[data] class ConstInstances extends ConstInstances0 {

override def filter[A](fa: Const[C, A])(f: (A) => Boolean): Const[C, A] = fa.retag

override def filterNot[A](fa: Const[C, A])(f: A => Boolean): Const[C, A] = fa.retag

def traverseFilter[G[_], A, B](
fa: Const[C, A]
)(f: (A) => G[Option[B]])(implicit G: Applicative[G]): G[Const[C, B]] =
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala/cats/data/Nested.scala
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ abstract private[data] class NestedFunctorFilter[F[_], G[_]] extends FunctorFilt

override def filter[A](fa: Nested[F, G, A])(f: (A) => Boolean): Nested[F, G, A] =
Nested[F, G, A](F.map(fa.value)(G.filter(_)(f)))

override def filterNot[A](fa: Nested[F, G, A])(f: A => Boolean): Nested[F, G, A] =
Nested[F, G, A](F.map(fa.value)(G.filterNot(_)(f)))

}

abstract private[data] class NestedTraverseFilter[F[_], G[_]]
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/data/OptionT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ sealed private[data] trait OptionTFunctorFilter[F[_]] extends FunctorFilter[Opti
override def flattenOption[A](fa: OptionT[F, Option[A]]): OptionT[F, A] = fa.subflatMap(identity)

override def filter[A](fa: OptionT[F, A])(f: (A) => Boolean): OptionT[F, A] = fa.filter(f)

override def filterNot[A](fa: OptionT[F, A])(f: A => Boolean): OptionT[F, A] = fa.filterNot(f)
}

sealed private[data] trait OptionTOrder[F[_], A] extends Order[OptionT[F, A]] with OptionTPartialOrder[F, A] {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/instances/list.scala
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ private[instances] trait ListInstancesBinCompat0 {

override def filter[A](fa: List[A])(f: (A) => Boolean): List[A] = fa.filter(f)

override def filterNot[A](fa: List[A])(f: A => Boolean): List[A] = fa.filterNot(f)

override def collect[A, B](fa: List[A])(f: PartialFunction[A, B]): List[B] = fa.collect(f)

override def flattenOption[A](fa: List[Option[A]]): List[A] = fa.flatten
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/instances/map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ private[instances] trait MapInstancesBinCompat0 {
override def filter[A](fa: Map[K, A])(f: A => Boolean) =
fa.filter { case (_, v) => f(v) }

override def filterNot[A](fa: Map[K, A])(f: A => Boolean): Map[K, A] =
fa.filterNot { case (_, v) => f(v) }

}

}
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/instances/option.scala
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ private[instances] trait OptionInstancesBinCompat0 {

override def filter[A](fa: Option[A])(f: (A) => Boolean): Option[A] = fa.filter(f)

override def filterNot[A](fa: Option[A])(f: A => Boolean): Option[A] = fa.filterNot(f)

override def collect[A, B](fa: Option[A])(f: PartialFunction[A, B]): Option[B] = fa.collect(f)

override def flattenOption[A](fa: Option[Option[A]]): Option[A] = fa.flatten
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/instances/queue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ private object QueueInstances {

override def filter[A](fa: Queue[A])(f: (A) => Boolean): Queue[A] = fa.filter(f)

override def filterNot[A](fa: Queue[A])(f: A => Boolean): Queue[A] = fa.filterNot(f)

override def collect[A, B](fa: Queue[A])(f: PartialFunction[A, B]): Queue[B] = fa.collect(f)

override def flattenOption[A](fa: Queue[Option[A]]): Queue[A] = fa.flatten
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/instances/sortedMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ private[instances] trait SortedMapInstancesBinCompat0 {
override def filter[A](fa: SortedMap[K, A])(f: (A) => Boolean): SortedMap[K, A] =
fa.filter { case (_, v) => f(v) }

override def filterNot[A](fa: SortedMap[K, A])(f: A => Boolean): SortedMap[K, A] =
fa.filterNot { case (_, v) => f(v) }

override def filterA[G[_], A](
fa: SortedMap[K, A]
)(f: (A) => G[Boolean])(implicit G: Applicative[G]): G[SortedMap[K, A]] =
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/instances/vector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ private[instances] trait VectorInstancesBinCompat0 {

override def filter[A](fa: Vector[A])(f: (A) => Boolean): Vector[A] = fa.filter(f)

override def filterNot[A](fa: Vector[A])(f: A => Boolean): Vector[A] = fa.filterNot(f)

override def collect[A, B](fa: Vector[A])(f: PartialFunction[A, B]): Vector[B] = fa.collect(f)

override def flattenOption[A](fa: Vector[Option[A]]): Vector[A] = fa.flatten
Expand Down
3 changes: 3 additions & 0 deletions laws/src/main/scala/cats/laws/FunctorFilterLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ trait FunctorFilterLaws[F[_]] {
def filterConsistentWithMapFilter[A](fa: F[A], f: A => Boolean): IsEq[F[A]] =
F.filter(fa)(f) <->
F.mapFilter(fa)(a => if (f(a)) Some(a) else None)

def filterNotConsistentWithFilter[A](fa: F[A], f: A => Boolean): IsEq[F[A]] =
F.filterNot(fa)(f) <-> F.filter(fa)(!f(_))
}

object FunctorFilterLaws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ trait FunctorFilterTests[F[_]] extends Laws {
"mapFilter map consistency" -> forAll(laws.mapFilterMapConsistency[A, B] _),
"collect mapFilter consistency" -> forAll(laws.collectConsistentWithMapFilter[A, B] _),
"flattenOption mapFilter consistency" -> forAll(laws.flattenOptionConsistentWithMapFilter[A] _),
"filter mapFilter consistency" -> forAll(laws.filterConsistentWithMapFilter[A] _)
"filter mapFilter consistency" -> forAll(laws.filterConsistentWithMapFilter[A] _),
"filterNot mapFilter consistency" -> forAll(laws.filterNotConsistentWithFilter[A] _)
)
}

Expand Down

0 comments on commit cc0ba4b

Please sign in to comment.