groovy/groovy-core — pull request #27 by timyates (2012-02-17 10:37:30+00:00)
Title: GROOVY-5315 Inject could take the initialValue from the first item in the Collection/Array

Pull request for http://jira.codehaus.org/browse/GROOVY-5315

Currently, you need to pass an initial value into inject. If (for example) you are composing a list of Closures, this means you end up with:

closures.drop( 1 ).inject( closures.head() ) { a, b -> a << b }

Or, if you are comparing the intersection of a list of list, you get:

def lists = [ [1, 2, 3], [2, 3, 4], [4, 5, 2] ]
lists.drop( 1 ).inject( lists.head() ) { a, b -> a.intersect( b ) }

I propose a new form of inject that takes the initial value from the head of the list, and applys the tail of the list to it (as per current implementation). The above examples would then become:

closures.inject { a, b -> a << b }

Or, if you are comparing the intersection of a list of list, you get:

lists.inject { a, b -> a.intersect( b ) }