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 init and size methods to NonEmptyList #1628

Merged
merged 4 commits into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 42 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,56 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) {

/**
* Return the head and tail into a single list
* {{{
* scala> import cats.data.NonEmptyList
* scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
* scala> nel.toList
* res0: scala.collection.immutable.List[Int] = List(1, 2, 3, 4, 5)
* }}}
*/
def toList: List[A] = head :: tail

/**
* Selects the last element
* {{{
* scala> import cats.data.NonEmptyList
* scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
* scala> nel.last
* res0: Int = 5
* }}}
*/
def last: A = tail.lastOption match {
case None => head
case Some(a) => a
Copy link
Contributor

Choose a reason for hiding this comment

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

tail.lastOption.getOrElse(head) might be more performant.

Copy link
Contributor

Choose a reason for hiding this comment

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

he got the exact opposite advice from me a few days ago. Why do you say this? My understanding is that case class matching is very efficient and this also avoids the closure allocation (since getOrElse is by-name).

Copy link
Contributor

@kailuowang kailuowang Apr 26, 2017

Choose a reason for hiding this comment

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

@johnynek yeah, I forgot that getOrElse is by-name, and corrected myself in the other comment #1628 (comment)

}

/**
* Selects all elements except the last
*
* {{{
* scala> import cats.data.NonEmptyList
* scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
* scala> nel.init
* res0: scala.collection.immutable.List[Int] = List(1, 2, 3, 4)
* }}}
*/
def init: List[A] = tail match {
Copy link
Contributor Author

@jtjeferreira jtjeferreira Apr 24, 2017

Choose a reason for hiding this comment

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

@kailuowang any preference regarding init performance? toList.init is also an alternative

Copy link
Contributor

Choose a reason for hiding this comment

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

I am going to stop pretend that I know anything about performance. I don't know if much difference can be made here.

Copy link
Contributor

Choose a reason for hiding this comment

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

It (should) be more performant this way I think, because there's one less branch. But only very slightly.

case Nil => List.empty
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this be head :: Nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the tail is empty the last element is the head, so the result is the empty list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@peterneyens I added tests for init and size

case t => head :: t.init
}

/**
* The size of this NonEmptyList
*
* {{{
* scala> import cats.data.NonEmptyList
* scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
* scala> nel.size
* res0: Int = 5
* }}}
*/
def size: Int = 1 + tail.size

/**
* Applies f to all the elements of the structure
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/src/test/scala/cats/tests/NonEmptyListTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ class NonEmptyListTests extends CatsSuite {
}
}

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

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

test("NonEmptyList#sorted is consistent with List#sorted") {
forAll { nel: NonEmptyList[Int] =>
nel.sorted.toList should === (nel.toList.sorted)
Expand Down