[
https://issues.apache.org/jira/browse/GROOVY-12216?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18100403#comment-18100403
]
ASF GitHub Bot commented on GROOVY-12216:
-----------------------------------------
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<Integer> add = (a, b) -> 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.
> Fat-free twins for the fold/reduce DGM methods (inject, injectAll)
> ------------------------------------------------------------------
>
> Key: GROOVY-12216
> URL: https://issues.apache.org/jira/browse/GROOVY-12216
> Project: Groovy
> Issue Type: Sub-task
> Reporter: Paul King
> Assignee: Paul King
> Priority: Major
>
> h3. Summary
> Sub-task of GROOVY-12205. Adds functional-interface twins for the fold/reduce
> family. Grouped together because they share one design decision — the
> {{BinaryOperator}} vs {{BiFunction}} choice and how it pairs with the
> {{initialValue}} overloads — so the type conventions are settled once.
> {{inject}} on {{Iterable}} already has both twins (from GROOVY-12054); this
> completes the remaining receivers.
> ({{sum}} was originally grouped here but is split out — its twin collides
> positionally with an existing {{Object initialValue}} overload; see the
> separate {{sum}} item.)
> h3. Type convention
> * No-initial-value forms (2-arg reduce): {{BinaryOperator<T>}}.
> * Initial-value forms (3-arg reduce): {{BiFunction<? super U, ? super E, ?
> extends U>}}, matching the existing {{inject(Iterable, U, BiFunction)}}.
> No collision risk: in both methods the reduce function sits in a distinct
> trailing position after any explicit {{initialValue}} parameter, so it never
> competes with the initial value for overload selection.
> h3. Methods
> ||Method||Closure overloads without a twin||Functional type||Receivers||
> |{{inject}}|8|{{BinaryOperator<T>}} (no init) / {{BiFunction<? super U, ?
> super E, ? extends U>}} (init)|no-init: Object, {{E[]}}, Collection;
> with-init: Object, Iterator, Map, {{E[]}}, Collection|
> |{{injectAll}}|5|{{BinaryOperator<T>}} (no init) / {{BiFunction}}
> (init)|no-init: Iterable, Iterator; with-init: Iterable, Iterator, Map|
> Total: 13 new overloads.
> h3. Conventions (per GROOVY-12054)
> * PECS wildcards; {{@since 6.0.0}}; inline javadoc example via {{<pre
> class="language-groovy groovyTestCase">}} cross-linking the {{Closure}}
> counterpart with {{@link}}.
> * Plain single-SAM overloads only — not {{@Incubating}}.
> h3. Compatibility
> * *{{inject}} arity*: since {{BinaryOperator<T> extends BiFunction<T,T,T>}},
> the no-init ({{BinaryOperator}}, 2-arg) and with-init ({{BiFunction}}, 3-arg)
> forms stay distinct by arity — no erasure clash, mirroring the existing
> {{Iterable}} pair.
> * Existing closure/lambda call sites unaffected; method references and typed
> functional values select the twin under {{@CompileStatic}} (GROOVY-12214).
> Only check: an existing {{@CompileStatic}} call passing a literal {{null}} to
> a currently-single-overload form would become ambiguous — grep-checkable,
> resolved with a cast if found.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)