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

findAll and findResults should take an optional 'collector' argument

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • groovy-jdk
    • None

    Description

      Currently, the only DGM methods that support providing a collection with which to collect results are collect, collectNested, collectEntries, and collectMany.
      Without this ability for findAll and findResults, one has to convert the resulting collection to the desired type after the fact, which results in extra collections being created.

      Example:

      def list = [1, 2, 2, 6, 7, 9]
      // we want to get the set of unique even numbers in the list
      
      // creates new list and set
      def oldAnswer = list.findAll { it % 2 == 0 }.toSet() 
      
      // only uses the given set
      def newAnswer = list.findAll(new HashSet()) { it % 2 == 0 } 
      

      I have created a pull request with the necessary changes here: https://github.com/groovy/groovy-core/pull/643

      The implementation is essentially the same as that of collect(collection, collector, closure).

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              shils Shil Sinha
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated: