// $Id: objinst.ici,v 1.0 2003/01/03 12:00:00 dada Exp $ // http://dada.perl.it/shootout // // contributed by Tim Long static Toggle = [class new(start_state) { t := this:^new(); t.state := start_state; return t; } activate() { state = !state; return this; } value() { return state; } ]; static NthToggle = [class:Toggle, new(start_state, count_max) { t := this:^new(start_state); t.count_max := count_max; t.counter := 0; return t; } activate() { this:^activate(); if (++counter >= count_max) { state = !state; counter = 0; } return this; } ]; n := argv[1] ? int(argv[1]) : 1; toggle := Toggle:new(1); for (i = 0; i < 5; ++i) printf(toggle:activate():value() ? "true\n" : "false\n"); for (i = 0; i < n; ++i) toggle := Toggle:new(1); printf("\n"); ntoggle := NthToggle:new(1, 3); for (i = 0; i < 8; ++i) printf(ntoggle:activate():value() ? "true\n" : "false\n"); for (i = 0; i < n; ++i) ntoggle := NthToggle:new(1, 3);