-
-
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 size to Foldable #1114
Add size to Foldable #1114
Conversation
Obviously, this naive implementation would not terminate on infinite streams. @yilinwei was suggesting that a possible approach could be to have |
@afiore I would be fine with that behavior. On the other hand, we already have potentially non-terminating methods (like |
I like this approach as opposed to the I think that this is a great start. There are some additional things that I think should be done:
Would you be willing to do those? If not, I'm sure that someone else can get them in a followup PR. |
@@ -56,6 +57,8 @@ import simulacrum.typeclass | |||
} | |||
} | |||
|
|||
def size[A](fa: F[A]): Long = foldMap(fa)(_ => 1) |
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 that Long
was a good choice here. Int
is probably the first thing that would have come to my mind, but people could conceivably be using Foldable
for some very large structures. Though hopefully if their structure is large enough to pass the max Int
value they have overridden size
to a more efficient implementation :)
A couple other structures came to mind that could benefit from an override of |
Current coverage is 88.70%
@@ master #1114 diff @@
==========================================
Files 227 227
Lines 2953 2958 +5
Methods 2897 2898 +1
Messages 0 0
Branches 53 57 +4
==========================================
+ Hits 2618 2624 +6
+ Misses 335 334 -1
Partials 0 0
|
@ceedubs I have added overrides for |
@afiore It will use a |
0e462e2
to
3986ec0
Compare
@ceedubs makes sense! I have amended my commit adding an override for |
4190152
to
2b59ee5
Compare
@@ -64,6 +64,12 @@ class OneAndTests extends CatsSuite { | |||
checkAll("NonEmptyList[Int]", ComonadTests[NonEmptyList].comonad[Int, Int, Int]) | |||
checkAll("Comonad[NonEmptyList[A]]", SerializableTests.serializable(Comonad[NonEmptyList])) | |||
|
|||
test("Foldable[OneAnd[Vector, ?]]#size consistent with Vector#size") { | |||
forAll { (oa: OneAnd[Vector, Int]) => | |||
oa.size should === (1L + oa.tail.size.toLong) |
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 seems to be duplicating the implementation within the test. Instead, it might be better to do something like oa.size should === (oa.unwrap.size)
or oa.size should === (oa.toList.size)
.
Override with a more performant implementation in Map, Set, Vector and AndOne using the size method provided by the standard library. see typelevel#1091
@ceedubs I have updated |
👍 thanks, @afiore! |
👍 |
Partially addresses what discussed in #1091.