Skip to content

Commit

Permalink
Avoid all evaluation of NonEmptyLazyList#reduceRightTo
Browse files Browse the repository at this point in the history
  • Loading branch information
takayahilton committed Aug 18, 2020
1 parent 1b66b81 commit 4939f83
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
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
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

0 comments on commit 4939f83

Please sign in to comment.