File: GenericCollectors.schelp

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (88 lines) | stat: -rw-r--r-- 3,020 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
title:: Generic Collectors
categories:: Language, Common methods
summary:: Methods that incrementally build up collections from nothing

You can see which classes implement a specific method by clicking on the method name.

There are a number of methods that incrementally build up (or reduce down) collections like arrays or events. Nil responds by creating a collection, so that variables do not need to be initialized. Nil is just the "ground" (default case) from which the rest is bootstrapped.

method::add
Returns an array with the value. This makes it unnecessary to initialize when adding to a variable.
code::
x = nil;
x = x.add(8);  // returns an array
x = x.add(7); // appends to the array
::

method::addAll
Returns an array with all the values. This makes it unnecessary to initialize when adding to a variable.
code::
x = nil;
x = x.addAll([0, 2, 1, 2]);  // returns an array
x = x.addAll(7); // single objects are converted to an array and appended
::

method::remove
For nil, it just returns nil. This makes it unnecessary to initialize when removing from a variable and adding to it again.
code::
x = nil;
x.remove(1); // stays nil, returns nil
x = x.addAll([0, 2, 1]);  // returns an array
x.remove(1); // returns 1
x;
::

method::++
Returns an array with all the values. This makes it unnecessary to initialize when adding to a variable.
code::
x = nil;
x = x ++ [7, 8, 9]; // returns the receiver
x = x ++ [3, 0, 1, 2]; // adds to the array
::
note:: Note that, unlike with addAll, the second operand must be a collection in order to function in this way.::

method::addFunc
Returns a function or a FunctionList.
This method is used to add multiple functions to already existing ones.
code::
f = nil;
f = f.addFunc { "----------****".scramble };
f = f.addFunc { 1.0.rand };
f.value;

// a typical use case for addFunc is where you have to add functionality to an already existing one.
(
w = Window("move me", Rect(300, 300, 600, 40)).front; w.addFlowLayout;
a = Slider(w, Rect(0, 0, 580, 30));
a.action = a.action.addFunc { "I am moved".postln };
)
a.action = a.action.addFunc { "I am so very moved".postln };
a.addFuncTo(\action, { "Now, I am finally really moved".postln }); // shorter alternative
::

method::removeFunc
This method is used to remove multiple functions from already existing ones. For Nil, it just returns itself.

code::
f = { 1.0.rand };
g = { "you have produced a random value".postln };
f = f.addFunc(g);
f.value;
f.removeFunc(g);
f.value;
::


method::transformEvent
This method is used to operate on events which are passed through the system as an argument.

code::
// for Nil: return the argument unmodified (an event).
nil.transformEvent((x: 8));
// for Dictionary (and thus for Event): add to the argument.
(y: 100, z: 1).transformEvent((x: 8));
// for Association: add the association to the event
(\a -> \x).transformEvent((x: 8));
// for Function: use the function receive the event as argument.
{ |event| event.use { ~x = ~x + 1 }; event }.transformEvent((x: 8));
::