Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-5315

Inject could take the initialValue from the first item in the Collection/Array

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.8.6
    • 2.0-beta-3, 1.8.7
    • groovy-jdk
    • 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 ) }
      

      Attachments

        Activity

          People

            paulk Paul King
            tim_yates Tim Yates
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: