Skip to content

Commit

Permalink
typelevel#3141 Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Twizty committed Nov 15, 2019
1 parent 6d28b9c commit f914fb5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
36 changes: 17 additions & 19 deletions core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cats
import scala.collection.mutable
import cats.instances.either._
import cats.kernel.CommutativeMonoid
import simulacrum.typeclass
import simulacrum.{noop, typeclass}
import Foldable.sentinel

/**
Expand Down Expand Up @@ -279,24 +279,22 @@ import Foldable.sentinel
}

/**
* Fold implemented using the given `Applicative[G]` and `Monoid[A]` instance.
*
* This method is identical to fold, except that we use `Applicative[G]` and `Monoid[A]`
* to combine a's inside an applicative G.
*
* For example:
*
* {{{
* scala> import cats.implicits._
* scala> val F = Foldable[List]
* scala> F.foldA(List(Right(1) :: Right(2) :: Nil)
* res0: Right[Int] = Right(3)
* }}}
*/
def foldA[G[_], A](fga: F[G[A]])(implicit G: Applicative[G], A: Monoid[A]): G[A] =
foldLeft(fga, G.pure(A.empty)) { (acc, ga) =>
G.map2(acc, ga)(A.combine)
}
* Fold implemented using the given `Applicative[G]` and `Monoid[A]` instance.
*
* This method is identical to fold, except that we use `Applicative[G]` and `Monoid[A]`
* to combine a's inside an applicative G.
*
* For example:
*
* {{{
* scala> import cats.implicits._
* scala> val F = Foldable[List]
* scala> F.foldA(List(Right(1) :: Right(2) :: Nil)
* res0: Right[Int] = Right(3)
* }}}
*/
@noop def foldA[G[_], A](fga: F[G[A]])(implicit G: Applicative[G], A: Monoid[A]): G[A] =
fold(fga)(Applicative.monoid)

/**
* Alias for [[foldM]].
Expand Down
22 changes: 10 additions & 12 deletions core/src/main/scala/cats/Reducible.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats

import cats.data.{Ior, NonEmptyList}
import simulacrum.typeclass
import simulacrum.{noop, typeclass}

/**
* Data structures that can be reduced to a summary value.
Expand Down Expand Up @@ -67,19 +67,17 @@ import simulacrum.typeclass
reduceLeftTo(fa)(f)((gb, a) => G.flatMap(gb)(g(_, a)))

/**
* Reduce a `F[G[A]]` value using `Applicative[G]` and `Semigroup[A]`, a universal
* semigroup for `G[_]`.
*
* This method is a generalization of `reduce`.
*/
def reduceA[G[_], A](fga: F[G[A]])(implicit G: Applicative[G], A: Semigroup[A]): G[A] =
reduceLeft(fga)((ga1, ga2) => G.map2(ga1, ga2)(A.combine))
* Reduce a `F[G[A]]` value using `Applicative[G]` and `Semigroup[A]`, a universal
* semigroup for `G[_]`.
*/
@noop def reduceA[G[_], A](fga: F[G[A]])(implicit G: Apply[G], A: Semigroup[A]): G[A] =
reduce(fga)(Apply.semigroup)

/**
* Apply `f` to each applicative `ga` of `fga` and combine them using the
* given `Semigroup[B]`.
*/
def reduceMapA[G[_], A, B](fga: F[G[A]])(f: A => B)(implicit G: Applicative[G], B: Semigroup[B]): G[B] =
* Apply `f` to each applicative `ga` of `fga` and combine them using the
* given `Semigroup[B]`.
*/
@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))

/**
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/syntax/foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ final class FoldableOps[F[_], A](private val fa: F[A]) extends AnyVal {
def foldr[B](b: Eval[B])(f: (A, Eval[B]) => Eval[B])(implicit F: Foldable[F]): Eval[B] =
F.foldRight(fa, b)(f)

def foldA[G[_], B](implicit F: Foldable[F], ev: A <:< G[B], G: Applicative[G], B: Monoid[B]): G[B] =
F.foldA[G, B](fa.asInstanceOf[F[G[B]]])

/**
* test if `F[A]` contains an `A`, named contains_ to avoid conflict with existing contains which uses universal equality
*
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/syntax/reducible.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ final class ReducibleOps0[F[_], A](private val fa: F[A]) extends AnyVal {
* */
def reduceMapK[G[_], B](f: A => G[B])(implicit F: Reducible[F], G: SemigroupK[G]): G[B] =
F.reduceLeftTo(fa)(f)((b, a) => G.combineK(b, f(a)))

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)
}

0 comments on commit f914fb5

Please sign in to comment.