Skip to content

Commit

Permalink
Rename Functor and Traverse Empty to Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Luka Jacobowitz committed Aug 28, 2018
1 parent 8255d59 commit 4b65c9a
Show file tree
Hide file tree
Showing 35 changed files with 110 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package cats
import simulacrum.typeclass

/**
* `FunctorEmpty[F]` allows you to `map` and filter out elements simultaneously.
* `FunctorFilter[F]` allows you to `map` and filter out elements simultaneously.
*/
@typeclass
trait FunctorEmpty[F[_]] extends Serializable {
trait FunctorFilter[F[_]] extends Serializable {
def functor: Functor[F]

def mapFilter[A, B](fa: F[A])(f: A => Option[B]): F[B]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package cats
import simulacrum.typeclass

/**
* `TraverseEmpty`, also known as `Witherable`, represents list-like structures
* `TraverseFilter`, also known as `Witherable`, represents list-like structures
* that can essentially have a `traverse` and a `filter` applied as a single
* combined operation (`traverseFilter`).
*
* Based on Haskell's [[https://hackage.haskell.org/package/witherable-0.1.3.3/docs/Data-Witherable.html Data.Witherable]]
*/

@typeclass
trait TraverseEmpty[F[_]] extends FunctorEmpty[F] {
trait TraverseFilter[F[_]] extends FunctorFilter[F] {
def traverse: Traverse[F]

final override def functor: Functor[F] = traverse
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/data/Chain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ private[data] sealed abstract class ChainInstances extends ChainInstances1 {
}
}

implicit val catsDataTraverseEmptyForChain: TraverseEmpty[Chain] = new TraverseEmpty[Chain] {
implicit val catsDataTraverseFilterForChain: TraverseFilter[Chain] = new TraverseFilter[Chain] {
def traverse: Traverse[Chain] = Chain.catsDataInstancesForChain

override def filter[A](fa: Chain[A])(f: A => Boolean): Chain[A] = fa.filter(f)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/data/Const.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private[data] sealed abstract class ConstInstances extends ConstInstances0 {
fa.traverse(f)
}

implicit def catsDataTraverseEmptyForConst[C]: TraverseEmpty[Const[C, ?]] = new TraverseEmpty[Const[C, ?]] {
implicit def catsDataTraverseFilterForConst[C]: TraverseFilter[Const[C, ?]] = new TraverseFilter[Const[C, ?]] {

override def mapFilter[A, B](fa: Const[C, A])(f: (A) => Option[B]): Const[C, B] = fa.retag

Expand Down
14 changes: 7 additions & 7 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,9 @@ private[data] abstract class EitherTInstances extends EitherTInstances1 {
EitherT(F.defer(fa.value))
}

implicit def catsDataTraverseEmptyForEitherT[F[_], L](implicit F0: TraverseEmpty[F]): TraverseEmpty[EitherT[F, L, ?]] =
new EitherTFunctorEmpty[F, L] with TraverseEmpty[EitherT[F, L, ?]] {
implicit def F: FunctorEmpty[F] = F0
implicit def catsDataTraverseFilterForEitherT[F[_], L](implicit F0: TraverseFilter[F]): TraverseFilter[EitherT[F, L, ?]] =
new EitherTFunctorFilter[F, L] with TraverseFilter[EitherT[F, L, ?]] {
implicit def F: FunctorFilter[F] = F0
def traverse: Traverse[EitherT[F, L, ?]] = catsDataTraverseForEitherT[F, L](F0.traverse)

def traverseFilter[G[_], A, B]
Expand Down Expand Up @@ -560,8 +560,8 @@ private[data] abstract class EitherTInstances1 extends EitherTInstances2 {
fa.ensureOr(error)(predicate)(F)
}

implicit def catsDataFunctorEmptyForEitherT[F[_], L](implicit F0: FunctorEmpty[F]): FunctorEmpty[EitherT[F, L, ?]] =
new EitherTFunctorEmpty[F, L] { implicit def F = F0 }
implicit def catsDataFunctorFilterForEitherT[F[_], L](implicit F0: FunctorFilter[F]): FunctorFilter[EitherT[F, L, ?]] =
new EitherTFunctorFilter[F, L] { implicit def F = F0 }
}

private[data] abstract class EitherTInstances2 extends EitherTInstances3 {
Expand Down Expand Up @@ -722,8 +722,8 @@ private[data] sealed trait EitherTOrder[F[_], L, A] extends Order[EitherT[F, L,
override def compare(x: EitherT[F, L, A], y: EitherT[F, L, A]): Int = x compare y
}

private[data] sealed trait EitherTFunctorEmpty[F[_], E] extends FunctorEmpty[EitherT[F, E, ?]] {
implicit def F: FunctorEmpty[F]
private[data] sealed trait EitherTFunctorFilter[F[_], E] extends FunctorFilter[EitherT[F, E, ?]] {
implicit def F: FunctorFilter[F]

override def functor: Functor[EitherT[F, E, ?]] = EitherT.catsDataFunctorForEitherT[F, E](F.functor)

Expand Down
10 changes: 5 additions & 5 deletions core/src/main/scala/cats/data/Nested.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ private[data] sealed abstract class NestedInstances extends NestedInstances0 {
Nested(F.defer(fa.value))
}

implicit def catsDataTraverseEmptyForNested[F[_], G[_]](implicit F0: Traverse[F], G0: TraverseEmpty[G]): TraverseEmpty[Nested[F, G, ?]] =
new NestedTraverseEmpty[F, G] {
implicit def catsDataTraverseFilterForNested[F[_], G[_]](implicit F0: Traverse[F], G0: TraverseFilter[G]): TraverseFilter[Nested[F, G, ?]] =
new NestedTraverseFilter[F, G] {
implicit val F: Traverse[F] = F0
implicit val G: TraverseEmpty[G] = G0
implicit val G: TraverseFilter[G] = G0
}


Expand Down Expand Up @@ -323,10 +323,10 @@ private[data] trait NestedInvariantSemigroupalApply[F[_], G[_]] extends Invarian
Nested(FG.product(fa.value, fb.value))
}

private[data] abstract class NestedTraverseEmpty[F[_], G[_]] extends TraverseEmpty[Nested[F, G, ?]] {
private[data] abstract class NestedTraverseFilter[F[_], G[_]] extends TraverseFilter[Nested[F, G, ?]] {
implicit val F: Traverse[F]

implicit val G: TraverseEmpty[G]
implicit val G: TraverseFilter[G]

def traverse: Traverse[Nested[F, G, ?]] = Nested.catsDataTraverseForNested(F, G.traverse)

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/data/OptionT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ private[data] sealed abstract class OptionTInstances extends OptionTInstances0 {
OptionT(F.defer(fa.value))
}

implicit def optionTFunctorEmpty[F[_]: Functor]: FunctorEmpty[OptionT[F, ?]] = {
new FunctorEmpty[OptionT[F, ?]] {
implicit def optionTFunctorFilter[F[_]: Functor]: FunctorFilter[OptionT[F, ?]] = {
new FunctorFilter[OptionT[F, ?]] {
override val functor: Functor[OptionT[F, ?]] = OptionT.catsDataFunctorForOptionT[F]

override def mapFilter[A, B](fa: OptionT[F, A])(f: (A) => Option[B]): OptionT[F, B] = fa.subflatMap(f)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/list.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ trait ListInstances extends cats.kernel.instances.ListInstances {
}

trait ListInstancesBinCompat0 {
implicit val catsStdTraverseEmptyForList: TraverseEmpty[List] = new TraverseEmpty[List] {
implicit val catsStdTraverseFilterForList: TraverseFilter[List] = new TraverseFilter[List] {
val traverse: Traverse[List] = cats.instances.list.catsStdInstancesForList

override def mapFilter[A, B](fa: List[A])(f: (A) => Option[B]): List[B] = fa.collect(Function.unlift(f))
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/instances/map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ trait MapInstancesBinCompat0 {
}
}

implicit def catsStdFunctorEmptyForMap[K]: FunctorEmpty[Map[K, ?]] = {
new FunctorEmpty[Map[K, ?]] {
implicit def catsStdFunctorFilterForMap[K]: FunctorFilter[Map[K, ?]] = {
new FunctorFilter[Map[K, ?]] {

val functor: Functor[Map[K, ?]] = cats.instances.map.catsStdInstancesForMap[K]

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/option.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ trait OptionInstances extends cats.kernel.instances.OptionInstances {
}

trait OptionInstancesBinCompat0 {
implicit val catsStdTraverseEmptyForOption: TraverseEmpty[Option] = new TraverseEmpty[Option] {
implicit val catsStdTraverseFilterForOption: TraverseFilter[Option] = new TraverseFilter[Option] {
val traverse: Traverse[Option] = cats.instances.option.catsStdInstancesForOption

override def mapFilter[A, B](fa: Option[A])(f: (A) => Option[B]): Option[B] = fa.flatMap(f)
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/cats/instances/sortedMap.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cats.instances

import cats.{Always, Applicative, Eval, FlatMap, Foldable, Monoid, Order, Show, Traverse, TraverseEmpty}
import cats.{Always, Applicative, Eval, FlatMap, Foldable, Monoid, Order, Show, Traverse, TraverseFilter}
import cats.kernel._
import cats.kernel.instances.StaticMethods

Expand Down Expand Up @@ -168,8 +168,8 @@ class SortedMapMonoid[K, V](implicit V: Semigroup[V], O: Order[K]) extends Monoi
}

trait SortedMapInstancesBinCompat0 {
implicit def catsStdTraverseEmptyForSortedMap[K: Order]: TraverseEmpty[SortedMap[K, ?]] =
new TraverseEmpty[SortedMap[K, ?]] {
implicit def catsStdTraverseFilterForSortedMap[K: Order]: TraverseFilter[SortedMap[K, ?]] =
new TraverseFilter[SortedMap[K, ?]] {

implicit val ordering: Ordering[K] = Order[K].toOrdering

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ trait StreamInstances extends cats.kernel.instances.StreamInstances {
}

trait StreamInstancesBinCompat0 {
implicit val catsStdTraverseEmptyForStream: TraverseEmpty[Stream] = new TraverseEmpty[Stream] {
implicit val catsStdTraverseFilterForStream: TraverseFilter[Stream] = new TraverseFilter[Stream] {
val traverse: Traverse[Stream] = cats.instances.stream.catsStdInstancesForStream

override def mapFilter[A, B](fa: Stream[A])(f: (A) => Option[B]): Stream[B] = {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/vector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ trait VectorInstances extends cats.kernel.instances.VectorInstances {
}

trait VectorInstancesBinCompat0 {
implicit val catsStdTraverseEmptyForVector: TraverseEmpty[Vector] = new TraverseEmpty[Vector] {
implicit val catsStdTraverseFilterForVector: TraverseFilter[Vector] = new TraverseFilter[Vector] {
val traverse: Traverse[Vector] = cats.instances.vector.catsStdInstancesForVector

override def mapFilter[A, B](fa: Vector[A])(f: (A) => Option[B]): Vector[B] =
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ trait AllSyntaxBinCompat1

trait AllSyntaxBinCompat2
extends ParallelTraverseSyntax
with TraverseEmptySyntax
with FunctorEmptySyntax
with TraverseFilterSyntax
with FunctorFilterSyntax
4 changes: 0 additions & 4 deletions core/src/main/scala/cats/syntax/functorEmpty.scala

This file was deleted.

4 changes: 4 additions & 0 deletions core/src/main/scala/cats/syntax/functorFilter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cats
package syntax

trait FunctorFilterSyntax extends FunctorFilter.ToFunctorFilterOps
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ package object syntax {
object flatMap extends FlatMapSyntax
object foldable extends FoldableSyntax
object functor extends FunctorSyntax
object functorEmpty extends FunctorEmptySyntax
object functorFilter extends FunctorFilterSyntax
object group extends GroupSyntax
object invariant extends InvariantSyntax
object ior extends IorSyntax
Expand All @@ -50,7 +50,7 @@ package object syntax {
object show extends ShowSyntax
object strong extends StrongSyntax
object traverse extends TraverseSyntax
object traverseEmpty extends TraverseEmptySyntax
object traverseFilter extends TraverseFilterSyntax
object nonEmptyTraverse extends NonEmptyTraverseSyntax
object unorderedTraverse extends UnorderedTraverseSyntax
object validated extends ValidatedSyntax with ValidatedExtensionSyntax
Expand Down
4 changes: 0 additions & 4 deletions core/src/main/scala/cats/syntax/traverseEmpty.scala

This file was deleted.

4 changes: 4 additions & 0 deletions core/src/main/scala/cats/syntax/traverseFilter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cats
package syntax

trait TraverseFilterSyntax extends TraverseFilter.ToTraverseFilterOps
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cats
package laws


trait FunctorEmptyLaws[F[_]] {
implicit def F: FunctorEmpty[F]
trait FunctorFilterLaws[F[_]] {
implicit def F: FunctorFilter[F]


implicit def functor: Functor[F] = F.functor
Expand Down Expand Up @@ -32,7 +32,7 @@ trait FunctorEmptyLaws[F[_]] {
}
}

object FunctorEmptyLaws {
def apply[F[_]](implicit ev: FunctorEmpty[F]): FunctorEmptyLaws[F] =
new FunctorEmptyLaws[F] { def F: FunctorEmpty[F] = ev }
object FunctorFilterLaws {
def apply[F[_]](implicit ev: FunctorFilter[F]): FunctorFilterLaws[F] =
new FunctorFilterLaws[F] { def F: FunctorFilter[F] = ev }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import cats.data.Nested
import cats.syntax.all._
import cats.instances.option._

trait TraverseEmptyLaws[F[_]] extends FunctorEmptyLaws[F] {
implicit override def F: TraverseEmpty[F]
trait TraverseFilterLaws[F[_]] extends FunctorFilterLaws[F] {
implicit override def F: TraverseFilter[F]

def traverseFilterIdentity[G[_]: Applicative, A](fa: F[A]): IsEq[G[F[A]]] = {
fa.traverseFilter(_.some.pure[G]) <-> fa.pure[G]
Expand Down Expand Up @@ -35,7 +35,7 @@ trait TraverseEmptyLaws[F[_]] extends FunctorEmptyLaws[F] {
}
}

object TraverseEmptyLaws {
def apply[F[_]](implicit ev: TraverseEmpty[F]): TraverseEmptyLaws[F] =
new TraverseEmptyLaws[F] { def F: TraverseEmpty[F] = ev }
object TraverseFilterLaws {
def apply[F[_]](implicit ev: TraverseFilter[F]): TraverseFilterLaws[F] =
new TraverseFilterLaws[F] { def F: TraverseFilter[F] = ev }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import org.scalacheck.Prop.forAll
import org.scalacheck.Arbitrary
import org.typelevel.discipline.Laws

trait FunctorEmptyTests[F[_]] extends Laws {
def laws: FunctorEmptyLaws[F]
trait FunctorFilterTests[F[_]] extends Laws {
def laws: FunctorFilterLaws[F]

def functorEmpty[A, B, C](implicit
def functorFilter[A, B, C](implicit
ArbFA: Arbitrary[F[A]],
ArbFABoo: Arbitrary[PartialFunction[A, B]],
ArbFOA: Arbitrary[F[Option[A]]],
Expand All @@ -22,7 +22,7 @@ trait FunctorEmptyTests[F[_]] extends Laws {
EqFC: Eq[F[C]]
): RuleSet = {
new DefaultRuleSet(
name = "functorEmpty",
name = "functorFilter",
parent = None,
"mapFilter composition" -> forAll(laws.mapFilterComposition[A, B, C] _),
"mapFilter map consistency" -> forAll(laws.mapFilterMapConsistency[A, B] _),
Expand All @@ -33,7 +33,7 @@ trait FunctorEmptyTests[F[_]] extends Laws {
}
}

object FunctorEmptyTests {
def apply[F[_]: FunctorEmpty]: FunctorEmptyTests[F] =
new FunctorEmptyTests[F] { def laws: FunctorEmptyLaws[F] = FunctorEmptyLaws[F] }
object FunctorFilterTests {
def apply[F[_]: FunctorFilter]: FunctorFilterTests[F] =
new FunctorFilterTests[F] { def laws: FunctorFilterLaws[F] = FunctorFilterLaws[F] }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import org.scalacheck.Prop.forAll
import org.scalacheck.Arbitrary
import cats.instances.option._

trait TraverseEmptyTests[F[_]] extends FunctorEmptyTests[F] {
def laws: TraverseEmptyLaws[F]
trait TraverseFilterTests[F[_]] extends FunctorFilterTests[F] {
def laws: TraverseFilterLaws[F]

def traverseEmpty[A, B, C](implicit
def traverseFilter[A, B, C](implicit
ArbFA: Arbitrary[F[A]],
ArbFOA: Arbitrary[F[Option[A]]],
ArbFABoo: Arbitrary[PartialFunction[A, B]],
Expand All @@ -29,8 +29,8 @@ trait TraverseEmptyTests[F[_]] extends FunctorEmptyTests[F] {
EqMNFC: Eq[Nested[Option, Option, F[C]]]
): RuleSet = {
new DefaultRuleSet(
name = "traverseEmpty",
parent = Some(functorEmpty[A, B, C]),
name = "traverseFilter",
parent = Some(functorFilter[A, B, C]),
"traverseFilter identity" -> forAll(laws.traverseFilterIdentity[Option, A] _),
"traverseFilter nested composition" -> forAll(laws.traverseFilterComposition[A, B, C, Option, Option] _),
"traverseFilter consistent with traverse" -> forAll(laws.traverseFilterConsistentWithTraverse[Option, A] _),
Expand All @@ -39,7 +39,7 @@ trait TraverseEmptyTests[F[_]] extends FunctorEmptyTests[F] {
}
}

object TraverseEmptyTests {
def apply[F[_]: TraverseEmpty]: TraverseEmptyTests[F] =
new TraverseEmptyTests[F] { def laws: TraverseEmptyLaws[F] = TraverseEmptyLaws[F] }
object TraverseFilterTests {
def apply[F[_]: TraverseFilter]: TraverseFilterTests[F] =
new TraverseFilterTests[F] { def laws: TraverseFilterLaws[F] = TraverseFilterLaws[F] }
}
6 changes: 3 additions & 3 deletions tests/src/test/scala/cats/tests/ChainSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cats
package tests

import cats.data.Chain
import cats.laws.discipline.{AlternativeTests, CoflatMapTests, MonadTests, SerializableTests, TraverseEmptyTests, TraverseTests}
import cats.laws.discipline.{AlternativeTests, CoflatMapTests, MonadTests, SerializableTests, TraverseFilterTests, TraverseTests}
import cats.kernel.laws.discipline.{EqTests, MonoidTests, OrderTests, PartialOrderTests}
import cats.laws.discipline.arbitrary._

Expand All @@ -26,8 +26,8 @@ class ChainSuite extends CatsSuite {
checkAll("Order[Chain]", SerializableTests.serializable(Order[Chain[Int]]))


checkAll("Chain[Int]", TraverseEmptyTests[Chain].traverseEmpty[Int, Int, Int])
checkAll("TraverseEmpty[Chain]", SerializableTests.serializable(TraverseEmpty[Chain]))
checkAll("Chain[Int]", TraverseFilterTests[Chain].traverseFilter[Int, Int, Int])
checkAll("TraverseFilter[Chain]", SerializableTests.serializable(TraverseFilter[Chain]))

{
implicit val partialOrder = ListWrapper.partialOrder[Int]
Expand Down
4 changes: 2 additions & 2 deletions tests/src/test/scala/cats/tests/ConstSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ConstSuite extends CatsSuite {
checkAll("Const[String, Int] with Option", TraverseTests[Const[String, ?]].traverse[Int, Int, Int, Int, Option, Option])
checkAll("Traverse[Const[String, ?]]", SerializableTests.serializable(Traverse[Const[String, ?]]))

checkAll("Const[String, Int]", TraverseEmptyTests[Const[String, ?]].traverseEmpty[Int, Int, Int])
checkAll("TraverseEmpty[Const[String, ?]]", SerializableTests.serializable(TraverseEmpty[Const[String, ?]]))
checkAll("Const[String, Int]", TraverseFilterTests[Const[String, ?]].traverseFilter[Int, Int, Int])
checkAll("TraverseFilter[Const[String, ?]]", SerializableTests.serializable(TraverseFilter[Const[String, ?]]))

// Get Apply[Const[C : Semigroup, ?]], not Applicative[Const[C : Monoid, ?]]
{
Expand Down
Loading

0 comments on commit 4b65c9a

Please sign in to comment.