Skip to content
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

Merged
merged 3 commits into from
Nov 6, 2018

Conversation

fmsbeekmans
Copy link
Contributor

Implemented and tested Chain#distinct and NonEmptyChain#distinct.

Still needs examples.

@codecov-io
Copy link

codecov-io commented Oct 22, 2018

Codecov Report

Merging #2579 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2579      +/-   ##
==========================================
+ Coverage   95.14%   95.15%   +0.01%     
==========================================
  Files         361      361              
  Lines        6630     6646      +16     
  Branches      289      304      +15     
==========================================
+ Hits         6308     6324      +16     
  Misses        322      322
Impacted Files Coverage Δ
core/src/main/scala/cats/data/Chain.scala 99.58% <100%> (+0.01%) ⬆️
core/src/main/scala/cats/data/NonEmptyChain.scala 99.07% <100%> (+0.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9069dbb...cef2e4d. Read the comment docs.


var alreadyIn = TreeSet.empty[AA]

foldLeft(Chain.empty[AA]) { (elementsSoFar, b) =>
Copy link
Contributor

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?

Copy link
Contributor Author

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 a HashSet with sets bigger than 4 elements. HashSet#contains has O(1) and TreeSet#contains has O(log(n)).

Changing Set would also remove the artificial dependency on Order. Was there a reason for this dependency when NonEmptyList#distinct was introduced?

Copy link
Contributor

@nasadorian nasadorian Nov 4, 2018

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 on Eq instead of Order. Closed issue here, was suggested to put up a PR on cats-collection instead. #2593

EDIT: Seems like this structure exists already in cats-collections, called ISet.

/**
* Remove duplicates. Duplicates are checked using `Order[_]` instance.
*/
final def distinct[AA >: A](implicit O: Order[AA]): NonEmptyChain[AA] = {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 toChain and then distinct on chain and then rebuild NonEmptyChain? It would remove duplication but I'm not sure if it will still be ok with performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other way around would be cheaper, something along the lines of NonEmptyChain.fromChain(this).fold(empty[A])(_.distinct.toChain)

@kailuowang kailuowang added this to the 1.5 milestone Oct 31, 2018
@kailuowang kailuowang merged commit 9807343 into typelevel:master Nov 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants