2 summary::Abstract superclass of all collections
3 related::Classes/List, Classes/Array, Classes/Dictionary, Classes/Bag, Classes/Set, Classes/SortedList
4 categories::Collections
7 Collection is an abstract class. You do not create direct instances of Collection.
8 There are many types of Collections including link::Classes/List::, link::Classes/Array::, link::Classes/Dictionary::, link::Classes/Bag::, link::Classes/Set::, link::Classes/SortedList::, etc. See link::Overviews/Collections:: for a complete class tree.
13 Creates a new Collection from another collection. This supports the interface for the method "as".
15 Array.newFrom(Set[4, 2, 1]);
16 Set.newFrom(Array[4, 2, 1]);
17 [1, 2, 3, 4, 3, 2].as(Set); // as(someClass) calls someClass.newFrom(this)
21 Creates a new Collection from the args.
27 Creates a Collection of the given size, the elements of which are determined by evaluation the given function. The function is passed the index as an argument.
29 Array.fill(4, { arg i; i * 2 });
37 Answers the number of objects contained in the Collection.
39 List[1, 2, 3, 4].size;
43 Answer whether the receiver contains no objects.
48 subsection::Adding and Removing
51 Add anObject to the receiver.
57 Add all items in aCollection to the receiver.
59 List[1, 2].addAll(List[3, 4]);
63 Remove anObject from the receiver. Answers the removed object.
74 Remove all items in aCollection from the receiver.
76 List[1, 2, 3, 4].removeAll(List[2, 3]);
78 note::that multiple items in the receiver will not necessarily be removed
80 ~closet = [\hat, \hat, \hat, \coat, \coat, \shoe, \shoe];
81 ~closet.removeAll([\hat, \coat, \shoe, \shoe]); // Doesn't empty the closet, just removes what we wanted to
83 See link::#-removeEvery:: for a related method that removes all occurrences.
87 Remove all occurrences of the items in aCollection from the receiver.
89 List[1, 2, 3, 2, 3, 2, 3, 4].removeEvery(List[2, 3]);
92 method::removeAllSuchThat
93 Remove all items in the receiver for which function answers link::Classes/True::. The function is passed two arguments, the item and an integer index. Answers the objects which have been removed.
98 a.removeAllSuchThat({ arg item, i; item < 3 });
104 Put the values in the corresponding indices given by keys. If one of the two argument arrays is longer then it will wrap.
106 y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
107 y.putEach([4, 7], [\smelly, \head]);
108 y.putEach([2, 3, 5, 6], \wotsits);
112 Return a collection of all the items for the keys.
121 Answer whether anObject is contained in the receiver.
123 List[1, 2, 3, 4].includes(3);
127 Answer whether any item in aCollection is contained in the receiver.
129 List[1, 2, 3, 4].includesAny(List[4, 5]);
133 Answer whether all items in aCollection are contained in the receiver.
135 List[1, 2, 3, 4].includesAll(List[4, 5]);
139 Returns link::Classes/True:: if this includes the strong::item::.
141 subsection::Iteration
144 Evaluates strong::function:: for each item in the collection. The function is passed two arguments, the item and an integer index.
146 List[1, 2, 3, 4].do({ arg item, i; item.postln });
150 Answer a new collection which consists of the results of strong::function:: evaluated for each item in the collection. The function is passed two arguments, the item and an integer index.
152 List[1, 2, 3, 4].collect({ arg item, i; item + 10 });
154 If you want to control what type of collection is returned, use link::#-collectAs::(function, class).
157 Answer a new collection which consists of all items in the receiver for which strong::function:: answers link::Classes/True::. The function is passed two arguments, the item and an integer index.
159 List[1, 2, 3, 4].select({ arg item, i; item.even });
161 If you want to control what type of collection is returned, use link::#-selectAs::(function, class).
164 Answer a new collection which consists of all items in the receiver for which strong::function:: answers link::Classes/False::. The function is passed two arguments, the item and an integer index.
166 List[1, 2, 3, 4].reject({ arg item, i; item.even });
168 If you want to control what type of collection is returned, use link::#-rejectAs::(function, class).
171 Answer the first item in the receiver for which strong::function:: answers link::Classes/True::. The function is passed two arguments, the item and an integer index.
173 List[1, 2, 3, 4].detect({ arg item, i; item.even });
177 Similar to link::#-detect:: but returns the index instead of the item itself.
179 List[1, 2, 3, 4].detectIndex({ arg item, i; item.even });
183 In functional programming, the operation known as a fold.
184 inject takes an initial value and a function and combines the elements of the collection by applying the function to the accumulated value and an element from the collection. The strong::function:: takes two arguments and returns the new value. The accumulated value is initialzed to strong::initialValue::.
186 [1,2,3,4,5].inject(0, _+_); // 15
188 [1,2,3,4,5].inject(1, _*_); // 120
190 // same as .collect(_.squared)
191 [1,2,3,4,5].inject([], {|a,b| a ++ b.squared }); // [ 1, 4, 9, 16, 25 ]
192 [1,2,3,4,5].inject([], {|a,b| [b] ++ a ++ [b]}); // [ 5, 4, 3, 2, 1, 1, 2, 3, 4, 5 ]
193 [1,2,3,4,5].inject([], {|a,b| a ++ b ++ a});
194 [1,2,3,4,5].inject([], {|a,b| a ++ a ++ b});
198 Answer whether strong::function:: answers link::Classes/True:: for any item in the receiver. The function is passed two arguments, the item and an integer index.
200 List[1, 2, 3, 4].any({ arg item, i; item.even });
204 Answer whether strong::function:: answers link::Classes/True:: for every item in the receiver. The function is passed two arguments, the item and an integer index.
206 List[1, 2, 3, 4].every({ arg item, i; item.even });
210 Answer the number of items for which strong::function:: answers link::Classes/True::. The function is passed two arguments, the item and an integer index.
212 List[1, 2, 3, 4].count({ arg item, i; item.even });
215 method::occurrencesOf
216 Answer the number of items in the receiver which are equal to anObject.
218 List[1, 2, 3, 3, 4, 3, 4, 3].occurrencesOf(3);
222 Answer the sum of the results of strong::function:: evaluated for each item in the receiver. The function is passed two arguments, the item and an integer index.
224 List[1, 2, 3, 4].sum;
225 (0..8).sum { |i| 1 / (2 ** i) };
229 Answer the maximum of the results of strong::function:: evaluated for each item in the receiver. The function is passed two arguments, the item and an integer index.
230 If function is nil, then answer the maximum of all items in the receiver.
232 List[1, 2, 3, 4].maxItem({ arg item, i; item + 10 });
236 Answer the minimum of the results of strong::function:: evaluated for each item in the receiver. The function is passed two arguments, the item and an integer index.
237 If function is nil, then answer the minimum of all items in the receiver.
239 List[1, 2, 3, 4].minItem({ arg item, i; item + 10 });
243 Answer the index of the maximum of the results of strong::function:: evaluated for each item in the receiver. The function is passed two arguments, the item and an integer index.
244 If function is nil, then answer the maximum of all items in the receiver.
246 List[1, 2, 3, 4].maxIndex({ arg item, i; item + 10 });
247 [3.2, 12.2, 13, 0.4].maxIndex;
251 Answer the index of the minimum of the results of strong::function:: evaluated for each item in the receiver. The function is passed two arguments, the item and an integer index.
252 If function is nil, then answer the minimum of all items in the receiver.
254 List[1, 2, 3, 4].minIndex({ arg item, i; item + 10 });
255 List[3.2, 12.2, 13, 0.4].minIndex;
259 Returns a link::Classes/Routine:: that returns the elements one by one.
261 r = Set[10, 2, -3, -4].iter;
268 subsection::Conversion
271 Answer a link::Classes/Bag:: to which all items in the receiver have been added.
273 List[1, 2, 3, 4].asBag;
277 Answer a link::Classes/List:: to which all items in the receiver have been added.
279 Set[1, 2, 3, 4].asList;
283 Answer a link::Classes/Set:: to which all items in the receiver have been added.
285 List[1, 2, 3, 4].asSet;
289 Answer a link::Classes/SortedList:: to which all items in the receiver have been added.
291 List[2, 1, 4, 3].asSortedList;
295 Returns all possible combinations of the collection's elements.
297 Set[1, 2, 3].powerset;
299 // generate the von neumann ordinals. (warning: only count to four at maximum!)
305 u = { |set| set.unify }; // union (count down)
306 n = { |set| set.powerset }; // powerset (count up)
307 a = Set[]; // empty set (zero)
309 u.(n.(n.(a))) == n.(a); // two - one == one
310 u.(u.(n.(n.(a)))) == u.(n.(a)); // two - two == one - one
314 Takes a collection of dictionaries and returns a single dictionary with arrays of all dictionaries' elements.
315 If unbubble is link::Classes/True:: (default), and if one element is singular, the array is replaced by this element.
317 [(degree: 7, x: 4), (degree: 8, x: 5), (degree: -2, dur: 2.5)].flopDict;
318 [(degree: 7, x: 4), (degree: 8, x: 5), (degree: -2, dur: 2.5)].flopDict(false);
322 Returns a histogram of the collection by counting the number of values that fall into each slot of size (default: 100) subdivisions between min and max. If there are any values outside this range, it posts a note. If min or max is not given, the smallest (or largest value respectively) is used.
324 { 1.0.linrand }.dup(10000).histo(1000).plot;
325 { 8.rand }.dup(10000).histo(8).plot(discrete: true);
328 subsection::Writing to streams
331 Print a representation of the collection to a stream.
334 Write a compileable representation of the collection to a stream.
337 Print a comma separated compileable representation of the items in the collection to a stream.
340 Write a comma separated compileable representation of the items in the collection to a stream.
342 subsection::Set specific operations
345 Return the set theoretical intersection of this and strong::that::.
347 a = [1, 2, 3]; b = [2, 3, 4, 5];
352 Return the set theoretical union of this and strong::that::.
354 a = [1, 2, 3]; b = [2, 3, 4, 5];
359 Return the set of all items which are elements of this, but not of strong::that::.
361 a = [1, 2, 3]; b = [2, 3, 4, 5];
365 method::symmetricDifference
366 Return the set of all items which are not elements of both this and strong::that::.
369 a = [1, 2, 3]; b = [2, 3, 4, 5];
370 symmetricDifference(a, b);
374 Returns link::Classes/True:: if all elements of this are also elements of strong::that::
377 Set[1, 2].isSubsetOf(a); // true
378 Set[1, 5].isSubsetOf(a); // false