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! :-)
---