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

Add foldRightDefer to Foldable #2772

Merged
merged 8 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
18 changes: 14 additions & 4 deletions core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ import Foldable.sentinel
*/
def foldRight[A, B](fa: F[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B]

def foldRightDefer[G[_]: Defer, A, B](fa: F[A], gb: G[B])(fn: (A, G[B]) => G[B]): G[B] =
Defer[G].defer(
this.foldLeft(fa, (z: G[B]) => z) { (acc, elem) => z =>
Defer[G].defer(acc(fn(elem, z)))
}(gb)
)

def reduceLeftToOption[A, B](fa: F[A])(f: A => B)(g: (B, A) => B): Option[B] =
foldLeft(fa, Option.empty[B]) {
case (Some(b), a) => Some(g(b, a))
Expand Down Expand Up @@ -587,11 +594,14 @@ import Foldable.sentinel
object Foldable {
private val sentinel: Function1[Any, Any] = new scala.runtime.AbstractFunction1[Any, Any] { def apply(a: Any) = this }

def iterateRight[A, B](iterable: Iterable[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = {
def loop(it: Iterator[A]): Eval[B] =
Eval.defer(if (it.hasNext) f(it.next, loop(it)) else lb)
def iterateRight[A, B](iterable: Iterable[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
iterateRightDefer(iterable, lb)(f)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would imagine inlining Eval.defer will give a slight performance improvement since the jit can at best do this inlining later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johnynek not sure I understand. Are you saying you would prefer the previous implementation explicitly calling Eval.defer?


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))

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

/**
Expand Down
11 changes: 11 additions & 0 deletions laws/src/main/scala/cats/laws/FoldableLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ trait FoldableLaws[F[_]] extends UnorderedFoldableLaws[F] {
): IsEq[B] =
F.foldM[Id, A, B](fa, b)(f) <-> F.foldLeft(fa, b)(f)

def foldRightDeferConsistentWithFoldRight[A, B](
fa: F[A],
f: (B, A) => B
)(implicit
M: Monoid[B]): IsEq[B] = {
val g: (A, Eval[B]) => Eval[B] = (a, ea) => ea.map(f(_, a))

F.foldRight(fa, Later(M.empty))(g).value <-> F.foldRightDefer(fa, Later(M.empty): Eval[B])(g).value
}

/**
* `reduceLeftOption` consistent with `reduceLeftToOption`
*/
Expand Down Expand Up @@ -111,6 +121,7 @@ trait FoldableLaws[F[_]] extends UnorderedFoldableLaws[F] {
def orderedConsistency[A: Eq](x: F[A], y: F[A])(implicit ev: Eq[F[A]]): IsEq[List[A]] =
if (x === y) (F.toList(x) <-> F.toList(y))
else List.empty[A] <-> List.empty[A]

}

object FoldableLaws {
Expand Down
3 changes: 2 additions & 1 deletion laws/src/main/scala/cats/laws/discipline/FoldableTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ trait FoldableTests[F[_]] extends UnorderedFoldableTests[F] {
"takeWhile_ reference" -> forAll(laws.takeWhile_Ref[A] _),
"dropWhile_ reference" -> forAll(laws.dropWhile_Ref[A] _),
"collectFirstSome reference" -> forAll(laws.collectFirstSome_Ref[A, B] _),
"collectFirst reference" -> forAll(laws.collectFirst_Ref[A, B] _)
"collectFirst reference" -> forAll(laws.collectFirst_Ref[A, B] _),
"foldRightDefer consistency" -> forAll(laws.foldRightDeferConsistentWithFoldRight[A, B] _)
)
}

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 @@ -282,6 +282,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 = F.foldRightDefer(large, 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] = F.foldRightDefer(large, boom[Int])((elem, acc) => acc.map(_ + elem))
}

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