-
-
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 combineAllOption to Foldable #2380
Changes from 8 commits
e3bd620
94fcff1
7f252c7
7f8e044
200d9c0
654aae5
64053b5
427c7f5
38cbe68
f951eef
2d835f1
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cats | ||
package compat | ||
|
||
private[cats] trait FoldableCompat[F[_]] { this: Foldable[F] => | ||
|
||
def iterable[A](fa: F[A]): Stream[A] = | ||
foldRight[A, Stream[A]](fa, Eval.now(Stream.empty)) { (a, eb) => | ||
eb.map(Stream.cons(a, _)) | ||
}.value | ||
} | ||
|
||
private[cats] object FoldableCompat { | ||
|
||
trait ToFoldableCompatOps { | ||
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. I couldn't make simulacrum generate the syntax for this so I added it manually. |
||
implicit final def foldableCompatSyntax[F[_], A](fa: F[A]): FoldableCompatAllOps[F, A] = | ||
new FoldableCompatAllOps(fa) | ||
} | ||
|
||
final class FoldableCompatAllOps[F[_], A](private val fa: F[A]) extends AnyVal { | ||
def iterable(implicit F: Foldable[F]): Stream[A] = F.iterable(fa) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cats | ||
package compat | ||
|
||
private[cats] trait FoldableCompat[F[_]] { this: Foldable[F] => | ||
|
||
def iterable[A](fa: F[A]): LazyList[A] = | ||
foldRight[A, LazyList[A]](fa, Eval.now(LazyList.empty)) { (a, eb) => | ||
eb.map(LazyList.cons(a, _)) | ||
}.value | ||
} | ||
|
||
private[cats] object FoldableCompat { | ||
|
||
trait ToFoldableCompatOps { | ||
implicit final def foldableCompatSyntax[F[_], A](fa: F[A]): FoldableCompatAllOps[F, A] = | ||
new FoldableCompatAllOps(fa) | ||
} | ||
|
||
final class FoldableCompatAllOps[F[_], A](private val fa: F[A]) extends AnyVal { | ||
def iterable(implicit F: Foldable[F]): LazyList[A] = F.iterable(fa) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ import Foldable.sentinel | |
* | ||
* See: [[http://www.cs.nott.ac.uk/~pszgmh/fold.pdf A tutorial on the universality and expressiveness of fold]] | ||
*/ | ||
@typeclass trait Foldable[F[_]] extends UnorderedFoldable[F] { self => | ||
@typeclass trait Foldable[F[_]] extends cats.compat.FoldableCompat[F] with UnorderedFoldable[F] { self => | ||
|
||
/** | ||
* Left associative fold on 'F' using the function 'f'. | ||
|
@@ -275,6 +275,9 @@ import Foldable.sentinel | |
*/ | ||
def combineAll[A: Monoid](fa: F[A]): A = fold(fa) | ||
|
||
def combineAllOption[A](fa: F[A])(implicit ev: Semigroup[A]): Option[A] = | ||
if (isEmpty(fa)) None else ev.combineAllOption(iterable(fa)) | ||
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. If we have iterable now, should we use it in fold for the default implementation? I think we should. 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. Right, it could be implemented in terms of the Monoid's A.combineAll(iterable(fa)) this would allow to optimize the |
||
|
||
/** | ||
* Fold implemented by mapping `A` values into `B` and then | ||
* combining them using the given `Monoid[B]` instance. | ||
|
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.
if this is
private[cats]
can people override outside of cats? I don't know how this works when it is mixed intoFoldble
. Is it even publicly visible at all?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.
I think this is not accessible outside
cats
soFoldableCompat
cannot be extended butiterable
can be overridden throughFoldable
.I ran a quick test for this extending from outside the
cats
package withand it seems that
T1
is ok andT2
fails as expected on 2.12 and 2.13