You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by paulk-asert <gi...@git.apache.org> on 2017/12/11 15:17:26 UTC
[GitHub] groovy pull request #644: GROOVY-8406: Various DefaultGroovyMethods missing ...
GitHub user paulk-asert opened a pull request:
https://github.com/apache/groovy/pull/644
GROOVY-8406: Various DefaultGroovyMethods missing Array variants resu…
…lting in no type inference
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/paulk-asert/groovy groovy8406
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/groovy/pull/644.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #644
----
commit 1d5676705a3a39a5f0c73d5a50a60804c5de80a3
Author: paulk <pa...@asert.com.au>
Date: 2017-12-11T15:14:26Z
GROOVY-8406: Various DefaultGroovyMethods missing Array variants resulting in no type inference
----
---
[GitHub] groovy pull request #644: GROOVY-8406: Various DefaultGroovyMethods missing ...
Posted by shils <gi...@git.apache.org>.
Github user shils commented on a diff in the pull request:
https://github.com/apache/groovy/pull/644#discussion_r156211571
--- Diff: src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java ---
@@ -3371,77 +3396,179 @@ public static Number count(byte[] self, Object value) {
}
/**
- * Iterates through this aggregate Object transforming each item into a new value using Closure.IDENTITY
- * as a transformer, basically returning a list of items copied from the original object.
- * <pre class="groovyTestCase">assert [1,2,3] == [1,2,3].iterator().collect()</pre>
+ * Iterates through this aggregate Object transforming each item into a new value using the <code>transform</code> closure
+ * and adding it to the supplied <code>collector</code>.
*
- * @param self an aggregate Object with an Iterator returning its items
+ * @param self an aggregate Object with an Iterator returning its items
+ * @param collector the Collection to which the transformed values are added
+ * @param transform the closure used to transform each item of the aggregate object
+ * @return the collector with all transformed values added to it
+ * @since 1.0
+ */
+ public static <T> Collection<T> collect(Object self, Collection<T> collector, Closure<? extends T> transform) {
+ return collect(InvokerHelper.asIterator(self), collector, transform);
+ }
+
+ /**
+ * Iterates through this Array transforming each item into a new value using the
+ * <code>transform</code> closure, returning a list of transformed values.
+ *
+ * @param self an Array
+ * @param transform the closure used to transform each item of the Array
* @return a List of the transformed values
- * @see Closure#IDENTITY
- * @since 1.8.5
+ * @since 2.5.0
*/
- public static Collection collect(Object self) {
- return collect(self, Closure.IDENTITY);
+ public static <S,T> List<T> collect(S[] self, @ClosureParams(FirstParam.Component.class) Closure<T> transform) {
+ return collect(new ArrayIterator<S>(self), transform);
}
/**
- * Iterates through this aggregate Object transforming each item into a new value using the <code>transform</code> closure
+ * Iterates through this Array transforming each item into a new value using the <code>transform</code> closure
* and adding it to the supplied <code>collector</code>.
+ * <pre class="groovyTestCase">
+ * Integer[] nums = [1,2,3]
+ * List<Integer> answer = []
+ * nums.collect(answer) { it * 2 }
+ * assert [2,4,6] == answer
+ * </pre>
*
- * @param self an aggregate Object with an Iterator returning its items
+ * @param self an Array
* @param collector the Collection to which the transformed values are added
- * @param transform the closure used to transform each item of the aggregate object
+ * @param transform the closure used to transform each item
* @return the collector with all transformed values added to it
- * @since 1.0
+ * @since 2.5.0
*/
- public static <T> Collection<T> collect(Object self, Collection<T> collector, Closure<? extends T> transform) {
- for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext(); ) {
+ public static <S,T> Collection<T> collect(S[] self, Collection<T> collector, @ClosureParams(FirstParam.Component.class) Closure<? extends T> transform) {
+ return collect(new ArrayIterator<S>(self), collector, transform);
+ }
+
+ /**
+ * Iterates through this Iterator transforming each item into a new value using the
+ * <code>transform</code> closure, returning a list of transformed values.
+ *
+ * @param self an Iterator
+ * @param transform the closure used to transform each item
+ * @return a List of the transformed values
+ * @since 2.5.0
+ */
+ public static <S,T> List<T> collect(Iterator<S> self, @ClosureParams(FirstParam.Component.class) Closure<T> transform) {
+ return (List<T>) collect(self, new ArrayList<T>(), transform);
+ }
+
+ /**
+ * Iterates through this Iterator transforming each item into a new value using the <code>transform</code> closure
+ * and adding it to the supplied <code>collector</code>.
+ *
+ * @param self an Iterator
+ * @param collector the Collection to which the transformed values are added
+ * @param transform the closure used to transform each item
+ * @return the collector with all transformed values added to it
+ * @since 2.5.0
+ */
+ public static <S,T> Collection<T> collect(Iterator<S> self, Collection<T> collector, @ClosureParams(FirstParam.FirstGenericType.class) Closure<? extends T> transform) {
+ Iterator iter = InvokerHelper.asIterator(self);
--- End diff --
Is this necessary?
---
[GitHub] groovy pull request #644: GROOVY-8406: Various DefaultGroovyMethods missing ...
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/groovy/pull/644
---
[GitHub] groovy pull request #644: GROOVY-8406: Various DefaultGroovyMethods missing ...
Posted by paulk-asert <gi...@git.apache.org>.
Github user paulk-asert commented on a diff in the pull request:
https://github.com/apache/groovy/pull/644#discussion_r156233058
--- Diff: src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java ---
@@ -3371,77 +3396,179 @@ public static Number count(byte[] self, Object value) {
}
/**
- * Iterates through this aggregate Object transforming each item into a new value using Closure.IDENTITY
- * as a transformer, basically returning a list of items copied from the original object.
- * <pre class="groovyTestCase">assert [1,2,3] == [1,2,3].iterator().collect()</pre>
+ * Iterates through this aggregate Object transforming each item into a new value using the <code>transform</code> closure
+ * and adding it to the supplied <code>collector</code>.
*
- * @param self an aggregate Object with an Iterator returning its items
+ * @param self an aggregate Object with an Iterator returning its items
+ * @param collector the Collection to which the transformed values are added
+ * @param transform the closure used to transform each item of the aggregate object
+ * @return the collector with all transformed values added to it
+ * @since 1.0
+ */
+ public static <T> Collection<T> collect(Object self, Collection<T> collector, Closure<? extends T> transform) {
+ return collect(InvokerHelper.asIterator(self), collector, transform);
+ }
+
+ /**
+ * Iterates through this Array transforming each item into a new value using the
+ * <code>transform</code> closure, returning a list of transformed values.
+ *
+ * @param self an Array
+ * @param transform the closure used to transform each item of the Array
* @return a List of the transformed values
- * @see Closure#IDENTITY
- * @since 1.8.5
+ * @since 2.5.0
*/
- public static Collection collect(Object self) {
- return collect(self, Closure.IDENTITY);
+ public static <S,T> List<T> collect(S[] self, @ClosureParams(FirstParam.Component.class) Closure<T> transform) {
+ return collect(new ArrayIterator<S>(self), transform);
}
/**
- * Iterates through this aggregate Object transforming each item into a new value using the <code>transform</code> closure
+ * Iterates through this Array transforming each item into a new value using the <code>transform</code> closure
* and adding it to the supplied <code>collector</code>.
+ * <pre class="groovyTestCase">
+ * Integer[] nums = [1,2,3]
+ * List<Integer> answer = []
+ * nums.collect(answer) { it * 2 }
+ * assert [2,4,6] == answer
+ * </pre>
*
- * @param self an aggregate Object with an Iterator returning its items
+ * @param self an Array
* @param collector the Collection to which the transformed values are added
- * @param transform the closure used to transform each item of the aggregate object
+ * @param transform the closure used to transform each item
* @return the collector with all transformed values added to it
- * @since 1.0
+ * @since 2.5.0
*/
- public static <T> Collection<T> collect(Object self, Collection<T> collector, Closure<? extends T> transform) {
- for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext(); ) {
+ public static <S,T> Collection<T> collect(S[] self, Collection<T> collector, @ClosureParams(FirstParam.Component.class) Closure<? extends T> transform) {
+ return collect(new ArrayIterator<S>(self), collector, transform);
+ }
+
+ /**
+ * Iterates through this Iterator transforming each item into a new value using the
+ * <code>transform</code> closure, returning a list of transformed values.
+ *
+ * @param self an Iterator
+ * @param transform the closure used to transform each item
+ * @return a List of the transformed values
+ * @since 2.5.0
+ */
+ public static <S,T> List<T> collect(Iterator<S> self, @ClosureParams(FirstParam.Component.class) Closure<T> transform) {
+ return (List<T>) collect(self, new ArrayList<T>(), transform);
+ }
+
+ /**
+ * Iterates through this Iterator transforming each item into a new value using the <code>transform</code> closure
+ * and adding it to the supplied <code>collector</code>.
+ *
+ * @param self an Iterator
+ * @param collector the Collection to which the transformed values are added
+ * @param transform the closure used to transform each item
+ * @return the collector with all transformed values added to it
+ * @since 2.5.0
+ */
+ public static <S,T> Collection<T> collect(Iterator<S> self, Collection<T> collector, @ClosureParams(FirstParam.FirstGenericType.class) Closure<? extends T> transform) {
+ Iterator iter = InvokerHelper.asIterator(self);
--- End diff --
Good catch. Removed. I'll blame the head cold I've had for the past few days! :-)
---