-
-
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 init and size methods to NonEmptyList #1628
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
|
||
/** | ||
* 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kailuowang any preference regarding There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @peterneyens I added tests for |
||
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 | ||
*/ | ||
|
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.
tail.lastOption.getOrElse(head)
might be more performant.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.
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).
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.
@johnynek yeah, I forgot that
getOrElse
is by-name, and corrected myself in the other comment #1628 (comment)