Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix foldA, reduceA, and reduceMapA short-circuiting #3199

Merged
merged 5 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ import Foldable.sentinel
* `noop` usage description [[https://github.com/typelevel/simulacrum/issues/162 here]]
*/
@noop def foldA[G[_], A](fga: F[G[A]])(implicit G: Applicative[G], A: Monoid[A]): G[A] =
fold(fga)(Applicative.monoid)
foldMapA(fga)(identity)

/**
* Fold implemented by mapping `A` values into `B` in a context `G` and then
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/Reducible.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ import simulacrum.{noop, typeclass}
* `noop` usage description [[https://github.com/typelevel/simulacrum/issues/162 here]]
*/
@noop def reduceA[G[_], A](fga: F[G[A]])(implicit G: Apply[G], A: Semigroup[A]): G[A] =
reduce(fga)(Apply.semigroup)
reduceMapA(fga)(identity)

/**
* Apply `f` to each `a` of `fa` and combine the result into Apply[G] using the
* given `Semigroup[B]`.
*/
def reduceMapA[G[_], A, B](fa: F[A])(f: A => G[B])(implicit G: Apply[G], B: Semigroup[B]): G[B] =
reduceLeftTo(fa)(f)((gb, a) => G.map2(gb, f(a))(B.combine))
reduceRightTo(fa)(f)((a, egb) => G.map2Eval(f(a), egb)(B.combine)).value

/**
* Monadic reducing by mapping the `A` values to `G[B]`. combining
Expand Down
7 changes: 7 additions & 0 deletions tests/src/test/scala/cats/tests/FoldableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,13 @@ class FoldableSuiteAdditional extends CatsSuite with ScalaVersionSpecificFoldabl
assert(ns.foldA == "boom!!!".asLeft[Int])
}

test(".foldA short-circuiting") {
implicit val F = foldableStreamWithDefaultImpl
val ns = Stream.from(1).map(n => if (n >= 100000) Left(n) else Right(n))

assert(F.foldA(ns) === Left(100000))
}

test(".foldLeftM short-circuiting") {
implicit val F = foldableStreamWithDefaultImpl
val ns = Stream.continually(1)
Expand Down
14 changes: 14 additions & 0 deletions tests/src/test/scala/cats/tests/ReducibleSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ class ReducibleSuiteAdditional extends CatsSuite {

assert(xs.reduceMapM(i => if (i < n) Right(i) else Left(i)) === Left(n))
}

test("reduceMapA should be stack-safe and short-circuiting if reduceRightTo is sufficiently lazy") {
val n = 100000
val xs = NES(0, Stream.from(1))

assert(xs.reduceMapA(i => if (i < n) Right(i) else Left(i)) === Left(n))
}

test("reduceA should be stack-safe and short-circuiting if reduceRightTo is sufficiently lazy") {
val n = 100000
val xs = NES(Right(0), Stream.from(1).map(i => if (i < n) Right(i) else Left(i)))

assert(xs.reduceA === Left(n))
}
}

abstract class ReducibleSuite[F[_]: Reducible](name: String)(implicit ArbFInt: Arbitrary[F[Int]],
Expand Down