-
-
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
Added Chain#distinct
and NonEmptyChain#distinct
to match List#distinct
and NonEmptyList#distinct
#2579
Added Chain#distinct
and NonEmptyChain#distinct
to match List#distinct
and NonEmptyList#distinct
#2579
Changes from all commits
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 |
---|---|---|
|
@@ -372,6 +372,24 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal { | |
/** Reverses this `NonEmptyChain` */ | ||
final def reverse: NonEmptyChain[A] = | ||
create(toChain.reverse) | ||
|
||
/** | ||
* Remove duplicates. Duplicates are checked using `Order[_]` instance. | ||
*/ | ||
final def distinct[AA >: A](implicit O: Order[AA]): NonEmptyChain[AA] = { | ||
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 wonder. Dont you think that it would be better to to call 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. The other way around would be cheaper, something along the lines of |
||
implicit val ord = O.toOrdering | ||
|
||
var alreadyIn = TreeSet(head: AA) | ||
|
||
foldLeft(NonEmptyChain(head: AA)) { (elementsSoFar, b) => | ||
if (alreadyIn.contains(b)) { | ||
elementsSoFar | ||
} else { | ||
alreadyIn += b | ||
elementsSoFar :+ b | ||
} | ||
} | ||
} | ||
} | ||
|
||
sealed abstract private[data] class NonEmptyChainInstances extends NonEmptyChainInstances1 { | ||
|
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.
Just a question of curiosity. Why are you using TreeSet instead of pure scala's Set?
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 was done for symmetry with
NonEmptyList#distinct
but it's a good question.Set
uses aHashSet
with sets bigger than 4 elements.HashSet#contains
has O(1) andTreeSet#contains
has O(log(n)).Changing
Set
would also remove the artificial dependency onOrder
. Was there a reason for this dependency whenNonEmptyList#distinct
was introduced?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 wrote up a functional set implementation that should speed up
distinct
in the future and let it depend onEq
instead ofOrder
. Closed issue here, was suggested to put up a PR on cats-collection instead. #2593EDIT: Seems like this structure exists already in cats-collections, called ISet.