-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add count as syntax to UnorderedFoldable (#2520)
* 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
1 parent
018cb0f
commit 4178665
Showing
6 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
tests/src/test/scala/cats/tests/UnorderedFoldableSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |