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
|
/*translation of the methcall test from The Great Computer Language Shootout
*/
class Toggle {
bool=null
}
function Toggle::constructor(startstate) {
bool = startstate
}
function Toggle::value() {
return bool;
}
function Toggle::activate() {
bool = !bool;
return this;
}
class NthToggle extends Toggle {
count_max=null
count=0
}
function NthToggle::constructor(start_state,max_counter)
{
base.constructor(start_state);
count_max = max_counter
}
function NthToggle::activate ()
{
++count;
if (count >= count_max ) {
base.activate();
count = 0;
}
return this;
}
function main() {
local n = vargv.len()!=0?vargv[0].tointeger():1
local val = 1;
local toggle = Toggle(val);
local i = n;
while(i--) {
val = toggle.activate().value();
}
print(toggle.value() ? "true\n" : "false\n");
val = 1;
local ntoggle = NthToggle(val, 3);
i = n;
while(i--) {
val = ntoggle.activate().value();
}
print(ntoggle.value() ? "true\n" : "false\n");
}
local start=clock();
main();
print("TIME="+(clock()-start)+"\n");
|