Skip to content

Commit

Permalink
Backport #2380 Add combineAllOption to Foldable (#3340)
Browse files Browse the repository at this point in the history
* backported #2380 Added combineAllOption to Foldable

* addressed review feedback, removed FoldableCompat

* removed toIterable override for `Stream`
  • Loading branch information
gagandeepkalra authored Mar 11, 2020
1 parent 3d5a5c2 commit 40544c9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
5 changes: 2 additions & 3 deletions core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cats
import cats.Foldable.sentinel
import cats.instances.either._
import cats.kernel.CommutativeMonoid
import cats.syntax.foldable
import simulacrum.typeclass

import scala.collection.mutable
Expand Down Expand Up @@ -239,9 +240,7 @@ import scala.collection.mutable
* Fold implemented using the given Monoid[A] instance.
*/
def fold[A](fa: F[A])(implicit A: Monoid[A]): A =
foldLeft(fa, A.empty) { (acc, a) =>
A.combine(acc, a)
}
A.combineAll(foldable.catsSyntaxFoldableOps0(fa).toIterable(this))

/**
* Alias for [[fold]].
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/scala/cats/syntax/foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,18 @@ final class FoldableOps0[F[_], A](private val fa: F[A]) extends AnyVal {
def maximumByOption[B: Order](f: A => B)(implicit F: Foldable[F]): Option[A] =
F.maximumOption(fa)(Order.by(f))

def combineAllOption(implicit ev: Semigroup[A], F: Foldable[F]): Option[A] =
if (F.isEmpty(fa)) None else ev.combineAllOption(toIterable)

/**
* Convert F[A] to an Iterable[A].
*
* This method may be overridden for the sake of performance, but implementers should take care
* not to force a full materialization of the collection.
*/
def toIterable(implicit F: Foldable[F]): Iterable[A] =
F.foldRight[A, Stream[A]](fa, Eval.now(Stream.empty))((a, eb) => eb.map(Stream.cons(a, _))).value

/**
* Implementers are responsible for ensuring they maintain consistency with foldRight; this is not checked by laws on Scala 2.11
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/src/test/scala/cats/tests/FoldableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(implicit ArbFInt: Arb
}
}

test(s"Foldable[$name].combineAllOption") {
forAll { (fa: F[Int]) =>
fa.combineAllOption should ===(fa.toList.combineAllOption)
fa.combineAllOption should ===(iterator(fa).toList.combineAllOption)
}
}

test(s"Foldable[$name].iterable") {
forAll { (fa: F[Int]) =>
fa.toIterable.toList should ===(fa.toList)
fa.toIterable.toList should ===(iterator(fa).toList)
}
}

test(s"Foldable[$name].intercalate") {
forAll { (fa: F[String], a: String) =>
fa.intercalate(a) should ===(fa.toList.mkString(a))
Expand Down

0 comments on commit 40544c9

Please sign in to comment.