[
https://issues.apache.org/jira/browse/GROOVY-12722?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18140068#comment-18140068
]
ASF GitHub Bot commented on GROOVY-12722:
-----------------------------------------
codeconsole opened a new pull request, #3256:
URL: https://github.com/apache/groovy/pull/3256
https://issues.apache.org/jira/browse/GROOVY-12722
`sumParallel(Collection<T>, BinaryOperator<T>)` is a reduction with a
required two-argument combiner, where `sum()` adds the elements and
`sum(Closure)` adds their transforms. Code written by analogy with `sum`
failed: `items.sumParallel { it.price }` called the one-parameter closure with
two arguments, and under static compilation was rejected with "Wrong number of
parameters for method target: apply(java.lang.Integer, java.lang.Integer)".
That is the form a model with the parallel-collections documentation in front
of it still wrote.
Two additive forms in `ParallelCollectionExtensions`:
- `sumParallel(Collection<?> self)` adds the elements with the left
operand's `plus`, as `sum()` does; an empty collection gives `null`.
- `<T, R> R sumParallel(Collection<T> self, Closure<R> closure)`, with
`@ClosureParams(value = FromString, options = ["T", "T,T"])`: a one-parameter
closure is a transform whose results are added; a two-parameter closure is the
combiner of the `BinaryOperator` form, so existing calls keep working and,
under static compilation, keep their result type (`int combined =
numbers.sumParallel { int a, int b -> a + b }`). The `BinaryOperator` overload
stays for method references and Java callers.
`CombinerChecker` leaves a one-parameter closure passed to `sumParallel`
alone: it is a transform, not a combiner. `core-parallel-collections.adoc` and
the GPars migration table show the three forms.
Tests: `ParallelCollectionTest.testSumParallelAddsUpLikeSum` (numbers,
decimals, strings, lists, empty and one-element collections, the transform, the
combiner, a method reference, and the three forms under `@CompileStatic`),
`CombinerCheckerTest.transform_closure_is_not_a_combiner` (strict mode). Also
run locally: `groovy.concurrent.ParallelCollectionTest`, `DesignPatternsTest`,
`ParallelExecutorTasksTest` and `CombinerCheckerTest`, 134 tests, passing.
> sumParallel: add the no-argument and transform forms of sum(); the combiner
> form reads as inject
> ------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12722
> URL: https://issues.apache.org/jira/browse/GROOVY-12722
> Project: Groovy
> Issue Type: Improvement
> Affects Versions: 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {{Collection.sum()}} adds the elements, and {{sum(Closure)}} adds the
> transformed elements.
> {{ParallelCollectionExtensions.sumParallel(Collection<T>,
> BinaryOperator<T>)}} is a reduction with a required two-argument combiner
> (GPars' {{sumParallel()}} took no argument; the migration guide maps it to
> {{sumParallel \{ a, b -> a + b \}}} and notes that the accumulator is
> required). Code written by analogy with {{sum}} fails:
> {code:groovy}
> @CompileStatic
> int total(List<Integer> numbers) {
> numbers.sumParallel { Integer n -> n }
> }
> {code}
> {noformat}
> [Static type checking] - Wrong number of parameters for method target:
> apply(java.lang.Integer, java.lang.Integer)
> {noformat}
> and in dynamic code {{items.sumParallel \{ it.price \}}} calls the
> one-parameter closure with two arguments.
> Proposal, additive:
> * {{sumParallel(Collection<?> self)}}: the elements added with the left
> operand's {{plus}}, as {{sum()}} does; an empty collection gives null.
> * {{<T, R> R sumParallel(Collection<T> self, Closure<R> closure)}} with
> {{@ClosureParams(value = FromString, options = ["T", "T,T"])}}: a
> one-parameter closure is a transform and the transformed elements are added,
> the {{sum(Closure)}} contract; a two-parameter closure stays the combiner, so
> existing calls keep working and, under static compilation, keep their result
> type. The {{BinaryOperator}} overload stays for method references and Java
> callers.
> * {{CombinerChecker}} (GROOVY-12013) leaves a one-parameter closure alone: it
> is not a combiner.
> * core-parallel-collections.adoc and the GPars migration table show the three
> forms.
> Found while scoring Groovy 6.0.0 code written by language models: with the
> parallel-collections skill text in front of it, GPT-6 still wrote the
> one-parameter form.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)