[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [sc-users] A bug in switch/case?
Thanks Alberto, for your answer. But can we do anything with
IdentityDictionary?
For example, the code below works, with switch:
switch (input,
"white", {in = WhiteNoise.ar(inmul, inadd);},
"pink", {in = PinkNoise.ar(inmul, inadd);},
"brown", {in = BrownNoise.ar(inmul, inadd);},
"gray", {in = GrayNoise.ar(inmul, inadd);},
"clip", {in = ClipNoise.ar(inmul, inadd);},
"crackle", {in = Crackle.ar(param, inmul, inadd);},
"dust", {in = Dust.ar(param, inmul, inadd);}
);
Then, I dug into the SC list, and found this code written by hjh:
c = IdentityDictionary[
{ x == 1 } -> { "case 1".postln },
{ x == 2 } -> { "case 2".postln },
{ x == 3 } -> { "case 3".postln }
];
I tried to write this:
inputdic = IdentityDictionary [
{input == "white"} -> {in = WhiteNoise.ar(inmul, inadd);},
{input == "pink"} -> {in = PinkNoise.ar(inmul,
inadd);},
{input == "brown"} -> {in = BrownNoise.ar(inmul, inadd);},
{input == "gray"} -> {in = GrayNoise.ar(inmul,
inadd);},
{input == "clip"} -> {in = ClipNoise.ar(inmul,
inadd);},
{input == "crackle"} -> {in = Crackle.ar(param, inmul,
inadd);},
{input == "dust"} -> {in = Dust.ar(param, inmul,
inadd);}
];
But, it doesn't seem to work. I'm just wondering whether my code is
wrong or there is an impossibility of doing this with
IdentityDictionary. I should add that input is not a variable but an
argument of a proto-synthdef function.
Ph.
Alberto de Campo wrote:
Hi Ph,
It is possible to use additional switch structures in case there are
more than 7 expressions to be evaluated, but I wonder what is the
advantage of using "case" or "switch" instead of an
IdentityDictionary. ??
AFAIR, people wanted to have switch, even against the advice that
dictionaries are better OOP style and faster. I just measured speed
again (on a PB G4/1.33 GHz):
(
var z, cases;
z = [0, 1, 1.1, 1.3, 1.5, 2, 3, 4, 5];
cases = [ 1, { \no },
1.1, { \wrong },
1.3, { \wrong },
1.5, { \wrong },
2, { \wrong },
0, { \true },
3, { \true }
];
)
{
100000.do {
z.choose.switch (*cases);
}
}.bench;
)
-> ca 1.4 to 1.75 sec.
(
var z, cases;
z = [0, 1, 1.1, 1.3, 1.5, 2, 3];
cases = IdentityDictionary[
1 -> { \no },
1.1 -> { \wrong },
1.3 -> { \wrong },
1.5 -> { \wrong },
2 -> { \wrong },
0 -> { \true },
3 -> { \true }
];
{
100000.do {
cases[z.choose].value;
}
}.bench;
)
-> ca 0.33 to 0.45 sec, and the same
same with Event instead of IdentityDictionary.
best,
adc