Maybe we need a help file, “How to mass-produce variables, and why you shouldn’t” 
The only way to kinda-sorta meaningfully mass produce variables is with environment vars.
100.do { |i|
("x" ++ i).asSymbol.envirPut(1000000.rand)
};
~x50 // ok
// get one randomly
("x" ++ 100.rand).asSymbol.envirGet
By contrast, if you use an array, creation is simpler: c = Array.fill(100, { 1000000.rand });
and indexed access is simpler: c[100.rand]
instead of… do you really want to write .asSymbol.envirGet
/ ...Put
every time?
The array is also about 8.5 times faster:
(
var size = 100;
var array = Array.fill(size, { 1000000.rand });
var env = Environment.make {
size.do { |i|
("x" ++ i).asSymbol.envirPut(1000000.rand)
};
};
"array access".postln;
bench { 1000000.do { array[size.rand] } };
"environment access".postln;
env.use {
bench { 1000000.do {
("x" ++ size.rand).asSymbol.envirGet
} };
};
)
array access
time to run: 0.043272551999507 seconds.
environment access
time to run: 0.37863402000039 seconds.
So, mass-producing environment variables gives you clunkier syntax and nearly an order of magnitude worse performance (edit: see note) – but it lets you once in a while write ~x50
instead of x[50]
. I’m not exactly sold.
Edit: Note – the worse performance above is mostly because of the string concatenation and symbol conversion. Using your own handwritten symbols in a dictionary, Environment or Event is very fast – it’s just that it takes more work to use Symbol keys in conjunction with numeric indices, and that slows things down. So – numeric indices ↔ arrays, symbols ↔ dictionaries.
It’s possible to hack interpreter variables:
// in interactive code, 'this' is the Interpreter object
// set vars a through j
(
10.do { |i|
this.perform(
(97 + i).asAscii.asString.asSymbol.asSetter,
100.rand
)
};
)
j // ok
But you only have 25 of those (if you respect the standard reservation of s
for the default server), and again, the syntax… well, you could actually drop .asString
because Char asSymbol
goes through Object which automatically .asString
-s it, but the flow is still the same: char → string → symbol → use in perform
. Not sure it’s worth all that.
The only way to mass-produce var
declarations is to make a string containing SC code and .compile
or .interpret
it – but those variables will be local to the compiled function, with no way to access them outside. So there’s not really much point, when it would be easier to just write the function in braces and use arrays.
hjh