-
-
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 last, sortBy and sorted to NonEmptyList #1578
Conversation
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") |
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 am not particularly proud of this, suggestions welcome!
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 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
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 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.
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.
sure, I will update the PR
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
* }}} | ||
*/ | ||
def sorted[AA >: A](implicit AA: Order[AA]): NonEmptyList[AA] = | ||
sortBy(a => a: AA) |
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'd rather not pay the cost to apply the identity function here. Can we instead directly implement?
274aa7d
to
4709187
Compare
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) |
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.
Can we instead case match since it is slightly kinder to the GC? The getOrElse allocates a thunk.
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.
sure
* }}} | ||
*/ | ||
def sortBy[B: Order](f: A => B): NonEmptyList[A] = | ||
toList.sortBy(f)(Order[B].toOrdering) match { |
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 think we generally use an implicit parameter (instead of a context bound) when we use the type class instance directly?
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.
let's be consistent
* }}} | ||
*/ | ||
def sorted[AA >: A](implicit AA: Order[AA]): NonEmptyList[AA] = | ||
toList.sorted(Order[AA].toOrdering) match { |
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.
Should we do AA.toOrdering
?
No description provided.