You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2018/08/08 14:25:43 UTC
[1/2] groovy git commit: GROOVY-8733: Groovy could provide a chop DGM
method (reworked slightly for consistency)
Repository: groovy
Updated Branches:
refs/heads/GROOVY_2_5_X 6cabae08f -> 7ad8f3884
GROOVY-8733: Groovy could provide a chop DGM method (reworked slightly for consistency)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/7ad8f388
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/7ad8f388
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/7ad8f388
Branch: refs/heads/GROOVY_2_5_X
Commit: 7ad8f388455950fbba6be7109cc4c07ae127208d
Parents: 5f84906
Author: Paul King <pa...@asert.com.au>
Authored: Wed Aug 8 23:09:05 2018 +1000
Committer: Paul King <pa...@asert.com.au>
Committed: Thu Aug 9 00:25:27 2018 +1000
----------------------------------------------------------------------
.../groovy/runtime/DefaultGroovyMethods.java | 46 ++++++++------------
1 file changed, 19 insertions(+), 27 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/7ad8f388/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 137f87f..afd363a 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -12529,13 +12529,13 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
}
/**
- * Chops the array into pieces, returning a piece for each chop size plus the remainder.
- * Returns a list of all the pieces leaving the original array intact.
- * If the array is too small, truncated (possibly empty) pieces are returned.
+ * Chops the array into pieces, returning lists with sizes corresponding to the supplied chop sizes.
+ * If the array isn't large enough, truncated (possibly empty) pieces are returned.
+ * Using a chop size of -1 will cause that piece to contain all remaining items from the array.
*
* @param self an Array to be chopped
- * @param chopSizes an Array of sizes
- * @return a list of lists chopping the original array into pieces determined by chopSizes
+ * @param chopSizes the sizes for the returned pieces
+ * @return a list of lists chopping the original array elements into pieces determined by chopSizes
* @see #collate(Object[], int) to chop a list into pieces of a fixed size
* @since 2.5.2
*/
@@ -12544,21 +12544,22 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
}
/**
- * Chops the Iterable into pieces, returning a piece for each chop size plus the remainder.
- * Returns a list of all the pieces leaving the original Iterable intact.
- * If the Iterable is too small, truncated (possibly empty) pieces are returned.
+ * Chops the Iterable into pieces, returning lists with sizes corresponding to the supplied chop sizes.
+ * If the Iterable isn't large enough, truncated (possibly empty) pieces are returned.
+ * Using a chop size of -1 will cause that piece to contain all remaining items from the Iterable.
* <p>
* Example usage:
* <pre class="groovyTestCase">
- * assert [1, 2, 3, 4].chop(1) == [[1], [2, 3, 4]]
- * assert ('a'..'h').chop(2, 4) == [['a', 'b'], ['c', 'd', 'e', 'f'], ['g', 'h']]
- * assert ['a', 'b', 'c', 'd', 'e'].chop(3) == [['a', 'b', 'c'], ['d', 'e']]
+ * assert [1, 2, 3, 4].chop(1) == [[1]]
+ * assert [1, 2, 3, 4].chop(1,-1) == [[1], [2, 3, 4]]
+ * assert ('a'..'h').chop(2, 4) == [['a', 'b'], ['c', 'd', 'e', 'f']]
+ * assert ['a', 'b', 'c', 'd', 'e'].chop(3) == [['a', 'b', 'c']]
* assert ['a', 'b', 'c', 'd', 'e'].chop(1, 2, 3) == [['a'], ['b', 'c'], ['d', 'e']]
* assert ['a', 'b', 'c', 'd', 'e'].chop(1, 2, 3, 3, 3) == [['a'], ['b', 'c'], ['d', 'e'], [], []]
* </pre>
*
* @param self an Iterable to be chopped
- * @param chopSizes an Array of sizes
+ * @param chopSizes the sizes for the returned pieces
* @return a list of lists chopping the original iterable into pieces determined by chopSizes
* @see #collate(Iterable, int) to chop an Iterable into pieces of a fixed size
* @since 2.5.2
@@ -12566,32 +12567,23 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
public static <T> List<List<T>> chop(Iterable<T> self, int... chopSizes) {
return chop(self.iterator(), chopSizes);
}
+
/**
- * Chops the iterator items into pieces, returning a piece for each chop size plus the remainder.
- * Returns a list of all the pieces.
+ * Chops the iterator items into pieces, returning lists with sizes corresponding to the supplied chop sizes.
* If the iterator is exhausted early, truncated (possibly empty) pieces are returned.
+ * Using a chop size of -1 will cause that piece to contain all remaining items from the iterator.
*
* @param self an Iterator to be chopped
- * @param chopSizes an Array of sizes
- * @return a list of lists chopping the original array into pieces determined by chopSizes
+ * @param chopSizes the sizes for the returned pieces
+ * @return a list of lists chopping the original iterator elements into pieces determined by chopSizes
* @since 2.5.2
*/
public static <T> List<List<T>> chop(Iterator<T> self, int... chopSizes) {
List<List<T>> result = new ArrayList<List<T>>();
for (Integer nextSize : chopSizes) {
int size = nextSize;
- if (size < 0) {
- throw new IllegalArgumentException("chop found negative chopSize: " + nextSize);
- }
- List<T> next = new ArrayList<T>();
- while (size-- > 0 && self.hasNext()) {
- next.add(self.next());
- }
- result.add(next);
- }
- if (self.hasNext()) {
List<T> next = new ArrayList<T>();
- while (self.hasNext()) {
+ while (size-- != 0 && self.hasNext()) {
next.add(self.next());
}
result.add(next);
[2/2] groovy git commit: GROOVY-8733: Groovy could provide a chop DGM
method
Posted by pa...@apache.org.
GROOVY-8733: Groovy could provide a chop DGM method
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/5f84906c
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/5f84906c
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/5f84906c
Branch: refs/heads/GROOVY_2_5_X
Commit: 5f84906c1947cf3e6eb3e9af4a81c3216cc1c627
Parents: 6cabae0
Author: Paul King <pa...@asert.com.au>
Authored: Wed Aug 8 18:42:44 2018 +1000
Committer: Paul King <pa...@asert.com.au>
Committed: Thu Aug 9 00:25:27 2018 +1000
----------------------------------------------------------------------
.../groovy/runtime/DefaultGroovyMethods.java | 71 ++++++++++++++++++++
1 file changed, 71 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/5f84906c/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 7c7d749..137f87f 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -12529,6 +12529,77 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
}
/**
+ * Chops the array into pieces, returning a piece for each chop size plus the remainder.
+ * Returns a list of all the pieces leaving the original array intact.
+ * If the array is too small, truncated (possibly empty) pieces are returned.
+ *
+ * @param self an Array to be chopped
+ * @param chopSizes an Array of sizes
+ * @return a list of lists chopping the original array into pieces determined by chopSizes
+ * @see #collate(Object[], int) to chop a list into pieces of a fixed size
+ * @since 2.5.2
+ */
+ public static <T> List<List<T>> chop(T[] self, int... chopSizes) {
+ return chop(Arrays.asList(self), chopSizes);
+ }
+
+ /**
+ * Chops the Iterable into pieces, returning a piece for each chop size plus the remainder.
+ * Returns a list of all the pieces leaving the original Iterable intact.
+ * If the Iterable is too small, truncated (possibly empty) pieces are returned.
+ * <p>
+ * Example usage:
+ * <pre class="groovyTestCase">
+ * assert [1, 2, 3, 4].chop(1) == [[1], [2, 3, 4]]
+ * assert ('a'..'h').chop(2, 4) == [['a', 'b'], ['c', 'd', 'e', 'f'], ['g', 'h']]
+ * assert ['a', 'b', 'c', 'd', 'e'].chop(3) == [['a', 'b', 'c'], ['d', 'e']]
+ * assert ['a', 'b', 'c', 'd', 'e'].chop(1, 2, 3) == [['a'], ['b', 'c'], ['d', 'e']]
+ * assert ['a', 'b', 'c', 'd', 'e'].chop(1, 2, 3, 3, 3) == [['a'], ['b', 'c'], ['d', 'e'], [], []]
+ * </pre>
+ *
+ * @param self an Iterable to be chopped
+ * @param chopSizes an Array of sizes
+ * @return a list of lists chopping the original iterable into pieces determined by chopSizes
+ * @see #collate(Iterable, int) to chop an Iterable into pieces of a fixed size
+ * @since 2.5.2
+ */
+ public static <T> List<List<T>> chop(Iterable<T> self, int... chopSizes) {
+ return chop(self.iterator(), chopSizes);
+ }
+ /**
+ * Chops the iterator items into pieces, returning a piece for each chop size plus the remainder.
+ * Returns a list of all the pieces.
+ * If the iterator is exhausted early, truncated (possibly empty) pieces are returned.
+ *
+ * @param self an Iterator to be chopped
+ * @param chopSizes an Array of sizes
+ * @return a list of lists chopping the original array into pieces determined by chopSizes
+ * @since 2.5.2
+ */
+ public static <T> List<List<T>> chop(Iterator<T> self, int... chopSizes) {
+ List<List<T>> result = new ArrayList<List<T>>();
+ for (Integer nextSize : chopSizes) {
+ int size = nextSize;
+ if (size < 0) {
+ throw new IllegalArgumentException("chop found negative chopSize: " + nextSize);
+ }
+ List<T> next = new ArrayList<T>();
+ while (size-- > 0 && self.hasNext()) {
+ next.add(self.next());
+ }
+ result.add(next);
+ }
+ if (self.hasNext()) {
+ List<T> next = new ArrayList<T>();
+ while (self.hasNext()) {
+ next.add(self.next());
+ }
+ result.add(next);
+ }
+ return result;
+ }
+
+ /**
* Compare the contents of this array to the contents of the given array.
*
* @param left an int array