You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by jw...@apache.org on 2017/03/26 21:25:46 UTC

[1/2] groovy git commit: Adds documentation around 'remove' method Integer/Object ambiguity (closes #514)

Repository: groovy
Updated Branches:
  refs/heads/master 293e1790f -> 5e4c92ff7


Adds documentation around 'remove' method Integer/Object ambiguity (closes #514)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/091a8b7b
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/091a8b7b
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/091a8b7b

Branch: refs/heads/master
Commit: 091a8b7be05208902d09492756e9b4953e7ec2fc
Parents: 293e179
Author: Jason Schindler <ja...@types.codes>
Authored: Sun Mar 19 14:50:40 2017 -0500
Committer: John Wagenleitner <jw...@apache.org>
Committed: Sun Mar 26 13:13:36 2017 -0700

----------------------------------------------------------------------
 src/spec/doc/working-with-collections.adoc      | 20 ++++++++++++++++----
 .../test/gdk/WorkingWithCollectionsTest.groovy  | 20 +++++++++++++++++++-
 2 files changed, 35 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/091a8b7b/src/spec/doc/working-with-collections.adoc
----------------------------------------------------------------------
diff --git a/src/spec/doc/working-with-collections.adoc b/src/spec/doc/working-with-collections.adoc
index 2e94c14..ace44ed 100644
--- a/src/spec/doc/working-with-collections.adoc
+++ b/src/spec/doc/working-with-collections.adoc
@@ -138,22 +138,34 @@ The {gdk} also contains methods allowing you to easily remove elements from a li
 include::{projectdir}/src/spec/test/gdk/WorkingWithCollectionsTest.groovy[tags=list_gdk3,indent=0]
 ----------------------------------------------------------------------------
 
-It is also possible to remove an element by referring to its index, in which case the list is mutated:
+It is also possible to remove an element by passing its index to the `remove` method, in which case the list is mutated:
 
 [source,groovy]
 ----------------------------------------------------------------------------
-include::{projectdir}/src/spec/test/gdk/WorkingWithCollectionsTest.groovy[tags=list_gdk4,indent=0]
+include::{projectdir}/src/spec/test/gdk/WorkingWithCollectionsTest.groovy[tags=list_gdk_remove_index,indent=0]
 ----------------------------------------------------------------------------
 
 In case you only want to remove the first element having the same value in a list, instead of removing all
-elements, you call call the `remove` method:
+elements, you call call the `remove` method passing the value:
 
 [source,groovy]
 ----------------------------------------------------------------------------
 include::{projectdir}/src/spec/test/gdk/WorkingWithCollectionsTest.groovy[tags=list_gdk5,indent=0]
 ----------------------------------------------------------------------------
 
-And removing all the elements in a list can be done by calling the `clear` method:
+As you can see, there are two `remove` methods available.  One that takes an integer and removes an element
+by its index, and another that will remove the first element that matches the passed value.  So what should we 
+do when we have a list of integers?  In this case, you may wish to use `removeAt` to remove an element by its
+index, and `removeElement` to remove the first element that matches a value.
+
+[source,groovy]
+----------------------------------------------------------------------------
+include::{projectdir}/src/spec/test/gdk/WorkingWithCollectionsTest.groovy[tags=list_gdk4,indent=0]
+----------------------------------------------------------------------------
+
+Of course, `removeAt` and `removeElement` will work with lists of any type.
+
+Additionally, removing all the elements in a list can be done by calling the `clear` method:
 
 [source,groovy]
 ----------------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/groovy/blob/091a8b7b/src/spec/test/gdk/WorkingWithCollectionsTest.groovy
----------------------------------------------------------------------
diff --git a/src/spec/test/gdk/WorkingWithCollectionsTest.groovy b/src/spec/test/gdk/WorkingWithCollectionsTest.groovy
index 46e5d51..a117ab1 100644
--- a/src/spec/test/gdk/WorkingWithCollectionsTest.groovy
+++ b/src/spec/test/gdk/WorkingWithCollectionsTest.groovy
@@ -238,10 +238,28 @@ class WorkingWithCollectionsTest extends GroovyTestCase {
         '''
 
         assertScript '''
+            // tag::list_gdk_remove_index[]
+            def list = ['a','b','c','d','e','f','b','b','a']
+            assert list.remove(2) == 'c'        // remove the third element, and return it
+            assert list == ['a','b','d','e','f','b','b','a']
+            // end::list_gdk_remove_index[]
+        '''
+        
+        assertScript '''
             // tag::list_gdk4[]
             def list = [1,2,3,4,5,6,2,2,1]
-            assert list.remove(2) == 3          // remove the third element, and return it
+
+            assert list.remove(2) == 3          // this removes the element at index 2, and returns it
             assert list == [1,2,4,5,6,2,2,1]
+
+            assert list.removeElement(2)        // remove first 2 and return true
+            assert list == [1,4,5,6,2,2,1]
+
+            assert ! list.removeElement(8)      // return false because 8 is not in the list
+            assert list == [1,4,5,6,2,2,1]
+
+            assert list.removeAt(1) == 4        // remove element at index 1, and return it
+            assert list == [1,5,6,2,2,1]
             // end::list_gdk4[]
         '''
 


[2/2] groovy git commit: Fix typo

Posted by jw...@apache.org.
Fix typo


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/5e4c92ff
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/5e4c92ff
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/5e4c92ff

Branch: refs/heads/master
Commit: 5e4c92ff7965f4922ca173919599317a2e01d2aa
Parents: 091a8b7
Author: John Wagenleitner <jw...@apache.org>
Authored: Sun Mar 26 13:23:51 2017 -0700
Committer: John Wagenleitner <jw...@apache.org>
Committed: Sun Mar 26 13:23:51 2017 -0700

----------------------------------------------------------------------
 src/spec/doc/working-with-collections.adoc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/5e4c92ff/src/spec/doc/working-with-collections.adoc
----------------------------------------------------------------------
diff --git a/src/spec/doc/working-with-collections.adoc b/src/spec/doc/working-with-collections.adoc
index ace44ed..0153983 100644
--- a/src/spec/doc/working-with-collections.adoc
+++ b/src/spec/doc/working-with-collections.adoc
@@ -146,7 +146,7 @@ include::{projectdir}/src/spec/test/gdk/WorkingWithCollectionsTest.groovy[tags=l
 ----------------------------------------------------------------------------
 
 In case you only want to remove the first element having the same value in a list, instead of removing all
-elements, you call call the `remove` method passing the value:
+elements, you can call the `remove` method passing the value:
 
 [source,groovy]
 ----------------------------------------------------------------------------