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

Backport #2772 add foldRightDefer to Foldable #3338

Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cats

import scala.collection.mutable
import cats.Foldable.sentinel
import cats.instances.either._
import cats.kernel.CommutativeMonoid
import simulacrum.typeclass
import Foldable.sentinel

import scala.collection.mutable

/**
* Data structures that can be folded to a summary value.
Expand Down Expand Up @@ -594,6 +595,13 @@ object Foldable {
Eval.always(iterable.iterator).flatMap(loop)
}

def iterateRightDefer[G[_]: Defer, A, B](iterable: Iterable[A], lb: G[B])(f: (A, G[B]) => G[B]): G[B] = {
def loop(it: Iterator[A]): G[B] =
Defer[G].defer(if (it.hasNext) f(it.next(), Defer[G].defer(loop(it))) else Defer[G].defer(lb))

Defer[G].defer(loop(iterable.iterator))
}

/**
* Isomorphic to
*
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/scala/cats/syntax/foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,16 @@ final class FoldableOps0[F[_], A](private val fa: F[A]) extends AnyVal {
*/
def maximumByOption[B: Order](f: A => B)(implicit F: Foldable[F]): Option[A] =
F.maximumOption(fa)(Order.by(f))

/**
* Implementers are responsible for ensuring they maintain consistency with foldRight; this is not checked by laws on Scala 2.11
*/
def foldRightDefer[G[_]: Defer, B](gb: G[B])(fn: (A, G[B]) => G[B])(implicit F: Foldable[F]): G[B] =
Defer[G].defer(
F.foldLeft(fa, (z: G[B]) => z)(
(acc, elem) => z => Defer[G].defer(acc(fn(elem, z)))
)(gb)
)
}

final private[syntax] class FoldableOps1[F[_]](private val F: Foldable[F]) extends AnyVal {
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 @@ -296,6 +296,13 @@ class FoldableSuiteAdditional extends CatsSuite {
// safely build large lists
val larger = F.foldRight(large, Now(List.empty[Int]))((x, lxs) => lxs.map((x + 1) :: _))
larger.value should ===(large.map(_ + 1))

val sum = large.foldRightDefer(Eval.later(0))((elem, acc) => acc.map(_ + elem))
sum.value should ===(large.sum)

def boom[A]: Eval[A] = Eval.later(sys.error("boom"))
// Ensure that the lazy param is actually handled lazily
val lazySum: Eval[Int] = large.foldRightDefer(boom[Int])((elem, acc) => acc.map(_ + elem))
}

def checkMonadicFoldsStackSafety[F[_]](fromRange: Range => F[Int])(implicit F: Foldable[F]): Unit = {
Expand Down