SequenceableCollection is a subclass of Collection whose elements can be indexed by an Integer. It has many useful subclasses; Array and List are amongst the most commonly used.
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.
Array.fill(4, { arg i; i * 2 }); Bag.fill(14, { arg i; i.rand });
size |
The size of the collection which is returned. If nil, it returns an empty collection. If an array of sizes is given, the resulting collection has the appropriate dimensions (see: *fillND). Array.fill([2, 2, 3], { arg i, j, k; i * 100 + (j * 10) + k }); |
function |
The function which is called for each new element - the index is passed in as a first argument. The function be anything that responds to the message "value". Array.fill(10, { arg i; 2 ** i }); Array.fill(10, Pxrand([0, 1, 2], inf).iter); Array.fill(10, 7); // an object that doesn't respond with a new value is just repeatedly added. |
Fill a SequenceableCollection with an arithmetic series.
Array.series(5, 10, 2);
Fill a SequenceableCollection with a geometric series.
Array.geom(5, 1, 3);
Fill a SequenceableCollection with a fibonacci series.
Array.fib(5); Array.fib(5, 2, 32); // start from 32 with step 2.
size |
the number of values in the collection |
a |
the starting step value |
b |
the starting value |
Fill a SequenceableCollection with random values in the range minVal to maxVal.
Array.rand(8, 1, 100);
Fill a SequenceableCollection with random values in the range -val to +val.
Array.rand2(8, 100);
Fill a SequenceableCollection with random values in the range minVal to maxVal with a linear distribution.
Array.linrand(8, 1, 100);
Fill a SequenceableCollection with random values in the range minVal to maxVal with exponential distribution.
Array.exprand(8, 1, 100);
Fill a SequenceableCollection with the interpolated values between the start and end values.
Array.interpolation(5, 3.2, 20.5);
synonym for ArrayedCollection: -clipAt.
[3, 4, 5]|@|6;
synonym for ArrayedCollection: -wrapAt.
[3, 4, 5]@@6; [3, 4, 5]@@ -1; [3, 4, 5]@@[6, 8]
synonym for ArrayedCollection: -foldAt.
[3, 4, 5]@|@[6, 8];
Return the first element of the collection.
[3, 4, 5].first;
Return the last element of the collection.
[3, 4, 5].last;
Place item at the first / last index in the collection. Note that if the collection is empty (and therefore has no indexed slots) the item will not be added.
[3, 4, 5].putFirst(100); [3, 4, 5].putLast(100);
Return the index of an item in the collection, or nil if not found.
[3, 4, 100, 5].indexOf(100); [3, 4, \foo, \bar].indexOf(\foo);
Return the index of something in the collection that equals the item, or nil if not found.
[3, 4, "foo", "bar"].indexOfEqual("foo");
Return an array of indices of things in the collection that equal the item, or nil if not found.
y = [7, 8, 7, 6, 5, 6, 7, 6, 7, 8, 9]; y.indicesOfEqual(7); y.indicesOfEqual(5);
Return the first index containing an item which is greater than item.
y = List[ 10, 5, 77, 55, 12, 123]; y.indexOfGreaterThan(70);
Return a new collection of same type as receiver which consists of all indices of those elements of the receiver for which function answers true
. The function is passed two arguments, the item and an integer index.
#[a, b, c, g, h, h, j, h].selectIndices({|item, i| item === \h})
If you want to control what type of collection is returned, use -selectIndicesAs
Return a new collection of type class which consists of all indices of those elements of the receiver for which function answers true
. The function is passed two arguments, the item and an integer index.
#[a, b, c, g, h, h, j, h].selectIndicesAs({|item, i| item === \h}, Set)
Return a new collection of same type as receiver which consists of all indices of those elements of the receiver for which function answers false
. The function is passed two arguments, the item and an integer index.
#[a, b, c, g, h, h, j, h].rejectIndices({|item, i| item === \h})
If you want to control what type of collection is returned, use -rejectIndicesAs
Return a new collection of type class which consists of all indices of those elements of the receiver for which function answers false
. The function is passed two arguments, the item and an integer index.
#[a, b, c, g, h, h, j, h].rejectIndicesAs({|item, i| item === \h}, Set)
Answer the index of the maximum of the results of function evaluated for each item in the receiver. The function is passed two arguments, the item and an integer index. If function is nil, then answer the maximum of all items in the receiver.
List[1, 2, 3, 4].maxIndex({ arg item, i; item + 10 }); [3.2, 12.2, 13, 0.4].maxIndex;
Answer the index of the minimum of the results of function evaluated for each item in the receiver. The function is passed two arguments, the item and an integer index. If function is nil, then answer the minimum of all items in the receiver.
List[1, 2, 3, 4].minIndex({ arg item, i; item + 10 }); List[3.2, 12.2, 13, 0.4].minIndex;
If the sublist exists in the receiver (in the specified order), at an offset greater than or equal to the initial offset, then return the starting index.
y = [7, 8, 7, 6, 5, 6, 7, 6, 7, 8, 9]; y.find([7, 6, 5]);
Similar to -find but returns an array of all the indices at which the sequence is found.
y = [7, 8, 7, 6, 5, 6, 7, 6, 7, 8, 9]; y.findAll([7, 6]);
Returns the closest index of the value in the collection (collection must be sorted).
[2, 3, 5, 6].indexIn(5.2);
Returns a linearly interpolated float index for the value (collection must be sorted). Inverse operation is -blendAt.
x = [2, 3, 5, 6].indexInBetween(5.2); [2, 3, 5, 6].blendAt(x);
Returns a linearly interpolated value between the two closest indices. Inverse operation is -indexInBetween.
x = [2, 5, 6].blendAt(0.4);
Return a new SequenceableCollection which is a copy of the indexed slots of the receiver from start to end. If end < start, an empty collection is returned.
( var y, z; z = [1, 2, 3, 4, 5]; y = z.copyRange(1, 3); z.postln; y.postln; )
x.copyRange(a, b)
is not equivalent to x[a..b]
. The latter compiles to ArrayedCollection: -copySeries, which has different behavior when end < start.Return a new SequenceableCollection which is a copy of the indexed slots of the receiver from start to the end of the collection. x.copyToEnd(a)
can also be written as x[a..]
Return a new SequenceableCollection which is a copy of the indexed slots of the receiver from the start of the collection to end. x.copyFromStart(a)
can also be written as x[..a]
Remove item from collection.
Remove and return item from collection. The last item in the collection will move to occupy the vacated slot (and the collection size decreases by one). See also takeAt, defined for ArrayedCollection: -takeAt.
a = [11, 12, 13, 14, 15]; a.take(12); a;
Retrieve an element from a given index (like SequenceableCollection: -at). This method is also implemented in Object, so that you can use it in situations where you don't want to know if the receiver is a collection or not. See also: SequenceableCollection: -instill
index |
The index at which to look for an element |
default |
If index exceeds collection size, or receiver is nil, return this instead ( a = [10, 20, 30]; b = [10, 20]; c = 7; ); // obtain third element, if outside bounds return 1 a.obtain(2, 1); b.obtain(2, 1); c.obtain(2, 1); |
Put an element at a given index (like SequenceableCollection: -put). This method is also implemented in Object, so that you can use it in situations where you don't want to know if the receiver is a collection or not. It will always return a new collection. See also: SequenceableCollection: -obtain
index |
The index at which to put the item |
item |
The object to put into the new collection |
default |
If the index exceeds the current collection's size, extend the collection with this element ( a = [10, 20, 30, 40]; b = [10, 20]; c = 7; ); a.instill(2, -1); b.instill(2, -1); c.instill(2, -1); // providing a default value c.instill(2, -1, 0); |
Keep the first n items of the array. If n is negative, keep the last -n items.
a = [1, 2, 3, 4, 5]; a.keep(3); a.keep(-3);
Drop the first n items of the array. If n is negative, drop the last -n items.
a = [1, 2, 3, 4, 5]; a.drop(3); a.drop(-3);
Returns a String formed by connecting all the elements of the receiver, with joiner inbetween. See also String: -split as the complementary operation.
["m", "ss", "ss", "pp", ""].join("i").postcs; "mississippi".split("i").postcs;
Returns a collection from which all nesting has been flattened.
[[1, 2, 3], [[4, 5], [[6]]]].flat; // [ 1, 2, 3, 4, 5, 6 ] [1, 2, [3, 4, [5, 6, [7, 8, [9, 0]]]]].flat; // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ]
Returns a collection from which numLevels of nesting has been flattened.
numLevels |
Specifies how many levels downward (inward) to flatten. Zero returns the original. a = [1, 2, [3, 4, [5, 6, [7, 8, [9, 0]]]]]; a.flatten(1); // [ 1, 2, [ 3, 4, [ 5, 6, [ 7, 8, [ 9, 0 ] ] ] ] ] a.flatten(2); // [ 1, 2, 3, 4, 5, 6, [ 7, 8, [ 9, 0 ] ] ] a.flatten(3); // [ 1, 2, 3, 4, 5, 6, 7, 8, [ 9, 0 ] ] a.flatten(4); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ] |
A symmetric version of -flatten. For a negative numLevels
, it flattens starting from the innermost arrays.
numLevels |
Specifies how many levels downward (inward) or upward (outward) to flatten. a = [1, 2, [3, 4, [5, 6, [7, 8, [9, 0]]]]]; a.flatten2(4); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ] a.flatten2(3); // [ 1, 2, 3, 4, 5, 6, 7, 8, [ 9, 0 ] ] a.flatten2(2); // [ 1, 2, 3, 4, 5, 6, [ 7, 8, [ 9, 0 ] ] ] a.flatten2(1); // [ 1, 2, 3, 4, [ 5, 6, [ 7, 8, [ 9, 0 ] ] ] ] a.flatten2(0); // [ 1, 2, [ 3, 4, [ 5, 6, [ 7, 8, [ 9, 0 ] ] ] ] ] a.flatten2(-1); // [ 1, 2, [ 3, 4, [ 5, 6, [ 7, 8, 9, 0 ] ] ] ] a.flatten2(-2); // [ 1, 2, [ 3, 4, [ 5, 6, 7, 8, 9, 0 ] ] ] a.flatten2(-3); // [ 1, 2, [ 3, 4, 5, 6, 7, 8, 9, 0 ] ] a.flatten2(-4); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ] |
Flatten all subarrays deeper than level.
level |
Specifies from what level onward to flatten. level 0 is outermost, so flatBelow(0) is like flat. a = [1, 2, [3, 4, [5, 6, [7, 8, [9, 0]]]]]; a.flatBelow(0); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ] a.flatBelow(1); // [ 1, 2, [ 3, 4, 5, 6, 7, 8, 9, 0 ] ] a.flatBelow(2); // [ 1, 2, [ 3, 4, [ 5, 6, 7, 8, 9, 0 ] ] ] // to set the level below which to flatten from the deepest level up, // one can use coll.maxDepth. E.g. to flatten only the innermost level: a.flatBelow( (a.maxDepth - 1) - 1); // for lowest two levels: a.flatBelow( (a.maxDepth - 1) - 2); |
Invert rows and columns in a two dimensional Collection (turn inside out). See also: Function.
[[1, 2, 3], [4, 5, 6]].flop; [[1, 2, 3], [4, 5, 6], [7, 8]].flop; // shorter array wraps [].flop; // result is always 2-d.
Note that the innermost arrays are not copied:
a = [1, 2]; x = [[[a, 5], [a, 10]], [[a, 50, 60]]].flop; a[0] = pi; x // pi is everywhere
Flop with a user defined function. Can be used to collect over several collections in parallel.
[[1, 2, 3], [4, 5, 6]].flopWith(_+_); [[1, 2, 3], 1, [7, 8]].flopWith{ |a,b,c| a+b+c }; // shorter array wraps // typical use case (pseudocode) [synths, buffers].flopWith{ |a,b| a.set(\buf, b) }
func |
A function taking as many arguments as elements in the array. |
Invert rows and columns in a an array of dimensional Collections (turn inside out), so that they all match up in size, but remain separated.
( a = flopTogether( [[1, 2, 3], [4, 5, 6, 7, 8]] * 100, [[1, 2, 3], [4, 5, 6], [7, 8]], [1000] ) ); a.collect(_.size); // sizes are the same a.collect(_.shape) // shapes can be different
Fold dimensions in a multi-dimensional Collection (turn inside out).
rank |
The depth (dimension) from which the array is inverted inside-out. [[1, 2, 3], [[41, 52], 5, 6]].flopDeep(2); [[1, 2, 3], [[41, 52], 5, 6]].flopDeep(1); [[1, 2, 3], [[41, 52], 5, 6]].flopDeep(0); [[1, 2, 3], [[41, 52], 5, 6]].flopDeep; // without argument, flop from the deepest level [[[10, 100, 1000], 2, 3], [[41, 52], 5, 6]].flopDeep(2); // shorter array wraps [].flopDeep(1); // result is always one dimension higher. [[]].flopDeep(4); NOTE: Note that, just like in flop, the innermost arrays (deeper than rank) are not copied. a = [1, 2]; x = [[[a, 5], [a, 10]], [[a, 50, 60]]].flopDeep(1); a[0] = pi; x // pi is everywhere |
Returns the maximum size of all subarrays at a certain depth (dimension)
rank |
The depth at which the size of the arrays is measured [[1, 2, 3], [[41, 52], 5, 6], 1, 2, 3].maxSizeAtDepth(2); [[1, 2, 3], [[41, 52], 5, 6], 1, 2, 3].maxSizeAtDepth(1); [[1, 2, 3], [[41, 52], 5, 6], 1, 2, 3].maxSizeAtDepth(0); [].maxSizeAtDepth(0); [[]].maxSizeAtDepth(0); [[]].maxSizeAtDepth(1); |
Returns the maximum depth of all subarrays.
max |
Internally used only. [[1, 2, 3], [[41, 52], 5, 6], 1, 2, 3].maxDepth |
Returns true if the collection is an arithmetic series.
step |
Step size to look for. If none is given, any step size will match. [ 0, 1, 2, 3, 4, 5 ].isSeries; // true [ 1.5, 2.5, 3.5, 4.5, 5.5, 6.5 ].isSeries; // true [ 0, 1, 4, 5 ].isSeries; // false [ 0, 3, 6, 9, 12, 15 ].isSeries; // true [ 0, 3, 6, 9, 12, 15 ].isSeries(1); // false [ 2 ] // true [ ] // true (empty sequence) |
Returns a new Collection of the desired length, with values resampled evenly-spaced from the receiver without interpolation.
[1, 2, 3, 4].resamp0(12); [1, 2, 3, 4].resamp0(2);
Returns a new Collection of the desired length, with values resampled evenly-spaced from the receiver with linear interpolation.
[1, 2, 3, 4].resamp1(12); [1, 2, 3, 4].resamp1(3);
Choose an element from the collection at random.
[1, 2, 3, 4].choose;
Choose an element from the collection at random using a list of probabilities or weights. The weights must sum to 1.0.
[1, 2, 3, 4].wchoose([0.1, 0.2, 0.3, 0.4]);
Sort the contents of the collection using the comparison function argument. The function should take two elements as arguments and return true if the first argument should be sorted before the second argument. If the function is nil, the following default function is used. { arg a, b; a < b }
[6, 2, 1, 7, 5].sort; [6, 2, 1, 7, 5].sort({ arg a, b; a > b }); // reverse sort
Sort the contents of the collection using the key key, which is assumed to be found inside each element of the receiver.
( a = [ Dictionary[\a->5, \b->1, \c->62], Dictionary[\a->2, \b->9, \c->65], Dictionary[\a->8, \b->5, \c->68], Dictionary[\a->1, \b->3, \c->61], Dictionary[\a->6, \b->7, \c->63] ] ) a.sortBy(\b); a.sortBy(\c);
Return an array of indices that would sort the collection into order. function is treated the same way as for the -sort method.
[6, 2, 1, 7, 5].order;
Swap two elements in the collection at indices i and j.
Calls function for each subsequent pair of elements in the SequentialCollection. The function is passed the two elements and an index.
[1, 2, 3, 4, 5].pairsDo({ arg a, b; [a, b].postln; });
Calls function for every adjacent pair of elements in the SequenceableCollection. The function is passed the two adjacent elements and an index.
[1, 2, 3, 4, 5].doAdjacentPairs({ arg a, b; [a, b].postln; });
Separates the collection into sub-collections by calling the function for each adjacent pair of elements. If the function returns true, then a separation is made between the elements.
[1, 2, 3, 5, 6, 8, 10].separate({ arg a, b; (b - a) > 1 }).postcs;
Separates the collection into sub-collections by separating every groupSize elements.
[1, 2, 3, 4, 5, 6, 7, 8].clump(3).postcs;
Separates the collection into sub-collections by separating elements into groupings whose size is given by integers in the groupSizeList.
[1, 2, 3, 4, 5, 6, 7, 8].clumps([1, 2]).postcs;
Separates the collection into sub-collections by randomly separating elements according to the given probability.
[1, 2, 3, 4, 5, 6, 7, 8].curdle(0.3).postcs;
Returns a collection with the incremental sums of all elements.
[3, 4, 1, 1].integrate;
Returns a collection with the pairwise difference between all elements.
[3, 4, 1, 1].differentiate;
Applies the method named by operator to the first and second elements of the collection, and then applies the method to the result and to the third element of the collection, then applies the method to the result and to the fourth element of the collection, and so on, until the end of the array.
If the collection contains only one element, it is returned as the result. If the collection is empty, returns nil
.
operator |
May be a Function (taking two or three arguments) or a Symbol (method selector). [3, 4, 5, 6].reduce('*'); // this is the same as [3, 4, 5, 6].product [3, 4, 5, 6].reduce(\lcm); // Lowest common multiple of the whole set of numbers ["d", "e", (0..9), "h"].reduce('++'); // concatenation [3, 4, 5, 6].reduce({ |a, b| sin(a) * sin(b) }); // product of sines |
adverb |
An optional adverb to be used together with the operator (see Adverbs for Binary Operators). If the operator is a functions, the adverb is passed as a third argument. // compare: [1, 2] *.x [10, 20, 30] [[1, 2], [10, 20, 30]].reduce('*', 'x') [[1, 2], [10, 20, 30], [1000, 2000]].reduce('+', 'x') // but you can combine more |
Returns an integer resulting from interpreting the elements as digits to a given base (default 10). See also asDigits in Integer: asDigits for the complementary method.
[1, 0, 0, 0].convertDigits; [1, 0, 0, 0].convertDigits(2); [1, 0, 0, 0].convertDigits(3);
Returns the count of array elements that are not equal in identical positions. http://en.wikipedia.org/wiki/Hamming_distance
The collections are not wrapped - if one array is shorter than the other, the difference in size should be included in the count.
[0, 0, 0, 1, 1, 1, 0, 1, 0, 0].hammingDistance([0, 0, 1, 1, 0, 0, 0, 0, 1, 1]); "SuperMan".hammingDistance("SuperCollider");
All of the following messages send the message -performUnaryOp to the receiver with the unary message selector as an argument.
Creates a new collection of the results of applying the selector to all elements in the receiver.
[1, 2, 3, 4].neg; [1, 2, 3, 4].reciprocal;
All of the following messages send the message -performBinaryOp to the receiver with the binary message selector and the second operand as arguments.
Creates a new collection of the results of applying the selector with the operand to all elements in the receiver. If the operand is a collection then elements of that collection are paired with elements of the receiver.
([1, 2, 3, 4] * 10); ([1, 2, 3, 4] * [4, 5, 6, 7]);
All of the following messages are performed on the elements of this collection, using Object: -multiChannelPerform.
The result depends on the objects in the collection, but the main use case is for UGens.
See also Multichannel Expansion
Calls this.multiChannelPerform(selector, *args)
where selector is the name of the message.
This method is called internally on inputs to UGens that take multidimensional arrays, like Klank and it allows proper multichannel expansion even in those cases. For SequenceableCollection, this returns the collection itself, assuming that it contains already a number of Refs. See Ref for the corresponding method implementation.
rank |
The depth at which the list is expanded. For instance the Klank spec has a rank of 2. For more examples, see SequenceableCollection: -flopDeep `([[[100, 200], 500], nil, [[[0.01, 0.3], 0.8]]]).multichannelExpandRef(2); [`[[100, 200], nil, [0.2, 0.8]], `[[130, 202], nil, [0.2, 0.5]]].multichannelExpandRef(2); |
Convert a rhythm-list to durations.
supports a variation of Mikael Laurson's rhythm list RTM-notation.1
The method converts a collection of the form [beat-count, [rtm-list], repeats]
to a List of Floats. A negative integer within the rtm-list equates to a value tied over to the duration following. The method is recursive in that any subdivision within the rtm-list can itself be a nested convertRhythm collection (see example below). The repeats integer has a default value of 1.
If the divisions in the rtm-list are events, the event durations are interpreted as relative durations, and a list of events is returned.
// using numbers as score [3, [1, 2, 1], 1].convertRhythm; // List[ 0.75, 1.5, 0.75 ] [2, [1, 3, [1, [2, 1, 1, 1]], 1, 3], 1].convertRhythm; [2, [1, [1, [2, 1, 1, 1]]], 1].convertRhythm; [2, [1, [1, [2, 1, 1, 1]]], 2].convertRhythm; // repeat [2, [1, [1, [2, 1, 1, -1]]], 2].convertRhythm; // negative value is tied over. // sound example Pbind(\degree, Pseries(0, 1, inf), \dur, Pseq([2, [1, [1, [2, 1, 1, -1]]], 2].convertRhythm)).play;