1class::FunctionList
2summary:: A function that composes multiple functions into one
3categories::Core>Kernel
4
5description::
6
7A FunctionList is a function that composes multiple functions into one. This allows allow to deal transparently with several functions as if they were one and to append new functions at a later point. The functions are evaluated in the order they have in the FunctionList's array, which is by default the order in which they have been added to it.
8
9See the link::Reference/Functions:: help file for a basic introduction.
10
11code::
12a = FunctionList.new;
13fork { loop { 0.7.wait; a.value.postln } };
14a.addFunc({ 800.rand });
15a.addFunc({ "another".scramble });
16::
17
18classMethods::
19
20method::new
21
22create a new instance.
23argument:: functions
24An array of functions or objects
25
26instanceMethods::
27
28method::array
29
30Set/get the FunctionList's array. New functions can be added to the array directly, e.g.
31code::
32x = FunctionList(...some functions);
33x.array = x.array.insert(2, aFunction);
34::
35
36method::addFunc
37
38This message is used to be able to add to an Object, to a Function, or to a FunctionList.
39code::nil.addFunc:: returns a function, if only one function is passed in the argument.
40code::function.addFunc:: then returns a FunctionList.
41
42method::removeFunc
43
44remove a function from the list.
45
46returns:: the last function when only one function is left, or code::nil:: when the last function was removed.
47
48discussion::
49code::addFunc:: and code::removeFunc:: are implemented for link::Classes/Nil::, link::Classes/Object:: and link::Classes/FunctionList::
50
51code::
52nil.addFunc(f) // returns f
53obj.addFunc(f) // returns FunctionList([obj, f])
54nil.removeFunc(f) // returns nil
55obj.removeFunc(f) // returns nil, if f === obj, else obj is returned
56::
57
58examples::
59
60code::
61// example
62
63a = nil;
64a = a.addFunc { |x="", y=""| "this % is an % example\n".postf(x, y); 1 };
65a.postln;
66a = a.addFunc { |x="", y=""| "there is no % that is %\n".postf(x, y); 2 };
67a.value;
68a.value("text", "extraordinary well written")
69a.valueArray(["x", "y"]);
70::
71
72code::
73// Function:do vs FunctionList:do (same)
74a.do { |x| x.value };
75{ 4 }.do { |x| x.value.postln }
76
77(
78().use {
79	~x = "array";
80	~y = "ominous";
81	a.valueEnvir;
82	a.valueEnvir("list");
83}
84)
85::
86
87code::
88// removing a function
89x = { "removal test".postln };
90a.addFunc(x);
91a.value;
92a = a.removeFunc(x);
93a.value;
94
95// mathematics
96a = nil;
97a = a.addFunc({ 1.0.rand }).addFunc({ [0, 1].choose });
98a = a.squared.linexp(0, 1, 1.0, 500);
99
100a.value;
101::
102
103code::
104// compatibility with function multichannel expansion
105a = nil;
106a = a.addFunc { |x=0| if(x > 0) { 7 } { 1000.rand } };
107a = a.addFunc { |x=0| if(x < 0) { 17 } { -1000.rand } };
108a.value
109
110a = a.flop;
111a.value
112a.value([-1, 1])
113::
114
115code::
116// typical usage in a Document action
117// see also SCView: addAction example.
118
119d = Document.current;
120d.keyDownAction = { "You touched the keyboard.".postln };
121
122d.keyDownAction = d.keyDownAction.addFunc {:x, x<-(1..), :: "already % times\n\n".postf(x) };
123
124
125d.keyDownAction = nil;
126
127// even if you don't know if there is already an action defined
128// one can add one.
129
130(
131d.keyDownAction = nil;
132d.keyDownAction = d.keyDownAction.addFunc {:x, x<-(1..), :: "already % times\n\n".postf(x) };
133
134);
135
136d.keyDownAction = nil;
137::
138
139
140
141