Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
1.8.6
-
None
Description
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 ) }