I have written some functions that have multiple arguments. Now, I am realizing that I want to seed them individually (without heavily restructuring my code in order to put each function on its own thread). Is there an elegant way to do this given that routine only takes one argument? See below a simplified example of a solution that I came up with. Would be nice to be able to do something like func.seed(1234)
Thanks! They are currently not containing inner routines or scheduling, but eventually might be in that one function will be called by another each being on their own thread. Either way, they are getting called several times by different other functions. The example you gave seems to create a new seeded function every time g is called. For example, the following just returns the same thing over and over. Am I missing something?
Again… Thanks. That definitely works and the simpler answer is even nicer. Exactly what I was looking for. I guess I still don’t understand how the args of g are getting propagated to f. But it clearly works. And can be used for any arbitrary function. Really great solution.
Good to know! But still I am grateful for you helping me get to exactly what I was wanting. Though I was close, I was still not quite there. I love that the wrapper is function agnostic. If you get this message, could you kindly explain how the args of g are getting passed down to the args in f? I really have not seen that paradigm before. If not, not worries. I am really happy about the solution all the same.
I see now that calling g is the same as: ~wrapper.value(f, 1798).value(1, 50)
Which is clear that ~wrapper returns a function which takes in the two arguments. Very cool. I had not thought of that. You can disregard that last question ; )
I would also posit that this could be a really useful trick for a lot of different cases. Would be cool to have this as part of the function class and you could just do func.seed(1234) and it returned that wrapper function.
This is the nice thing you can do with closures. The trick is that the function that you return from the outer function has access to the outer one’s hidden state.
Here is an example with two inner functions:
// make a function that has hidden state
// and that returns two functions for setting or getting it
(
f = {
var hidden;
var set = { |x| hidden = x };
var get = { hidden };
[set, get]
};
)
a = f.value;
a[1].value; // nil
a[0].value(200);
a[1].value; // 200
Thanks! Also good to know. I am always hesitant to mess with classes because you have to recompile the class library and for things I am going to hand to other non-SuperCollider people, I try to make it as easy as possible. On that note, is there a way to programatically recompile the class library?
Yeah. For now, I implemented it as a function, but who knows in the future. Either way, it is good to know that you can programatically recompile the class library.