Skip to content

Commit 78da69f

Browse files
committed
IndexedSeq.head uses isEmpty
Seq.isEmpty is lengthCompare and IndexedSeq has efficient length aka knownSize. headOption already uses isEmpty.
1 parent 4c52b44 commit 78da69f

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

library/src/scala/collection/IndexedSeq.scala

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,24 @@ trait IndexedSeqOps[+A, +CC[_], +C] extends Any with SeqOps[A, CC, C] { self =>
9292
override def slice(from: Int, until: Int): C = fromSpecific(new IndexedSeqView.Slice(this, from, until))
9393

9494
override def head: A =
95-
try apply(0)
96-
catch {
97-
case e: IndexOutOfBoundsException =>
98-
val what = self match {
99-
case self: IndexedSeq[_] => self.collectionClassName
100-
case _ => toString
101-
}
102-
throw new NoSuchElementException(s"head of empty $what")
103-
}
95+
if (!isEmpty) apply(0)
96+
else throw new NoSuchElementException(s"head of empty ${
97+
self match {
98+
case self: IndexedSeq[_] => self.collectionClassName
99+
case _ => toString
100+
}
101+
}")
104102

105103
override def headOption: Option[A] = if (isEmpty) None else Some(head)
106104

107105
override def last: A =
108-
try apply(length - 1)
109-
catch {
110-
case e: IndexOutOfBoundsException =>
111-
val what = self match {
112-
case self: IndexedSeq[_] => self.collectionClassName
113-
case _ => toString
114-
}
115-
throw new NoSuchElementException(s"last of empty $what")
116-
}
106+
if (!isEmpty) apply(length - 1)
107+
else throw new NoSuchElementException(s"last of empty ${
108+
self match {
109+
case self: IndexedSeq[_] => self.collectionClassName
110+
case _ => toString
111+
}
112+
}")
117113

118114
// We already inherit an efficient `lastOption = if (isEmpty) None else Some(last)`
119115

0 commit comments

Comments
 (0)