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 last, sortBy and sorted to NonEmptyList #1578

Merged
merged 4 commits into from
Mar 31, 2017
Merged

Conversation

julien-truffaut
Copy link
Contributor

No description provided.

def sortBy[B: Order](f: A => B): NonEmptyList[A] = {
toList.sortBy(f)(Order[B].toOrdering) match {
case x :: xs => NonEmptyList(x, xs)
case Nil => sys.error("absurd")
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 am not particularly proud of this, suggestions welcome!

Copy link
Contributor

Choose a reason for hiding this comment

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

This is not really a solution but you can “disable” exhaustivity checking instead:

match {
  case List(x, xs @ _*) => NonEmptyList(x, xs)
}

I would add a comment, though, to emphasize that we can not get an empty list since we got our list by converting the NonEmptyList into a List

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the current thing is fine except I would say: => sys.error("unreachable: reduced the number of items in a NonEmptyList after a sort") or something that is somewhat more of a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, I will update the PR

@codecov-io
Copy link

codecov-io commented Mar 27, 2017

Codecov Report

Merging #1578 into master will decrease coverage by 0.03%.
The diff coverage is 75%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #1578      +/-   ##
=========================================
- Coverage   92.43%   92.4%   -0.04%     
=========================================
  Files         248     248              
  Lines        3953    3961       +8     
  Branches      146     141       -5     
=========================================
+ Hits         3654    3660       +6     
- Misses        299     301       +2
Impacted Files Coverage Δ
core/src/main/scala/cats/data/NonEmptyList.scala 97.08% <75%> (-1.86%) ⬇️

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 02fb9a1...12c95c6. Read the comment docs.

* }}}
*/
def sorted[AA >: A](implicit AA: Order[AA]): NonEmptyList[AA] =
sortBy(a => a: 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'd rather not pay the cost to apply the identity function here. Can we instead directly implement?

@julien-truffaut
Copy link
Contributor Author

rebased

@@ -19,6 +19,8 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) {
*/
def toList: List[A] = head :: tail

def last: A = tail.lastOption.getOrElse(head)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we instead case match since it is slightly kinder to the GC? The getOrElse allocates a thunk.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure

* }}}
*/
def sortBy[B: Order](f: A => B): NonEmptyList[A] =
toList.sortBy(f)(Order[B].toOrdering) match {
Copy link
Collaborator

@peterneyens peterneyens Mar 31, 2017

Choose a reason for hiding this comment

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

I think we generally use an implicit parameter (instead of a context bound) when we use the type class instance directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let's be consistent

* }}}
*/
def sorted[AA >: A](implicit AA: Order[AA]): NonEmptyList[AA] =
toList.sorted(Order[AA].toOrdering) match {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we do AA.toOrdering?

@peterneyens peterneyens merged commit 388acd1 into master Mar 31, 2017
@stew stew removed the in progress label Mar 31, 2017
@peterneyens peterneyens deleted the last-sort-nel branch March 31, 2017 14:35
@kailuowang kailuowang modified the milestone: 1.0.0-MF Apr 26, 2017
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.

7 participants