Copilot commented on code in PR #2751:
URL: https://github.com/apache/groovy/pull/2751#discussion_r3682387263


##########
src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java:
##########
@@ -9867,6 +10034,29 @@ public static <E, T, V extends T> Iterator<T> 
injectAll(Iterator<E> self, @Closu
         return (Iterator<T>) injectAll(self, self.next(), closure);
     }
 
+    /**
+     * Iterates through the given iterator, injecting values as per 
<tt>inject</tt>
+     * but returns an iterator of all calculated values instead of just the 
final result.
+     * A "fat-free" variant of {@link #injectAll(Iterator, Closure)} accepting 
a {@link BinaryOperator}.
+     * <pre class="language-groovy groovyTestCase">
+     * import java.util.function.BinaryOperator
+     * BinaryOperator&lt;Integer&gt; add = (a, b) -&gt; a + b
+     * assert (1..5).iterator().injectAll(add).toList() == [3, 6, 10, 15]
+     * </pre>
+     *
+     * @param self     an iterator
+     * @param operator a binary operator combining the running result with 
each element
+     * @return an iterator of all calculated values
+     * @throws NoSuchElementException if the iterator is empty
+     * @since 6.0.0
+     */
+    public static <T> Iterator<T> injectAll(Iterator<T> self, 
BinaryOperator<T> operator) {
+        if (!self.hasNext()) {
+            throw new NoSuchElementException("Cannot call injectAll() on an 
empty iterable without passing an initial value.");
+        }

Review Comment:
   The new iterator overload throws a NoSuchElementException whose message says 
"empty iterable", but this overload accepts an Iterator (and the Javadoc also 
refers to an iterator). This makes the error message misleading when called on 
an iterator directly.



-- 
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]

Reply via email to