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

.each {} should return its collection, not void

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0
    • 1.1-beta-3
    • groovy-jdk
    • None

    Description

      The .each{} gdk method should return its own collection, unaltered. That will make it possible to chain .each{} methods, as demonstrated in the code below. It may be desirable to look at other gdk methods as well.

      THE CODE AS IT IS NOW (in Groovy 1.0):

      def numbers = ['one', 'two', 'three', 4, 5];
      def maxSize = 0;

      numbers
      .collect

      { maxSize = Math.max(it.toString().size(), maxSize); return it; }

      .each
      {
      println "${it.toString().padRight(maxSize)} : ${it.getClass().name}";
      };

      OUTPUT:
      one : java.lang.String
      two : java.lang.String
      three : java.lang.String
      4 : java.lang.Integer
      5 : java.lang.Integer

      THE CODE WHERE each{} RETURNS ITS COLLECTION:

      def numbers = ['one', 'two', 'three', 4, 5];
      def maxSize = 0;

      numbers
      .each

      { maxSize = Math.max(it.toString().size(), maxSize); }

      .each
      {
      println "${it.toString().padRight(maxSize)} : ${it.getClass().name}";
      };

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            jasonsmith Jason Smith
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: