eric-milles commented on code in PR #1785:
URL: https://github.com/apache/groovy/pull/1785#discussion_r976651181
##########
src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java:
##########
@@ -2273,6 +2273,26 @@ public static <T> T[] eachWithIndex(T[] self,
@ClosureParams(value=FromString.cl
return self;
}
+ /**
+ * Iterates through an int[],
+ * passing each int and the element's index (a counter starting at
+ * zero) to the given closure.
+ * <pre class="groovyTestCase">
+ * int result = 0
+ * [0, 1, 2].eachWithIndex{ x, index {@code ->} result += x * index }
+ * assert result == 5
+ * </pre>
+ *
+ * @param self an int array
+ * @param closure a Closure to operate on each item
+ * @return the self Object
+ * @since 1.0
+ */
+ public static int[] eachWithIndex(int[] self,
@ClosureParams(value=FromString.class, options="int,Integer") Closure closure) {
+ eachWithIndex(new IntArrayIterator(self), closure);
Review Comment:
We could avoid the intermediate object and method calls and just use simple
iterative implementation like `T[]` provides.
```java
final Object[] args = new Object[2];
for (int i = 0, n = self.length; i < n; i += 1) {
args[0] = self[i]; args[1] = i;
closure.call(args);
}
```
Yes it will be repeat, but I think the efficiency is more important in this
case.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]