2 summary:: A function that composes multiple functions into one
3 categories::Core>Kernel
7 A 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.
9 See the link::Reference/Functions:: help file for a basic introduction.
13 fork { loop { 0.7.wait; a.value.postln } };
14 a.addFunc({ 800.rand });
15 a.addFunc({ "another".scramble });
22 create a new instance.
24 An array of functions or objects
30 Set/get the FunctionList's array. New functions can be added to the array directly, e.g.
32 x = FunctionList(...some functions);
33 x.array = x.array.insert(2, aFunction);
38 This message is used to be able to add to an Object, to a Function, or to a FunctionList.
39 code::nil.addFunc:: returns a function, if only one function is passed in the argument.
40 code::function.addFunc:: then returns a FunctionList.
44 remove a function from the list.
46 returns:: the last function when only one function is left, or code::nil:: when the last function was removed.
49 code::addFunc:: and code::removeFunc:: are implemented for link::Classes/Nil::, link::Classes/Object:: and link::Classes/FunctionList::
52 nil.addFunc(f) // returns f
53 obj.addFunc(f) // returns FunctionList([obj, f])
54 nil.removeFunc(f) // returns nil
55 obj.removeFunc(f) // returns nil, if f === obj, else obj is returned
64 a = a.addFunc { |x="", y=""| "this % is an % example\n".postf(x, y); 1 };
66 a = a.addFunc { |x="", y=""| "there is no % that is %\n".postf(x, y); 2 };
68 a.value("text", "extraordinary well written")
69 a.valueArray(["x", "y"]);
73 // Function:do vs FunctionList:do (same)
75 { 4 }.do { |x| x.value.postln }
88 // removing a function
89 x = { "removal test".postln };
97 a = a.addFunc({ 1.0.rand }).addFunc({ [0, 1].choose });
98 a = a.squared.linexp(0, 1, 1.0, 500);
104 // compatibility with function multichannel expansion
106 a = a.addFunc { |x=0| if(x > 0) { 7 } { 1000.rand } };
107 a = a.addFunc { |x=0| if(x < 0) { 17 } { -1000.rand } };
116 // typical usage in a Document action
117 // see also SCView: addAction example.
119 d = Document.current;
120 d.keyDownAction = { "You touched the keyboard.".postln };
122 d.keyDownAction = d.keyDownAction.addFunc {:x, x<-(1..), :: "already % times\n\n".postf(x) };
125 d.keyDownAction = nil;
127 // even if you don't know if there is already an action defined
131 d.keyDownAction = nil;
132 d.keyDownAction = d.keyDownAction.addFunc {:x, x<-(1..), :: "already % times\n\n".postf(x) };
136 d.keyDownAction = nil;