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:24:48 UTC
[2/2] groovy git commit: Adds documentation around 'remove' method
Integer/Object ambiguity (closes #514)
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/99aa7614
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/99aa7614
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/99aa7614
Branch: refs/heads/GROOVY_2_4_X
Commit: 99aa761456b881351ba7e00feff8437a9999f954
Parents: e621ca7
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:43:56 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/99aa7614/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/99aa7614/src/spec/test/gdk/WorkingWithCollectionsTest.groovy
----------------------------------------------------------------------
diff --git a/src/spec/test/gdk/WorkingWithCollectionsTest.groovy b/src/spec/test/gdk/WorkingWithCollectionsTest.groovy
index 3b432d3..42ebe86 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[]
'''