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

Add distinctBy to NonEmptyCollection and all impls #4608

Merged
merged 2 commits into from
Jun 6, 2024

Conversation

cybersaurus
Copy link
Contributor

First time contributor... 👋

What

  • Added distinctBy to NonEmptyCollection and implementations.

Why

  • When migrating some existing code from List to NonEmptyList I noticed that distinctBy was missing. This addition adds parity with distinct/distinctBy in the scala standard library, and also with cats.data.Chain.

@cybersaurus cybersaurus marked this pull request as ready for review May 31, 2024 18:20
rossabaker
rossabaker previously approved these changes Jun 3, 2024
Copy link
Member

@rossabaker rossabaker left a comment

Choose a reason for hiding this comment

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

Thanks!

Comment on lines 348 to 350
override def distinct[AA >: A](implicit O: Order[AA]): NonEmptyLazyList[AA] = distinctBy(identity[AA])

override def distinctBy[AA >: A, B](f: A => B)(implicit O: Order[B]): NonEmptyLazyList[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'm concerned quite a bit about this type boundary: AA >: A.
It doesn't seem doing any useful work here.

Although the former method distinct requires that type boundary to overcome a variance error, e.g.:

covariant type A occurs in invariant position in type cats.Order[A] of value O

or something like that.

However, in distinctBy it doesn't seem necessary and can only make calling distinctBy more difficult in some specific cases.

Copy link
Contributor

@satorg satorg Jun 5, 2024

Choose a reason for hiding this comment

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

The same concern applies to the other distinctBy methods down below.

Copy link
Contributor

Choose a reason for hiding this comment

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

Although the former method distinct requires that type boundary to overcome a variance error,

Just for a record, I feel the widening of the result type in the original distinct implementation does not seem necessary either. I believe, it could be just this:

def distinct[AA >: A](implicit O: Order[AA]): NonEmptyLazyList[A] // <- can return `A`, not `AA`

Copy link
Contributor

@satorg satorg Jun 5, 2024

Choose a reason for hiding this comment

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

Also just for a record, looking at the original implementation I find it far from efficient: it uses foldLeft which is not really necessary there. Especially when results get accumulated in a mutable collection (ListBuffer in that case). I think it would be more efficient to use a regular loop there to avoid creation of anonymous functions and closures.

UPDATE: sorry, forgot that it is a lazy list. A regular loop wouldn't work well for this particular collection. But for the other non-lazy ones it would do. Anyway, it is just for a record, no need to address it in this PR – it would be out of its scope.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm concerned quite a bit about this type boundary: AA >: A. It doesn't seem doing any useful work here.

Although the former method distinct requires that type boundary to overcome a variance error, e.g.:

covariant type A occurs in invariant position in type cats.Order[A] of value O

or something like that.

However, in distinctBy it doesn't seem necessary and can only make calling distinctBy more difficult in some specific cases.

Thanks for your suggestion. I've simplified the type signature for distinctBy, as per your suggestion.
distinct is unchanged.

@satorg
Copy link
Contributor

satorg commented Jun 5, 2024

Thank you for the contribution! Despite my nitpicks above I really appreciate your work and believe that distinctBy would be a great addition to non-empty collections.

@cybersaurus cybersaurus force-pushed the NonEmptyCollection-distinctBy branch from 5e78f33 to a39d75f Compare June 5, 2024 10:38
@cybersaurus
Copy link
Contributor Author

Thank you for the contribution! Despite my nitpicks above I really appreciate your work and believe that distinctBy would be a great addition to non-empty collections.

Thanks for your feedback 👍 I think I've addressed all your comments now.

@rossabaker
Copy link
Member

The errors in the native build are on suites that I otherwise can't find in the logs. I don't think they're related to this, but I don't really pay attention to Native, so I'm not the best one to help:

  [error] Error: Total 12586, Failed 0, Errors 27, Passed 12559
  [error] Error during tests:
  [error] 	cats.tests.ListInstancesSuite
  [error] 	cats.tests.FoldableSortedMapSuite
  [error] 	cats.tests.ValidatedSuite
  [error] 	cats.tests.ReducibleNonEmptyListSuite
  [error] 	cats.tests.SplitSuite
  [error] 	cats.tests.CategorySuite
  [error] 	cats.tests.FoldableTrySuite
  [error] 	cats.tests.TraverseStreamSuiteUnderlying
  [error] 	cats.tests.FoldableEitherSuite
  [error] 	cats.tests.InjectKSuite
  [error] 	cats.tests.PartialOrderSuite
  [error] 	cats.tests.TraverseListSuiteUnderlying
  [error] 	cats.tests.VectorInstancesSuite
  [error] 	cats.tests.BifunctorSuite
  [error] 	cats.tests.FiniteDurationSuite
  [error] 	cats.tests.FoldableEitherKSuite
  [error] 	cats.tests.ReducibleNonEmptyLazyListSuite
  [error] 	cats.tests.ParallelSuite
  [error] 	cats.tests.ListSuite
  [error] 	cats.tests.ApplicativeErrorSuite
  [error] 	cats.tests.TraverseVectorSuiteUnderlying
  [error] 	cats.tests.TailRecSuite
  [error] 	cats.tests.MonoidSuite
  [error] 	cats.tests.KleisliSuite
  [error] 	cats.tests.EitherKSuite
  [error] 	cats.tests.FoldableSuiteAdditional
  [error] 	cats.tests.EqSuite

@satorg
Copy link
Contributor

satorg commented Jun 5, 2024

I don't think they're related to this

I don't think either. I noticed that after migration to native 0.5 the build became a bit flaky. I'll try to re-run the jobs.

@satorg
Copy link
Contributor

satorg commented Jun 5, 2024

The build has succeeded after the re-run 🎉

I wonder though what is wrong with Scala Native 0.5 that makes our build flaky 🤔
It bothers me a little.

Copy link
Contributor

@satorg satorg left a comment

Choose a reason for hiding this comment

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

Thanks!

@satorg satorg merged commit 17d7e95 into typelevel:main Jun 6, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants