Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filterFold and collectFold to Foldable #2452

Merged
merged 9 commits into from
Sep 12, 2018
Merged
33 changes: 33 additions & 0 deletions core/src/main/scala/cats/syntax/foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,37 @@ final class FoldableOps[F[_], A](val fa: F[A]) extends AnyVal {
case Some((a, src)) => G.map(p(a))(if (_) Right(Some(a)) else Left(src.value))
case None => G.pure(Right(None))
})

/**
* Tear down a subset of this structure using a `PartialFunction`.
*{{{
* scala> import cats.implicits._
* scala> val xs = List(1, 2, 3, 4)
* scala> xs.collectFold { case n if n % 2 == 0 => n }
* res0: Int = 6
*}}}
*/
def collectFold[M: Monoid](f: PartialFunction[A, M])(implicit F: Foldable[F]): M = {
val m = Monoid[M]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totally nitpick - there are two styles of implicit type class instance summoning in this one method (and the filterFold method below). I think it would help readability a little bit if we just use (implicit F: Foldable[F], M: Monoid[M])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice advice, it will be more readable, and I'll fix it :)

Thanks!

F.foldLeft(fa, m.empty)((acc, a) ⇒ m.combine(acc, f.applyOrElse(a, (_: A) ⇒ m.empty)))
}

/**
* Tear down a subset of this structure using a `A => Option[M]`.
*{{{
* scala> import cats.implicits._
* scala> val xs = List(1, 2, 3, 4)
* scala> def f(n: Int): Option[Int] = if (n % 2 == 0) Some(n) else None
* scala> xs.filterFold(f)
* res0: Int = 6
*}}}
*/
def filterFold[M: Monoid](f: A ⇒ Option[M])(implicit F: Foldable[F]): M = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def filterFold[M: Monoid](f: A  Option[M])(implicit F: Foldable[F]): M =
  collectFold(Function.unlift(f))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or, the other way around:

def collectFold[M: Monoid](f: PartialFunction[A, M])(implicit F: Foldable[F]): M =
  filterFold(f.lift)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could indeed define these in terms of each other. I'm not sure if they'd be more performant than just using foldLeft, but if we do this, then it should be filterFold in terms of collectFold, since A => Option[M] has to allocate an extra Option :)

Up to you, @satansk :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @PeterPerhac @LukaJCB , I think define them in terms of each other is good, but people are more familiar with foldLeft, so I personally prefer to define them by foldLeft :)

But if there are big performance differences, we can change to the better one.

val m = Monoid[M]
F.foldLeft(fa, m.empty)((acc, a) ⇒ f(a) match {
case Some(x) ⇒ m.combine(acc, x)
case None ⇒ acc
})
}

}
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 @@ -85,6 +85,20 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(
}
}

test(s"Foldable[$name] partial summation") {
forAll { (fa: F[String], f: String ⇒ Boolean) ⇒
val m: Monoid[String] = Monoid[String]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s test with a non commutative Monoid like String.