@@ -125,11 +125,12 @@ object ArrayOps {
125
125
private [this ] val len = xs.length
126
126
override def knownSize : Int = len - pos
127
127
def hasNext : Boolean = pos < len
128
- def next (): A = try {
128
+ def next (): A = {
129
+ if (pos >= xs.length) Iterator .empty.next()
129
130
val r = xs(pos)
130
131
pos += 1
131
132
r
132
- } catch { case _ : ArrayIndexOutOfBoundsException => Iterator .empty.next() }
133
+ }
133
134
override def drop (n : Int ): Iterator [A ] = {
134
135
if (n > 0 ) {
135
136
val newPos = pos + n
@@ -145,11 +146,12 @@ object ArrayOps {
145
146
private final class ReverseIterator [@ specialized(Specializable .Everything ) A ](xs : Array [A ]) extends AbstractIterator [A ] with Serializable {
146
147
private [this ] var pos = xs.length- 1
147
148
def hasNext : Boolean = pos >= 0
148
- def next (): A = try {
149
+ def next (): A = {
150
+ if (pos < 0 ) Iterator .empty.next()
149
151
val r = xs(pos)
150
152
pos -= 1
151
153
r
152
- } catch { case _ : ArrayIndexOutOfBoundsException => Iterator .empty.next() }
154
+ }
153
155
154
156
override def drop (n : Int ): Iterator [A ] = {
155
157
if (n > 0 ) pos = Math .max( - 1 , pos - n)
@@ -227,14 +229,14 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
227
229
* @return the first element of this array.
228
230
* @throws NoSuchElementException if the array is empty.
229
231
*/
230
- def head : A = try xs.apply(0 ) catch { case _ : ArrayIndexOutOfBoundsException => throw new NoSuchElementException (" head of empty array" ) }
232
+ def head : A = if (nonEmpty) xs.apply(0 ) else throw new NoSuchElementException (" head of empty array" )
231
233
232
234
/** Selects the last element.
233
235
*
234
236
* @return The last element of this array.
235
237
* @throws NoSuchElementException If the array is empty.
236
238
*/
237
- def last : A = try xs.apply(xs.length- 1 ) catch { case _ : ArrayIndexOutOfBoundsException => throw new NoSuchElementException (" last of empty array" ) }
239
+ def last : A = if (nonEmpty) xs.apply(xs.length- 1 ) else throw new NoSuchElementException (" last of empty array" )
238
240
239
241
/** Optionally selects the first element.
240
242
*
@@ -500,7 +502,7 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
500
502
* @tparam A2 the element type of the second resulting collection
501
503
* @param f the 'split function' mapping the elements of this array to an [[scala.util.Either ]]
502
504
*
503
- * @return a pair of arrays: the first one made of those values returned by `f` that were wrapped in [[scala.util.Left ]],
505
+ * @return a pair of arrays: the first one made of those values returned by `f` that were wrapped in [[scala.util.Left ]],
504
506
* and the second one made of those wrapped in [[scala.util.Right ]]. */
505
507
def partitionMap [A1 : ClassTag , A2 : ClassTag ](f : A => Either [A1 , A2 ]): (Array [A1 ], Array [A2 ]) = {
506
508
val res1 = ArrayBuilder .make[A1 ]
0 commit comments