-
-
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
add init and size methods to NonEmptyList #1628
Conversation
* }}} | ||
*/ | ||
def init: List[A] = tail match { | ||
case Nil => List.empty |
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.
Shouldn't this be head :: Nil
?
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
@peterneyens I added tests for init
and size
Codecov Report
@@ Coverage Diff @@
## master #1628 +/- ##
==========================================
+ Coverage 93.37% 93.37% +<.01%
==========================================
Files 240 240
Lines 3937 3941 +4
Branches 139 144 +5
==========================================
+ Hits 3676 3680 +4
Misses 261 261
Continue to review full report at Codecov.
|
* scala> nel.last | ||
* res0: Int = 5 | ||
* }}} | ||
*/ | ||
def last: A = tail.lastOption match { | ||
case None => head | ||
case Some(a) => a |
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)
* 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 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
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 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 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.
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.
Nice change!
* }}} | ||
*/ | ||
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.
Please don't change this implementation. The one above avoids the allocation of the closure and was the motivation for writing it that way.
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.
ok. will revert
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.
oops, I forgot that getOrElse
is by-name. 👅 we need a strict getOrElese
.
👍 from me after https://github.com/typelevel/cats/pull/1628/files#r112997682 is addressed. |
5745f5b
to
63f74f1
Compare
👍 |
No description provided.