Skip to content

Commit

Permalink
typelevel#3141 Changed .reduceMap signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Twizty committed Nov 18, 2019
1 parent 619b1b4 commit c910cea
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/Reducible.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ import simulacrum.{noop, typeclass}
*
* `noop` usage description [[https://github.com/typelevel/simulacrum/issues/162 here]]
*/
@noop def reduceMapA[G[_], A, B](fga: F[G[A]])(f: A => B)(implicit G: Apply[G], B: Semigroup[B]): G[B] =
reduceLeftTo(fga)(ga => G.map(ga)(f))((gb, ga) => G.map2(gb, G.map(ga)(f))(B.combine))
@noop def reduceMapA[G[_], A, B](fa: F[A])(f: A => G[B])(implicit G: Apply[G], B: Semigroup[B]): G[B] =
reduceLeftTo(fa)(f)((gb, a) => G.map2(gb, f(a))(B.combine))

/**
* Monadic reducing by mapping the `A` values to `G[B]`. combining
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/syntax/reducible.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ final class ReducibleOps0[F[_], A](private val fa: F[A]) extends AnyVal {
def reduceA[G[_], B](implicit F: Reducible[F], ev: A <:< G[B], G: Apply[G], B: Semigroup[B]): G[B] =
F.reduceA[G, B](fa.asInstanceOf[F[G[B]]])

def reduceMapA[G[_], B, C](f: B => C)(implicit F: Reducible[F], ev: A <:< G[B], G: Apply[G], C: Semigroup[C]): G[C] =
F.reduceMapA[G, B, C](fa.asInstanceOf[F[G[B]]])(f)
def reduceMapA[G[_], B](f: A => G[B])(implicit F: Reducible[F], G: Apply[G], C: Semigroup[B]): G[B] =
F.reduceMapA[G, A, B](fa)(f)
}
13 changes: 5 additions & 8 deletions tests/src/test/scala/cats/tests/ReducibleSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,18 @@ abstract class ReducibleSuite[F[_]: Reducible](name: String)(implicit ArbFInt: A
}

test(s"Reducible[$name].reduceMapA successful case") {
def intToString(i: Int): String = i.toString

val expected = "123"
val actual =
rangeE(1.asRight[String], 2.asRight[String], 3.asRight[String]).reduceMapA(intToString)
val actual = range(1, 3).reduceMapA(_.toString.some)

actual should ===(expected.asRight[String])
actual should ===(expected.some)
}

test(s"Reducible[$name].reduceMapA failure case") {
def intToString(i: Int): String = i.toString
def intToString(i: Long): Either[String, Int] = if (i == 2) i.toInt.asRight else "boom!!!".asLeft

val expected = "boom!!!"
val actual = rangeE(1.asRight, "boom!!!".asLeft, 3.asRight).reduceMapA(intToString)
actual should ===(expected.asLeft[String])
val actual = range(1, 3).reduceMapA(intToString)
actual should ===(expected.asLeft[Int])
}

test(s"Reducible[$name].toNonEmptyList/toList consistency") {
Expand Down

0 comments on commit c910cea

Please sign in to comment.