Skip to content

Commit 1470081

Browse files
committed
Enable breakout in functions nonEmptyTraverse_ and nonEmptySequence_
1 parent 201e834 commit 1470081

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

core/src/main/scala/cats/Reducible.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ import scala.annotation.implicitNotFound
182182
* the traversal.
183183
*/
184184
def nonEmptyTraverse_[G[_], A, B](fa: F[A])(f: A => G[B])(implicit G: Apply[G]): G[Unit] =
185-
G.void(reduceLeftTo(fa)(f)((x, y) => G.map2(x, f(y))((_, b) => b)))
185+
G.void(reduceRightTo(fa)(f)((x, y) => G.map2Eval(f(x), y)((_, b) => b)).value)
186186

187187
/**
188188
* Sequence `F[G[A]]` using `Apply[G]`.
@@ -192,7 +192,7 @@ import scala.annotation.implicitNotFound
192192
* [[nonEmptyTraverse_]] documentation for a description of the differences.
193193
*/
194194
def nonEmptySequence_[G[_], A](fga: F[G[A]])(implicit G: Apply[G]): G[Unit] =
195-
G.void(reduceLeft(fga)((x, y) => G.map2(x, y)((_, b) => b)))
195+
nonEmptyTraverse_(fga)(identity)
196196

197197
def toNonEmptyList[A](fa: F[A]): NonEmptyList[A] =
198198
reduceRightTo(fa)(a => NonEmptyList(a, Nil)) { (a, lnel) =>

tests/src/test/scala/cats/tests/ReducibleSuite.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,16 @@ class ReducibleSuiteAdditional extends CatsSuite {
9292

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

95-
assert(out.toList === (List(2, 4, 6, 9)))
95+
assert(out.toList === List(2, 4, 6, 9))
96+
}
97+
98+
test("Reducible[NonEmptyList].nonEmptyTraverse_ can breakout") {
99+
val notAllEven = NonEmptyList.of(2, 4, 6, 9, 10, 12, 14)
100+
val out = mutable.ListBuffer[Int]()
101+
102+
notAllEven.nonEmptyTraverse_ { a => out += a; if (a % 2 == 0) Some(a) else None }
103+
104+
assert(out.toList === List(2, 4, 6, 9))
96105
}
97106

98107
// A simple non-empty stream with lazy `foldRight` and `reduceRightTo` implementations.

0 commit comments

Comments
 (0)