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. |
| } | ||
| } | ||
|
|
||
| def size[A](fa: F[A]): Long = foldMap(fa)(_ => 1) |
There was a problem hiding this comment.
👍 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
|
|
||
| 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.
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.