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
29 changes: 29 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,33 @@ 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](f: PartialFunction[A, M])(implicit F: Foldable[F], M: Monoid[M]): M =
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](f: A ⇒ Option[M])(implicit F: Foldable[F], M: Monoid[M]): M =
Copy link
Contributor

Choose a reason for hiding this comment

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

The word filter, to me, kind of implies using a boolean value.
We already have a collectFirstSome https://github.com/typelevel/cats/blob/master/core/src/main/scala/cats/Foldable.scala#L233
how about naming this one collectSomeFold?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A reasonable advice, I'll fix it.

Copy link
Member

Choose a reason for hiding this comment

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

We do have mapFilter which does use Option though.
Maybe this should be mapFilterFold? I don't know

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, I am fine with mapFilterFold too. Sorry about the bikeshedding @satansk , your call.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, l will do it, thanks.

val pf: PartialFunction[String, String] = {
case n if f(n) ⇒ n
}
fa.collectFold(pf) should === (fa.toList.collect(pf).fold(m.empty)(m.combine))

def g(a: String): Option[String] = Some(a).filter(f)
fa.filterFold(g) should === (fa.toList.filter(f).fold(m.empty)(m.combine))
}
}

test(s"Foldable[$name].find/exists/forall/findM/existsM/forallM/filter_/dropWhile_") {
forAll { (fa: F[Int], n: Int) =>
fa.find(_ > n) should === (iterator(fa).find(_ > n))
Expand Down