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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update comment for unreachable code and optimise sorted
  • Loading branch information
julien-truffaut committed Mar 30, 2017
commit 4709187f0ab17a60b2b2a35390f2d659dd2304cc
10 changes: 6 additions & 4 deletions core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,11 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) {
* res0: cats.data.NonEmptyList[(Char, Int)] = NonEmptyList((z,1), (a,4), (e,22))
* }}}
*/
def sortBy[B: Order](f: A => B): NonEmptyList[A] = {
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

case x :: xs => NonEmptyList(x, xs)
case Nil => sys.error("absurd")
case Nil => sys.error("unreachable: sorting a NonEmptyList cannot produce an empty List")
}
}

/**
* Sorts this `NonEmptyList` according to an `Order`
Expand All @@ -250,7 +249,10 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) {
* }}}
*/
def sorted[AA >: A](implicit AA: Order[AA]): NonEmptyList[AA] =
sortBy(a => a: 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?

case x :: xs => NonEmptyList(x, xs)
case Nil => sys.error("unreachable: sorting a NonEmptyList cannot produce an empty List")
}

/**
* Groups elements inside of this `NonEmptyList` using a mapping function
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/NonEmptyListTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ class NonEmptyListTests extends CatsSuite {
}
}

test("NonEmptyList#sortBy is consistent with List#sortBy") {
forAll { (nel: NonEmptyList[Int], f: Int => Int) =>
nel.sortBy(f).toList should === (nel.toList.sortBy(f))
}
}


test("NonEmptyList#groupBy is consistent with List#groupBy") {
forAll { (nel: NonEmptyList[Int], f: Int => Int) =>
Expand Down