-
-
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 missing Chain#distinctBy method #4156
Conversation
Note: this
Not sure if it's really worth it though. Wdyt? |
6349f4a
to
0f8e118
Compare
25a4812
to
3e197e4
Compare
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.
Thanks!
3e197e4
to
159a315
Compare
159a315
to
e090080
Compare
var result = Chain.empty[A] | ||
val seen = mutable.TreeSet.empty[B] | ||
val it = iterator | ||
while (it.hasNext) { |
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.
one issue: this is going to create a Chain
the costs O(N)
to uncons.
I wonder if the better approach would build a Vector and Wrap
that. Vector seems to be the default collection we use in other places
e.g.
val bldr = Vector.newBuilder[A]
...
Wrap(bldr.result())
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.
Yeah, I wonder too. I didn't really think about it, perhaps you're right. Let's try this option.
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.
fixed
e090080
to
cf16b6a
Compare
bldr += next | ||
} | ||
// Result can contain a single element only. | ||
Chain.fromSeq(bldr.result()) |
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.
Potentially this line (and the similar one below) could be expanded into something more efficient like
bldr.result() match {
case Vector(a) => Singleton(a)
case seq => Wrap(seq)
}
but... I'm not really sure it is worth it – I mean, it is unlikely that such a single call can lead to substantial (or even noticeable) performance decrease, IMO.
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 looks great to me!
Core:
Chain#distinctBy
method.TreeSet
viacontains
followed by+=
with just one access viaadd
).Tests:
trait cats.tests.compat.ScalaVersionSpecificSyntax
that implements an extension methoddistinctBy
for anySeq
-derived collection in Scala 2.12 (and does nothing for other Scala versions).Chain#distinctBy
toChainSuite
.