/** * 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> * * @param self an aggregate Object with an Iterator returning its items * @return a Collection of the transformed values * @see Closure#IDENTITY * @since 1.8.5 */ public static Collection collect(Object self) { return collect(self, Closure.IDENTITY); }
/** * Iterates through this collection transforming each entry into a new value using Closure.IDENTITY * as a transformer, basically returning a list of items copied from the original collection. * <pre class="groovyTestCase">assert [1,2,3] == [1,2,3].collect()</pre> * * @param self an Iterable * @return a List of the transformed values * @see Closure#IDENTITY * @since 2.5.0 */ @SuppressWarnings("unchecked") public static <T> List<T> collect(Iterable<T> self) { return collect(self, (Closure<T>) Closure.IDENTITY); }
/** * Iterates through this collection transforming each entry into a new value using Closure.IDENTITY * as a transformer, basically returning a list of items copied from the original collection. * <pre class="groovyTestCase">assert [1,2,3] == [1,2,3].collect()</pre> * * @param self a collection * @return a List of the transformed values * @see Closure#IDENTITY * @since 1.8.5 * @deprecated use the Iterable version instead */ @Deprecated public static <T> List<T> collect(Collection<T> self) { return collect((Iterable<T>) self); }
/** * Iterates through this aggregate Object transforming each item into a new value using the * <code>transform</code> closure, returning a list of transformed values. * Example: * <pre class="groovyTestCase">def list = [1, 'a', 1.23, true ] * def types = list.collect { it.class } * assert types == [Integer, String, BigDecimal, Boolean]</pre> * * @param self an aggregate Object with an Iterator returning its items * @param transform the closure used to transform each item of the aggregate object * @return a List of the transformed values * @since 1.0 */ public static <T> List<T> collect(Object self, 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, 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.FirstGenericType.class) Closure<T> transform) { return (List<T>) collect(self, new ArrayList<T>(), transform); }
/** * Iterates through this Iterable transforming each entry into a new value using the <code>transform</code> closure * returning a list of transformed values. * <pre class="groovyTestCase">assert [2,4,6] == [1,2,3].collect { it * 2 }</pre> * * @param self an Iterable * @param transform the closure used to transform each item of the collection * @return a List of the transformed values * @since 2.5.0 */ public static <S,T> List<T> collect(Iterable<S> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<T> transform) { return (List<T>) collect(self.iterator(), transform); }
/** * 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 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 Map transforming each map entry into a new value using the <code>transform</code> closure * returning a list of transformed values. * <pre class="groovyTestCase">assert [a:1, b:2].collect { key, value -> key*value } == ["a", "bb"] * assert [3:20, 2:30].collect { entry -> entry.key * entry.value } == [60, 60]</pre> * * @param self a Map * @param transform the transformation closure which can take one (Map.Entry) or two (key, value) parameters * @return the resultant list of transformed values * @since 1.0 */ public static <T,K,V> List<T> collect(Map<K,V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<T> transform) { return (List<T>) collect(self, new ArrayList<T>(self.size()), transform); }
/** * Iterates through this collection transforming each entry into a new value using the <code>transform</code> closure * returning a list of transformed values. * * @param self a collection * @param transform the closure used to transform each item of the collection * @return a List of the transformed values * @deprecated use the Iterable version instead * @since 1.0 */ @Deprecated public static <S,T> List<T> collect(Collection<S> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<T> transform) { return (List<T>) collect(self, new ArrayList<T>(self.size()), transform); }
/** * Adds GroovyCollections#combinations(Iterable, Closure) as a method on collections. * <p> * Example usage: * <pre class="groovyTestCase">assert [[2, 3],[4, 5, 6]].combinations {x,y -> x*y } == [8, 12, 10, 15, 12, 18]</pre> * * @param self a Collection of lists * @param function a closure to be called on each combination * @return a List of the results of applying the closure to each combinations found * @see groovy.util.GroovyCollections#combinations(Iterable) * @since 2.2.0 */ public static List combinations(Iterable self, Closure<?> function) { return collect((Iterable)GroovyCollections.combinations(self), function); }
/** * Finds all permutations of an iterable, applies a function to each permutation and collects the result * into a list. * <p> * Example usage: * <pre class="groovyTestCase">Set result = [1, 2, 3].permutations { it.collect { v -> 2*v }} * assert result == [[6, 4, 2], [6, 2, 4], [2, 6, 4], [4, 6, 2], [4, 2, 6], [2, 4, 6]] as Set</pre> * * @param self the Iterable of items * @param function the function to apply on each permutation * @return the list of results of the application of the function on each permutation * @since 2.2.0 */ public static <T,V> List<V> permutations(Iterable<T> self, Closure<V> function) { return collect((Iterable<List<T>>) permutations(self),function); }
return DefaultGroovyMethods.collect(matcher, closure);
/** * 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 * @since 2.5.0 */ 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 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 Array * @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(S[] self, Collection<T> collector, @ClosureParams(FirstParam.Component.class) Closure<? extends T> transform) { return collect(new ArrayIterator<S>(self), collector, transform); }
public void testCollectList() { assertEquals(Arrays.asList(3, 4, 5), collect(animals, new Closure<Integer>(null) { public Integer doCall(String it) { return it.length(); } })); }
public void testListArithmetic() { List<List> numLists = new ArrayList<List>(); numLists.add(Arrays.asList(1, 2, 3)); numLists.add(Arrays.asList(10, 20, 30)); assertEquals(Arrays.asList(6, 60), collect(numLists, new Closure<Integer>(null) { public Integer doCall(Integer a, Integer b, Integer c) { return a + b + c; } })); Closure<Integer> arithmeticClosure = new Closure<Integer>(null) { public Integer doCall(Integer a, Integer b, Integer c) { return a * b + c; } }; Closure<Integer> tensAndUnits = arithmeticClosure.curry(10); assertEquals(35, (int) tensAndUnits.call(3, 5)); tensAndUnits = arithmeticClosure.ncurry(0, 10); assertEquals(35, (int) tensAndUnits.call(3, 5)); tensAndUnits = arithmeticClosure.ncurry(1, 10); assertEquals(35, (int) tensAndUnits.call(3, 5)); Closure<Integer> timesPlus5 = arithmeticClosure.rcurry(5); assertEquals(35, (int) timesPlus5.call(15, 2)); timesPlus5 = arithmeticClosure.ncurry(2, 5); assertEquals(35, (int) timesPlus5.call(15, 2)); }
/** * Iterates through this collection transforming each entry into a new value using the closure * as a transformer, returning a list of transformed values. * * @param self a collection * @param closure the closure used for mapping * @return a List of the transformed values * @since 1.0 */ public static List collect(Collection self, Closure closure) { return (List) collect(self, new ArrayList(self.size()), closure); }
public static<S extends Object, T extends Object >List<T> collect(Collection<S> self, Closure<T> transform) { if ((!Caller.isAsynchronous(self, "collect", transform))&&(!Caller.isAsynchronous(CpsDefaultGroovyMethods.class, "collect", self, transform))) { return DefaultGroovyMethods.collect(self, transform); } return CpsDefaultGroovyMethods.$collect__java_util_Collection__groovy_lang_Closure(self, transform); }
public static<T extends Object, K extends Object, V extends Object >List<T> collect(Map<K, V> self, Closure<T> transform) { if ((!Caller.isAsynchronous(self, "collect", transform))&&(!Caller.isAsynchronous(CpsDefaultGroovyMethods.class, "collect", self, transform))) { return DefaultGroovyMethods.collect(self, transform); } return CpsDefaultGroovyMethods.$collect__java_util_Map__groovy_lang_Closure(self, transform); }
public static<T extends Object >Collection<T> collect(Object self, Collection<T> collector, Closure<? extends T> transform) { if ((!Caller.isAsynchronous(self, "collect", collector, transform))&&(!Caller.isAsynchronous(CpsDefaultGroovyMethods.class, "collect", self, collector, transform))) { return DefaultGroovyMethods.collect(self, collector, transform); } return CpsDefaultGroovyMethods.$collect__java_lang_Object__java_util_Collection__groovy_lang_Closure(self, collector, transform); }