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

Avoid all evaluation of NonEmptyLazyList#reduceRightTo #3573

Merged
merged 1 commit into from
Aug 20, 2020
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
9 changes: 6 additions & 3 deletions core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,12 @@ sealed abstract private[data] class NonEmptyLazyListInstances extends NonEmptyLa
def reduceLeftTo[A, B](fa: NonEmptyLazyList[A])(f: A => B)(g: (B, A) => B): B = fa.reduceLeftTo(f)(g)

def reduceRightTo[A, B](fa: NonEmptyLazyList[A])(f: A => B)(g: (A, cats.Eval[B]) => cats.Eval[B]): cats.Eval[B] =
Eval.defer(fa.reduceRightTo(a => Eval.later(f(a))) { (a, b) =>
Eval.defer(g(a, b))
})
fa.tail match {
case LazyList() => Eval.later(f(fa.head))
case head +: tail =>
val nell = NonEmptyLazyList.fromLazyListPrepend(head, tail)
g(fa.head, Eval.defer(reduceRightTo(nell)(f)(g)))
}

private val alignInstance = Align[LazyList].asInstanceOf[Align[NonEmptyLazyList]]

Expand Down
8 changes: 3 additions & 5 deletions core/src/main/scala/cats/instances/function.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,9 @@ sealed private[instances] trait Function1Instances extends Function1Instances0 {
def unit: Unit => R = Function.const(Monoid[R].empty)
def contramap[A, B](fa: A => R)(f: B => A): B => R =
fa.compose(f)
def product[A, B](fa: A => R, fb: B => R): ((A, B)) => R =
(ab: (A, B)) =>
ab match {
case (a, b) => Monoid[R].combine(fa(a), fb(b))
}
def product[A, B](fa: A => R, fb: B => R): ((A, B)) => R = {
case (a, b) => Monoid[R].combine(fa(a), fb(b))
}
}

implicit def catsStdMonadForFunction1[T1]: Monad[T1 => *] =
Expand Down
15 changes: 15 additions & 0 deletions tests/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import cats.laws.discipline.arbitrary._
import cats.syntax.either._
import cats.syntax.foldable._
import cats.syntax.eq._
import cats.Reducible
import cats.Eval
Copy link
Contributor

Choose a reason for hiding this comment

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

Not important but I'm surprised the alphabetical order here isn't checked?

import org.scalacheck.Prop._

class NonEmptyLazyListSuite extends NonEmptyCollectionSuite[LazyList, NonEmptyLazyList, NonEmptyLazyListOps] {
Expand Down Expand Up @@ -164,6 +166,19 @@ class NonEmptyLazyListSuite extends NonEmptyCollectionSuite[LazyList, NonEmptyLa
assert(ci.toNev === (NonEmptyVector.fromVectorUnsafe(Vector.empty[Int] ++ ci.toList.toVector)))
}
}

test("Avoid all evaluation of NonEmptyLazyList#reduceRightTo") {
val sum = implicitly[Reducible[NonEmptyLazyList]]
.reduceRightTo(
NonEmptyLazyList
.fromLazyListPrepend(1, LazyList.from(2))
)(identity) { (elem, acc) =>
if (elem <= 100) acc.map(_ + elem) else Eval.later(0)
}
.value

(1 to 100).sum === sum
}
}

class ReducibleNonEmptyLazyListSuite extends ReducibleSuite[NonEmptyLazyList]("NonEmptyLazyList") {
Expand Down