Skip to content

Commit

Permalink
Changed foldMapK to delegate shortcircuiting. Added tests (#3451)
Browse files Browse the repository at this point in the history
  • Loading branch information
barambani authored Jun 2, 2020
1 parent 331f81c commit c70273e
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,9 @@ import scala.annotation.implicitNotFound
* */
@noop
def foldMapK[G[_], A, B](fa: F[A])(f: A => G[B])(implicit G: MonoidK[G]): G[B] =
foldMap(fa)(f)(G.algebra)
foldRight(fa, Eval.now(G.empty[B])) { (a, evalGb) =>
G.combineKEval(f(a), evalGb)
}.value

/**
* Alias for [[foldM]].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class NonEmptyStreamSuite extends CatsSuite {
checkAll("NonEmptyStream[Int]", SemigroupTests[NonEmptyStream[Int]].semigroup)
checkAll("Semigroup[NonEmptyStream[Int]]", SerializableTests.serializable(Semigroup[NonEmptyStream[Int]]))

checkAll("NonEmptyStream[Int]", ShortCircuitingTests[NonEmptyStream].foldable[Int])
checkAll("NonEmptyStream[Int]", ShortCircuitingTests[NonEmptyStream].traverse[Int])

{
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/scala-2.13+/cats/tests/LazyListSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class LazyListSuite extends CatsSuite {
checkAll("LazyList[Int]", AlignTests[LazyList].align[Int, Int, Int, Int])
checkAll("Align[LazyList]", SerializableTests.serializable(Align[LazyList]))

checkAll("LazyList[Int]", ShortCircuitingTests[LazyList].foldable[Int])
checkAll("LazyList[Int]", ShortCircuitingTests[LazyList].traverseFilter[Int])

// Can't test applicative laws as they don't terminate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class NonEmptyLazyListSuite extends NonEmptyCollectionSuite[LazyList, NonEmptyLa
checkAll("NonEmptyLazyList[Int]", AlignTests[NonEmptyLazyList].align[Int, Int, Int, Int])
checkAll("Align[NonEmptyLazyList]", SerializableTests.serializable(Align[NonEmptyLazyList]))

checkAll("NonEmptyLazyList[Int]", ShortCircuitingTests[NonEmptyLazyList].foldable[Int])
checkAll("NonEmptyLazyList[Int]", ShortCircuitingTests[NonEmptyLazyList].traverse[Int])

test("show") {
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/scala/cats/tests/ChainSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ChainSuite extends CatsSuite {
checkAll("Chain[Int]", TraverseFilterTests[Chain].traverseFilter[Int, Int, Int])
checkAll("TraverseFilter[Chain]", SerializableTests.serializable(TraverseFilter[Chain]))

checkAll("Chain[Int]", ShortCircuitingTests[Chain].foldable[Int])
checkAll("Chain[Int]", ShortCircuitingTests[Chain].traverseFilter[Int])

{
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/scala/cats/tests/NonEmptyChainSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class NonEmptyChainSuite extends NonEmptyCollectionSuite[Chain, NonEmptyChain, N
checkAll("NonEmptyChain[Int]", AlignTests[NonEmptyChain].align[Int, Int, Int, Int])
checkAll("Align[NonEmptyChain]", SerializableTests.serializable(Align[NonEmptyChain]))

checkAll("NonEmptyChain[Int]", ShortCircuitingTests[NonEmptyChain].foldable[Int])
checkAll("NonEmptyChain[Int]", ShortCircuitingTests[NonEmptyChain].traverse[Int])

{
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/scala/cats/tests/NonEmptyListSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class NonEmptyListSuite extends NonEmptyCollectionSuite[List, NonEmptyList, NonE

checkAll("ZipNonEmptyList[Int]", CommutativeApplyTests[ZipNonEmptyList].commutativeApply[Int, Int, Int])

checkAll("NonEmptyList[Int]", ShortCircuitingTests[NonEmptyList].foldable[Int])
checkAll("NonEmptyList[Int]", ShortCircuitingTests[NonEmptyList].traverse[Int])

{
Expand Down
2 changes: 2 additions & 0 deletions tests/src/test/scala/cats/tests/NonEmptySetSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class NonEmptySetSuite extends CatsSuite {
PartialOrder[NonEmptySet[ListWrapper[Int]]]
}

checkAll("NonEmptySet[Int]", ShortCircuitingTests[NonEmptySet].foldable[Int])

test("First element is always the smallest") {
forAll { (nes: NonEmptySet[Int]) =>
nes.forall { v =>
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/scala/cats/tests/NonEmptyVectorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class NonEmptyVectorSuite extends NonEmptyCollectionSuite[Vector, NonEmptyVector
checkAll("NonEmptyVector[Int]", BimonadTests[NonEmptyVector].bimonad[Int, Int, Int])
checkAll("Bimonad[NonEmptyVector]", SerializableTests.serializable(Bimonad[NonEmptyVector]))

checkAll("NonEmptyVector[Int]", ShortCircuitingTests[NonEmptyVector].foldable[Int])
checkAll("NonEmptyVector[Int]", ShortCircuitingTests[NonEmptyVector].traverse[Int])

test("size is consistent with toList.size") {
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/scala/cats/tests/QueueSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class QueueSuite extends CatsSuite {
checkAll("Queue[Int]", TraverseFilterTests[Queue].traverseFilter[Int, Int, Int])
checkAll("TraverseFilter[Queue]", SerializableTests.serializable(TraverseFilter[Queue]))

checkAll("Queue[Int]", ShortCircuitingTests[Queue].foldable[Int])
checkAll("Queue[Int]", ShortCircuitingTests[Queue].traverseFilter[Int])

test("show") {
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/scala/cats/tests/SortedMapSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class SortedMapSuite extends CatsSuite {
checkAll("SortedMap[Int, Int]", AlignTests[SortedMap[Int, *]].align[Int, Int, Int, Int])
checkAll("Align[SortedMap[Int, *]]", SerializableTests.serializable(Align[SortedMap[Int, *]]))

checkAll("SortedMap[Int, *]", ShortCircuitingTests[SortedMap[Int, *]].foldable[Int])
checkAll("SortedMap[Int, *]", ShortCircuitingTests[SortedMap[Int, *]].traverseFilter[Int])

test("show isn't empty and is formatted as expected") {
Expand Down
2 changes: 2 additions & 0 deletions tests/src/test/scala/cats/tests/SortedSetSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class SortedSetSuite extends CatsSuite {
)
checkAll("Hash[SortedSet[Int]]", HashTests[SortedSet[Int]].hash)

checkAll("SortedSet[Int]", ShortCircuitingTests[SortedSet].foldable[Int])

test("show keeps separate entries for items that map to identical strings") {
// note: this val name has to be the same to shadow the cats.instances instance
implicit val catsStdShowForInt: Show[Int] = Show.show(_ => "1")
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/scala/cats/tests/StreamSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class StreamSuite extends CatsSuite {
checkAll("Stream[Int]", AlignTests[Stream].align[Int, Int, Int, Int])
checkAll("Align[Stream]", SerializableTests.serializable(Align[Stream]))

checkAll("Stream[Int]", ShortCircuitingTests[Stream].foldable[Int])
checkAll("Stream[Int]", ShortCircuitingTests[Stream].traverseFilter[Int])

// Can't test applicative laws as they don't terminate
Expand Down

0 comments on commit c70273e

Please sign in to comment.