Skip to content

Commit

Permalink
Add count as syntax to UnorderedFoldable (#2520)
Browse files Browse the repository at this point in the history
* Add count_ as syntax to UnorderedFoldable

* Change method name count_ to count in UnorderedFoldable

* Fire travis build

* Add unorederedFoldable to syntax package object
  • Loading branch information
leusgalvan authored and Luka Jacobowitz committed Sep 25, 2018
1 parent 018cb0f commit 4178665
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/src/main/scala/cats/implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ object implicits
with syntax.AllSyntaxBinCompat0
with syntax.AllSyntaxBinCompat1
with syntax.AllSyntaxBinCompat2
with syntax.AllSyntaxBinCompat3
with instances.AllInstances
with instances.AllInstancesBinCompat0
with instances.AllInstancesBinCompat1
4 changes: 4 additions & 0 deletions core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ abstract class AllSyntaxBinCompat
with AllSyntaxBinCompat0
with AllSyntaxBinCompat1
with AllSyntaxBinCompat2
with AllSyntaxBinCompat3

trait AllSyntax
extends AlternativeSyntax
Expand Down Expand Up @@ -77,3 +78,6 @@ trait AllSyntaxBinCompat2
with EitherSyntaxBinCompat0
with ListSyntaxBinCompat0
with ValidatedSyntaxBincompat0

trait AllSyntaxBinCompat3
extends UnorderedFoldableSyntax
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ package object syntax {
object traverse extends TraverseSyntax
object traverseFilter extends TraverseFilterSyntax
object nonEmptyTraverse extends NonEmptyTraverseSyntax
object unorderedFoldable extends UnorderedFoldableSyntax
object unorderedTraverse extends UnorderedTraverseSyntax
object validated extends ValidatedSyntax with ValidatedExtensionSyntax with ValidatedSyntaxBincompat0
object vector extends VectorSyntax
Expand Down
31 changes: 31 additions & 0 deletions core/src/main/scala/cats/syntax/unorderedFoldable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cats
package syntax

import cats.instances.long._

trait UnorderedFoldableSyntax extends UnorderedFoldable.ToUnorderedFoldableOps {
implicit final def catsSyntaxUnorderedFoldableOps[F[_]: UnorderedFoldable, A](fa: F[A]): UnorderedFoldableOps[F, A] =
new UnorderedFoldableOps[F, A](fa)
}

final class UnorderedFoldableOps[F[_], A](val fa: F[A]) extends AnyVal {
/**
* Count the number of elements in the structure that satisfy the given predicate.
*
* For example:
* {{{
* scala> import cats.implicits._
* scala> val map1 = Map[Int, String]()
* scala> val p1: String => Boolean = _.length > 0
* scala> map1.count(p1)
* res0: Long = 0
*
* scala> val map2 = Map(1 -> "hello", 2 -> "world", 3 -> "!")
* scala> val p2: String => Boolean = _.length > 1
* scala> map2.count(p2)
* res1: Long = 2
* }}}
*/
def count(p: A => Boolean)(implicit F: UnorderedFoldable[F]): Long =
F.unorderedFoldMap(fa)(a => if (p(a)) 1L else 0L)
}
4 changes: 2 additions & 2 deletions testkit/src/main/scala/cats/tests/CatsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tests

import catalysts.Platform
import cats.instances.{AllInstances, AllInstancesBinCompat0, AllInstancesBinCompat1}
import cats.syntax.{AllSyntax, AllSyntaxBinCompat0, AllSyntaxBinCompat1, AllSyntaxBinCompat2, EqOps}
import cats.syntax.{AllSyntax, AllSyntaxBinCompat0, AllSyntaxBinCompat1, AllSyntaxBinCompat2, AllSyntaxBinCompat3, EqOps}
import org.scalactic.anyvals.{PosInt, PosZDouble, PosZInt}
import org.scalatest.{FunSuite, FunSuiteLike, Matchers}
import org.scalatest.prop.{Configuration, GeneratorDrivenPropertyChecks}
Expand Down Expand Up @@ -35,7 +35,7 @@ trait CatsSuite extends FunSuite
with TestSettings
with AllInstances with AllInstancesBinCompat0 with AllInstancesBinCompat1
with AllSyntax with AllSyntaxBinCompat0 with AllSyntaxBinCompat1
with AllSyntaxBinCompat2
with AllSyntaxBinCompat2 with AllSyntaxBinCompat3
with StrictCatsEquality { self: FunSuiteLike =>

implicit override val generatorDrivenConfig: PropertyCheckConfiguration =
Expand Down
26 changes: 26 additions & 0 deletions tests/src/test/scala/cats/tests/UnorderedFoldableSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cats
package tests

import org.scalatest.prop.PropertyChecks
import org.scalacheck.Arbitrary
import cats.instances.all._

abstract class UnorderedFoldableSuite[F[_]: UnorderedFoldable](name: String)(
implicit ArbFString: Arbitrary[F[String]]) extends CatsSuite with PropertyChecks {

def iterator[T](fa: F[T]): Iterator[T]

test(s"UnorderedFoldable[$name].count") {
forAll { (fa: F[String], p: String => Boolean) =>
fa.count(p) === iterator(fa).count(p).toLong
}
}
}

class UnorderedFoldableSetSuite extends UnorderedFoldableSuite[Set]("set") {
def iterator[T](set: Set[T]): Iterator[T] = set.iterator
}

class UnorderedFoldableMapSuite extends UnorderedFoldableSuite[Map[String, ?]]("map") {
def iterator[T](map: Map[String, T]): Iterator[T] = map.valuesIterator
}

0 comments on commit 4178665

Please sign in to comment.