You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Daniel Sun (Jira)" <ji...@apache.org> on 2025/03/29 09:10:00 UTC

[jira] [Resolved] (GROOVY-11586) Provide an injectAll DGM method

     [ https://issues.apache.org/jira/browse/GROOVY-11586?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Daniel Sun resolved GROOVY-11586.
---------------------------------
    Fix Version/s: 5.0.0-alpha-13
         Assignee: Paul King
       Resolution: Fixed

> Provide an injectAll DGM method
> -------------------------------
>
>                 Key: GROOVY-11586
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11586
>             Project: Groovy
>          Issue Type: Improvement
>            Reporter: Paul King
>            Assignee: Paul King
>            Priority: Major
>             Fix For: 5.0.0-alpha-13
>
>
> Streams have both reduce and scan (in gatherers as part of JDK 24). Groovy has inject for reduce but no equivalent for scan. The intention is to provide injectAll to provide this:
> {code:groovy}
> assert (1..3).injectAll(''){ carry, next -> carry + next } == ['1', '12', '123']
> var runningAvg = [1.0, 2.0, 3.0].injectAll([0.0, 0, null]){ accum, next ->
>     var total = accum[0] + next
>     var count = accum[1] + 1
>     [total, count, total/count]
> }
> assert runningAvg*.get(2) == [1.0, 1.5, 2.0]
> assert [a:1, b:2, c:3].injectAll('') { carry, k, v ->
>     carry + k * v
> } == ['a', 'abb', 'abbccc']
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)