groovy
  1. groovy
  2. GROOVY-4865

Add a take method to Collections, Iterators, Arrays

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.8.1, 1.9-beta-1
    • Component/s: groovy-jdk
    • Labels:
      None
    • Number of attachments :
      1

      Description

      With regards to a thread in the groovy user list here:

      http://groovy.329449.n5.nabble.com/How-to-access-first-10-characters-even-if-string-only-have-5-td4456146.html

      A take( n ) method would be a really useful addition to the Groovy codebase

      It is assumed it will work similar to other languages, in that:

      def a = [ 1, 2, 3 ]
      
      assert a.take( 0 ) == []
      assert a.take( 1 ) == [ 1 ]
      assert a.take( 4 ) == [ 1, 2, 3 ]
      

      The method should work for Collection, String, Map and lazily for Iterator, Reader and InputStream

        Issue Links

          Activity

          Hide
          Tim Yates added a comment -

          Fantastic! Thanks Guillaume! Looks like my @since 1.8.2 was too pessemistic! :-D

          Show
          Tim Yates added a comment - Fantastic! Thanks Guillaume! Looks like my @since 1.8.2 was too pessemistic! :-D
          Hide
          Tim Yates added a comment -

          Tests failed on Java 5...

          The reason is that javax.swing.text.Segment did not implement CharSequence in Java 5, but does in Java 6...

          Here's a patch script to remove that class from the tests:

          Index: src/test/groovy/GroovyMethodsTest.groovy
          ===================================================================
          --- src/test/groovy/GroovyMethodsTest.groovy	(revision 22354)
          +++ src/test/groovy/GroovyMethodsTest.groovy	(working copy)
          @@ -947,7 +947,6 @@
                   def data = [ 'groovy',      // String
                                "${'groovy'}", // GString
                                java.nio.CharBuffer.wrap( 'groovy' ),
          -                     new javax.swing.text.Segment( 'groovy' as char[], 0, 6 ),
                                new StringBuffer( 'groovy' ),
                                new StringBuilder( 'groovy' ) ]
                   data.each {
          @@ -1015,7 +1014,6 @@
                   def data = [ 'groovy',      // String
                                "${'groovy'}", // GString
                                java.nio.CharBuffer.wrap( 'groovy' ),
          -                     new javax.swing.text.Segment( 'groovy' as char[], 0, 6 ),
                                new StringBuffer( 'groovy' ),
                                new StringBuilder( 'groovy' ) ]
                   data.each {
          @@ -1048,7 +1046,6 @@
                     // CharSequences
                     (java.lang.String)        : new String( 'groovy' ),
                     (java.nio.CharBuffer)     : java.nio.CharBuffer.wrap( 'groovy' ),
          -          (javax.swing.text.Segment): new javax.swing.text.Segment( 'groovy' as char[], 0, 6 ),
                   ]
                   data.each { Class clazz, object ->
                       assert clazz.isInstance( object.take( 5 ) )
          

          Sorry about that

          Show
          Tim Yates added a comment - Tests failed on Java 5... The reason is that javax.swing.text.Segment did not implement CharSequence in Java 5, but does in Java 6... Here's a patch script to remove that class from the tests: Index: src/test/groovy/GroovyMethodsTest.groovy =================================================================== --- src/test/groovy/GroovyMethodsTest.groovy (revision 22354) +++ src/test/groovy/GroovyMethodsTest.groovy (working copy) @@ -947,7 +947,6 @@ def data = [ 'groovy', // String "${'groovy'}", // GString java.nio.CharBuffer.wrap( 'groovy' ), - new javax.swing.text.Segment( 'groovy' as char[], 0, 6 ), new StringBuffer( 'groovy' ), new StringBuilder( 'groovy' ) ] data.each { @@ -1015,7 +1014,6 @@ def data = [ 'groovy', // String "${'groovy'}", // GString java.nio.CharBuffer.wrap( 'groovy' ), - new javax.swing.text.Segment( 'groovy' as char[], 0, 6 ), new StringBuffer( 'groovy' ), new StringBuilder( 'groovy' ) ] data.each { @@ -1048,7 +1046,6 @@ // CharSequences (java.lang.String) : new String( 'groovy' ), (java.nio.CharBuffer) : java.nio.CharBuffer.wrap( 'groovy' ), - (javax.swing.text.Segment): new javax.swing.text.Segment( 'groovy' as char[], 0, 6 ), ] data.each { Class clazz, object -> assert clazz.isInstance( object.take( 5 ) ) Sorry about that
          Hide
          Dinko Srkoc added a comment -

          Thanks from me too, Guillaume!

          Show
          Dinko Srkoc added a comment - Thanks from me too, Guillaume!
          Hide
          Rodrigo Rosenfeld Rosas added a comment -

          Shouldn't "take" also be documented in Groovy String class? Currently it isn't:

          http://groovy.codehaus.org/groovy-jdk/java/lang/String.html

          Show
          Rodrigo Rosenfeld Rosas added a comment - Shouldn't "take" also be documented in Groovy String class? Currently it isn't: http://groovy.codehaus.org/groovy-jdk/java/lang/String.html
          Hide
          Tim Yates added a comment -

          Ahhh, because take operates on the class CharSequence (which String implements), the documentation is there instead: http://groovy.codehaus.org/groovy-jdk/java/lang/CharSequence.html

          Show
          Tim Yates added a comment - Ahhh, because take operates on the class CharSequence (which String implements), the documentation is there instead: http://groovy.codehaus.org/groovy-jdk/java/lang/CharSequence.html

            People

            • Assignee:
              Guillaume Laforge
              Reporter:
              Tim Yates
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: