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.
15 create a new instance.
17 An array of functions or objects
23 Set/get the FunctionList's array. New functions can be added to the array directly, e.g.
25 x = FunctionList(...some functions);
26 x.array = x.array.insert(2, aFunction);
31 This message is used to be able to add to an Object, to a Function, or to a FunctionList.
32 code::nil.addFunc:: returns a function, if only one function is passed in the argument.
33 code::function.addFunc:: then returns a FunctionList.
37 remove a function from the list.
39 returns:: the last function when only one function is left, or code::nil:: when the last function was removed.
42 code::addFunc:: and code::removeFunc:: are implemented for link::Classes/Nil::, link::Classes/Object:: and link::Classes/FunctionList::
45 nil.addFunc(f) // returns f
46 obj.addFunc(f) // returns FunctionList([obj, f])
47 nil.removeFunc(f) // returns nil
48 obj.removeFunc(f) // returns nil, if f === obj, else obj is returned
57 a = a.addFunc { |x="", y=""| "this % is an % example\n".postf(x, y); 1 };
59 a = a.addFunc { |x="", y=""| "there is no % that is %\n".postf(x, y); 2 };
61 a.value("text", "extraordinary well written")
62 a.valueArray(["x", "y"]);
66 // Function:do vs FunctionList:do (same)
68 { 4 }.do { |x| x.value.postln }
81 // removing a function
82 x = { "removal test".postln };
90 a = a.addFunc({ 1.0.rand }).addFunc({ [0, 1].choose });
91 a = a.squared.linexp(0, 1, 1.0, 500);
97 // compatibility with function multichannel expansion
99 a = a.addFunc { |x=0| if(x > 0) { 7 } { 1000.rand } };
100 a = a.addFunc { |x=0| if(x < 0) { 17 } { -1000.rand } };
109 // typical usage in a Document action
110 // see also SCView: addAction example.
112 d = Document.current;
113 d.keyDownAction = { "You touched the keyboard.".postln };
115 d.keyDownAction = d.keyDownAction.addFunc {:x, x<-(1..), :: "already % times\n\n".postf(x) };
118 d.keyDownAction = nil;
120 // even if you don't know if there is already an action defined
124 d.keyDownAction = nil;
125 d.keyDownAction = d.keyDownAction.addFunc {:x, x<-(1..), :: "already % times\n\n".postf(x) };
129 d.keyDownAction = nil;