-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwitch.sc
executable file
·93 lines (82 loc) · 1.78 KB
/
Switch.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Switch1 {
// by Chad Kirby <[email protected]>
*new { arg test ... cases;
var x;
x = cases.detect({ arg case, i;
var r;
r = case.at(0).valueArray( test );
(r == test) // if we're comparing values
or: {r.isKindOf(Boolean).if({r},{false})}
});
^x.notNil.if({x.at(1).valueArray( test ); true},{false})
}
}
Case {
*new { arg ... args;
^args
}
}
/*
Emulates a switch statement
by sekhar, modified by CK
*/
Switch {
var <onValue, hasExecuted, <result=nil;
// Instance Creation
*new {arg aValue;
^super.new.init(aValue);
}
// return empty string instead of nil
resultStr { ^result ? "" }
// initialize-release
init {arg aValue;
onValue = aValue;
hasExecuted = false;
}
// case stuff
case {arg test, block;
var result;
hasExecuted.if({^this});
result = test.valueArray(onValue);
((result == onValue) or: {result.isKindOf(Boolean).if({result},{false})}).if({ this.execute(block) });
}
caseIncludes {arg test, block;
var result;
hasExecuted.if({^this});
result = test.includes(onValue);
result.if({ this.execute(block) });
}
caseGT { arg test, block;
var result;
hasExecuted.if({^this});
result = (onValue > test);
result.if({ this.execute(block) });
}
caseGTE { arg test, block;
var result;
hasExecuted.if({^this});
result = (onValue >= test);
result.if({ this.execute(block) });
}
caseLT { arg test, block;
var result;
hasExecuted.if({^this});
result = (onValue < test);
result.if({ this.execute(block) });
}
caseLTE { arg test, block;
var result;
hasExecuted.if({^this});
result = (onValue <= test);
result.if({ this.execute(block) });
}
execute {arg block;
result = block.valueArray(onValue);
hasExecuted = true;
}
fall {
hasExecuted = false;
}
value { ^hasExecuted }
valueArray { ^hasExecuted }
}