Skip to content

Commit

Permalink
Enable breakout in Reducible[NonEmptyLazyList].reduceRightTo
Browse files Browse the repository at this point in the history
  • Loading branch information
takayahilton committed Aug 6, 2020
1 parent 80036f0 commit b77952b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ 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.now(f(a))) { (a, b) =>
Eval.defer(fa.reduceRightTo(a => Eval.later(f(a))) { (a, b) =>
Eval.defer(g(a, b))
})

Expand Down
11 changes: 10 additions & 1 deletion tests/src/test/scala/cats/tests/ReducibleSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ReducibleSuiteAdditional extends CatsSuite {
val large = NonEmptyList(1, (2 to 10000).toList)
assert(contains(large, 10000).value)
}

// A simple non-empty stream with lazy `foldRight` and `reduceRightTo` implementations.
case class NES[A](h: A, t: Stream[A]) {
def toStream: Stream[A] = h #:: t
Expand Down Expand Up @@ -215,4 +215,13 @@ abstract class ReducibleSuite[F[_]: Reducible](name: String)(implicit

assert(out.toList === List(2, 4, 6, 9))
}

test(s"Reducible[$name].nonEmptyTraverse_ can breakout") {
val notAllEven = fromValues(2, 4, 6, 9, 10, 12, 14)
val out = mutable.ListBuffer[Int]()

notAllEven.nonEmptyTraverse_ { a => out += a; if (a % 2 == 0) Some(a) else None }

assert(out.toList === List(2, 4, 6, 9))
}
}

0 comments on commit b77952b

Please sign in to comment.