-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 7 commits
ed49557
a716cbd
05f5c9b
1d82827
11202e0
0ca4ba7
417a5ce
8b232bd
46d92cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Up to you, @satansk :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 | ||
}) | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let’s test with a non commutative Monoid like String. |
There was a problem hiding this comment.
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])
There was a problem hiding this comment.
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!