Method Calls Back to the Win32 Shootout
Back to dada's perl lab

[The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform Details]   [Acknowledgements]   [Scorecard]  
All Source For Method Calls
methcall.bcc
/* -*- mode: c -*-
 * $Id: methcall.gcc,v 1.3 2001/06/14 12:58:39 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>

#define true  1
#define false 0


#define TOGGLE \
    char state; \
    char (*value)(struct Toggle *); \
    struct Toggle *(*activate)(struct Toggle *)

#define DESTROY  free

typedef struct Toggle {
    TOGGLE;
} Toggle;

char toggle_value(Toggle *this) {
    return(this->state);
}
Toggle *toggle_activate(Toggle *this) {
    this->state = !this->state;
    return(this);
}
Toggle *init_Toggle(Toggle *this, char start_state) {
    this->state = start_state;
    this->value = toggle_value;
    this->activate = toggle_activate;
    return(this);
}
Toggle *new_Toggle(char start_state) {
    Toggle *this = (Toggle *)malloc(sizeof(Toggle));
    return(init_Toggle(this, start_state));
}


typedef struct NthToggle {
    TOGGLE;
    int count_max;
    int counter;
} NthToggle;

NthToggle *nth_toggle_activate(NthToggle *this) {
    if (++this->counter >= this->count_max) {
    this->state = !this->state;
    this->counter = 0;
    }
    return(this);
}
NthToggle *init_NthToggle(NthToggle *this, int max_count) {
    this->count_max = max_count;
    this->counter = 0;
    this->activate = (Toggle *(*)(Toggle *))nth_toggle_activate;
    return(this);
}
NthToggle *new_NthToggle(char start_state, int max_count) {
    NthToggle *this = (NthToggle *)malloc(sizeof(NthToggle));
    this = (NthToggle *)init_Toggle((Toggle *)this, start_state);
    return(init_NthToggle(this, max_count));
}


int main(int argc, char *argv[]) {
    int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
    Toggle *tog;
    NthToggle *ntog;
    char val = true;

    tog = new_Toggle(true);
    for (i=0; i<n; i++) {
    val = tog->activate(tog)->value(tog);
    }
    fputs(val ? "true\n" : "false\n", stdout);
    DESTROY(tog);
    
    val = true;
    ntog = new_NthToggle(val, 3);
    for (i=0; i<n; i++) {
    val = ntog->activate(ntog)->value(ntog);
    }
    fputs(val ? "true\n" : "false\n", stdout);
    DESTROY(ntog);
    return 0;
}
methcall.bigforth
\ -*- mode: forth -*-
\ $Id: methcall.bigforth,v 1.2 2001/06/24 23:22:53 doug Exp $
\ http://www.bagley.org/~doug/shootout/

\ from Bernd Paysan:
\ I'm using oof.fs here (native OOF for bigforth), code using one of
\ the other OO Forth extensions will look different.

0. argc @ 1- arg >number 2drop drop constant NUM

include oof.fb

object class Toggle
    cell var state
  public:
    method activate 
    method value 
  how:
    : init   state ! ;
    : value   state @ ;
    : activate   state @ invert state ! ;
class;

Toggle class NthToggle
    cell var count-max
    cell var counter
  how:
    : init 
    super init  count-max ! 0 counter ! ;
    : activate 
    1 counter +!
    counter @ count-max @ >= if
        state @ invert state !
        0 counter !
    then ;
class;

: flag. 
    if ." true" else ." false" then cr ;

: mainloop 
    true NUM 0 ?do
    drop dup toggle with activate value endwith
    loop
    flag.  drop ;

: main 
    true Toggle new mainloop
    3 true NthToggle new mainloop ;

main bye
methcall.csharp
// $Id: methcall.csharp,v 1.0 2002/02/14 13:01:00 dada Exp $
// http://dada.perl.it/shootout/

using System;

class Toggle {
    public bool state = true;
    public Toggle(bool start_state) {
        this.state = start_state;
    }
    
    public bool value() {
        return(this.state);
    }
    
    public Toggle activate() {
        this.state = !this.state;
        return(this);
    }
}

class NthToggle : Toggle {
    int count_max = 0;
    int counter = 0;

    public NthToggle(bool start_state, int max_counter) : base(start_state) {
        this.count_max = max_counter;
        this.counter = 0;
    }
    public new NthToggle activate() {
        this.counter += 1;
        if (this.counter >= this.count_max) {
            this.state = !this.state;
            this.counter = 0;
        }
        return(this);
    }
}

class App {
    public static int Main(String[] args) {
        bool val = true;
        int n;

        Toggle toggle = new Toggle(val);
        NthToggle ntoggle = new NthToggle(true, 3);

        n = System.Convert.ToInt32(args[0]);
        if(n < 1) n = 1;

        for (int i=0; i<n; i++) {
            val = toggle.activate().value();
        }
        Console.WriteLine((val ? "true" : "false"));
        
        for (int i=0; i<n; i++) {
            val = ntoggle.activate().value();
        }
        Console.WriteLine((val ? "true" : "false"));
        
        return(0);
    }
}
methcall.cygperl
#!/usr/local/bin/perl
# $Id: methcall.perl,v 1.5 2001/05/27 16:44:24 doug Exp $
# http://www.bagley.org/~doug/shootout/
# with help from Ben Tilly

package Toggle;

sub new {
    my($class, $start_state) = @_;
    bless( { Bool => $start_state }, $class );
}

sub value {
    (shift)->{Bool};
}

sub activate {
    my $self = shift;
    $self->{Bool} ^= 1;
    return($self);
}


package NthToggle;
our @ISA = qw(Toggle);

sub new {
    my($class, $start_state, $max_counter) = @_;
    my $self = $class->SUPER::new($start_state);
    $self->{CountMax} = $max_counter;
    $self->{Counter} = 0;
    return($self);
}

sub activate {
    my $self = shift;
    if (++$self->{Counter} >= $self->{CountMax}) {
    $self->{Bool} ^= 1;
    $self->{Counter} = 0;
    }
    return($self);
}


package main;

sub main {
    my $NUM = $ARGV[0];
    $NUM = 1 if ($NUM < 1);

    my $val = 1;
    my $toggle = Toggle->new($val);
    for (1..$NUM) {
    $val = $toggle->activate->value;
    }
    print (($val) ? "true\n" : "false\n");

    $val = 1;
    my $ntoggle = NthToggle->new($val, 3);
    for (1..$NUM) {
    $val = $ntoggle->activate->value;
    }
    print (($val) ? "true\n" : "false\n");
}

main();
methcall.delphi
program methCall;


type
  TToggle = class(TObject)
  private
    FValue : boolean;
    function getValue: boolean;
  public
    constructor create(aValue: boolean);
    function Activate: TToggle; virtual;
    property Value: boolean read getValue;
  end;

  TNthToggle = class(TToggle)
  private
    FCounter, FCountMax : integer;
  public
    constructor create(aValue: boolean; aMaxCount: integer);
    function Activate: TToggle; override;
    property CountMax: integer read FCountMax write FCountMax;
  end;

constructor TToggle.Create(aValue: boolean);
begin
  FValue:=aValue;
end;

function TToggle.Activate: TToggle;
begin
  FValue:=not FValue;
  result:=self;
end;

function TToggle.getValue: boolean;
begin
  result:=FValue;
end;

constructor TNthToggle.Create(aValue: boolean; aMaxCount: integer);
begin
  inherited create(aValue);
  FCountMax:=aMaxCount;
end;

function TNthToggle.Activate: TToggle;
begin
  inc(FCounter);
  if FCounter>=CountMax then begin
    inherited Activate;
    FCounter:=0;
  end;
  result:=self;
end;

var NUM, code, i: integer;
    value: boolean;
    toggle: TToggle;
    nToggle: TNthToggle;

begin
  value:=false; // just to shut up the compiler
  NUM :=1;
  if (ParamCount=1) then Val(ParamStr(1),NUM,code);

  toggle := TToggle.Create(True);
  for i:=1 to NUM do
    value:=toggle.Activate.Value;
  toggle.Destroy;
  if value then Write('true'#13#10) else Write('false'#13#10);

  nToggle:=TNthToggle.Create(True,3);
  for i:=1 to NUM do
    value:=nToggle.Activate.Value;
  nToggle.Destroy;
  if value then Write('true'#13#10) else Write('false'#13#10);
end.

methcall.fpascal
program methcall;


uses SysUtils;

type TToggle = class 
    private
        value : boolean;

    public
        property Bool : boolean read value write value;
        procedure Activate;
end;    

type TNthToggle = class 
    constructor Create;
    private
        value : boolean;
        counter : integer;
        cmax : integer;
    public
        property CountMax : integer read cmax write cmax;
        property Bool : boolean read value write value;
        procedure Activate;
end;

constructor TNthToggle.Create;
begin
    counter := 0;
end;

procedure TToggle.Activate;
begin
    if value = True Then
        value := False
    else
        value := True;
end;

procedure TNthToggle.Activate;
begin
    counter := counter + 1;
    if counter >= cmax Then begin
        if value = True Then
            value := False
        Else
            value := True;
        counter := 0;
    end;
end;


var 
    NUM, i : longint;
    val : boolean;
    oToggle : TToggle;
    onToggle : TNthToggle;
begin
    if ParamCount = 0 then
        NUM := 1
    else
        NUM := StrToInt(ParamStr(1));
        
    if NUM < 1 then NUM := 1;

    val := True;
    oToggle := TToggle.Create;    
    oToggle.Bool := val;
    For i := 1 to NUM do
    begin
        oToggle.Activate;
        val := oToggle.Bool;
    end;
    If val = True Then
        WriteLn('true')
    else
        WriteLn('false');

    val := True;
    onToggle := TNthToggle.Create;
    onToggle.Bool := val;
    onToggle.CountMax := 3;
    For i := 1 to NUM do
    begin
        onToggle.Activate;
        val := onToggle.Bool;
    end;
    If val = True Then
        WriteLn('true')
    else
        WriteLn('false');
end.
methcall.gcc
/* -*- mode: c -*-
 * $Id: methcall.gcc,v 1.3 2001/06/14 12:58:39 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>

#define true  1
#define false 0


#define TOGGLE \
    char state; \
    char (*value)(struct Toggle *); \
    struct Toggle *(*activate)(struct Toggle *)

#define DESTROY  free

typedef struct Toggle {
    TOGGLE;
} Toggle;

char toggle_value(Toggle *this) {
    return(this->state);
}
Toggle *toggle_activate(Toggle *this) {
    this->state = !this->state;
    return(this);
}
Toggle *init_Toggle(Toggle *this, char start_state) {
    this->state = start_state;
    this->value = toggle_value;
    this->activate = toggle_activate;
    return(this);
}
Toggle *new_Toggle(char start_state) {
    Toggle *this = (Toggle *)malloc(sizeof(Toggle));
    return(init_Toggle(this, start_state));
}


typedef struct NthToggle {
    TOGGLE;
    int count_max;
    int counter;
} NthToggle;

NthToggle *nth_toggle_activate(NthToggle *this) {
    if (++this->counter >= this->count_max) {
    this->state = !this->state;
    this->counter = 0;
    }
    return(this);
}
NthToggle *init_NthToggle(NthToggle *this, int max_count) {
    this->count_max = max_count;
    this->counter = 0;
    this->activate = (Toggle *(*)(Toggle *))nth_toggle_activate;
    return(this);
}
NthToggle *new_NthToggle(char start_state, int max_count) {
    NthToggle *this = (NthToggle *)malloc(sizeof(NthToggle));
    this = (NthToggle *)init_Toggle((Toggle *)this, start_state);
    return(init_NthToggle(this, max_count));
}


int main(int argc, char *argv[]) {
    int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
    Toggle *tog;
    NthToggle *ntog;
    char val = true;

    tog = new_Toggle(true);
    for (i=0; i<n; i++) {
    val = tog->activate(tog)->value(tog);
    }
    fputs(val ? "true\n" : "false\n", stdout);
    DESTROY(tog);
    
    val = true;
    ntog = new_NthToggle(val, 3);
    for (i=0; i<n; i++) {
    val = ntog->activate(ntog)->value(ntog);
    }
    fputs(val ? "true\n" : "false\n", stdout);
    DESTROY(ntog);
    return 0;
}
methcall.gforth
\ -*- mode: forth -*-
\ $Id: methcall.gforth,v 1.2 2001/06/24 23:22:53 doug Exp $
\ http://www.bagley.org/~doug/shootout/

\ from Anton Ertl:
\ I'm using objects.fs here, code using one of the other OO Forth
\ extensions will look different.

warnings off \ don't complain about redefining catch, state, value

0. argc @ 1- arg >number 2drop drop constant NUM

require objects.fs

object class
    selector activate 
    selector value 
    cell% inst-var state

    m: 
    state ! ;m
    overrides construct
    
    m: 
    state @ ;m
    overrides value
    
    m: 
    state @ invert state !
    this ;m
    overrides activate

end-class Toggle

Toggle class
    cell% inst-var count-max
    cell% inst-var counter

    m: 
    this [parent] construct
    count-max !
    0 counter ! ;m
    overrides construct

    m: 
    1 counter +!
    counter @ count-max @ >= if
        state @ invert state !
        0 counter !
    endif
    this ;m
    overrides activate
    
end-class NthToggle

: flag. 
    if ." true" else ." false" endif cr ;

: mainloop 
    true swap heap-new true NUM 0 ?do
    drop dup activate value
    loop
    flag. drop ;

: main 
    Toggle mainloop
    3 NthToggle mainloop ;

main bye
methcall.gnat
-- $Id: methcall.gnat,v 1.0 2003/06/11 12:10:00 dada Exp $
-- http://dada.perl.it/shootout/
-- Ada 95 code by C.C.

with Text_IO, Ada.Command_Line, Ada.Characters.Handling;

procedure MethCall is
   package Toggles is
      type A_Rec is                          --  'tagged' implies hidden field,
         tagged record                       --   and can append new fields
            Value       : Boolean := True;
         end record;

      procedure Activate (A : in out A_Rec);

      type B_Rec is new A_Rec with                 --  Add more fields to A_Rec
         record
            Flip_Period : Positive := 1;
            Count       : Natural := 0;
         end record;

      procedure Activate (B : in out B_Rec);
      pragma Inline (Activate);
   end Toggles;

   package body Toggles is
      procedure Activate (A : in out A_Rec) is
      begin
         A.Value := not A.Value;
      end Activate;

      procedure Activate (B : in out B_Rec) is
      begin
         B.Count := B.Count + 1;
         if B.Count = B.Flip_Period then
            B.Count := 0;
            B.Value := not B.Value;
         end if;
      end Activate;
   end Toggles;

   X        : Toggles.A_Rec := (Value => True);
   Y        : Toggles.B_Rec := (Value => True, Count => 0, Flip_Period => 3);
   Val      : Boolean;
   N        : Positive := 1;
begin
   begin
      N := Positive'Value (Ada.Command_Line.Argument (1));
   exception
      when Constraint_Error => null;
   end;
   for Iter in 1 .. N loop
      Toggles.Activate (X);
      Val := X.Value;
   end loop;
   Text_IO.Put_Line (Ada.Characters.Handling.To_Lower (Boolean'Image (Val)));
   for Iter in 1 .. N loop
      Toggles.Activate (Y);
      Val := Y.Value;
   end loop;
   Text_IO.Put_Line (Ada.Characters.Handling.To_Lower (Boolean'Image (Val)));
end MethCall;
methcall.guile
#!/usr/local/bin/guile-oops \
-e main -s
!#

;;; $Id: methcall.guile,v 1.4 2001/05/27 17:33:15 doug Exp $
;;; http://www.bagley.org/~doug/shootout/
;;; from: Benedikt Rosenau

(use-modules (oop goops)) 

(define-method (print-bool (b <;boolean>))
   (display (if b "true\n" "false\n")))


(define-class <;toggle> ()
  (state #:getter value? #:init-keyword #:state))

(define-class <;nth-toggle> (<toggle>)
  (count-max #:init-keyword #:count-max)
  (counter #:init-value 0))


(define-method (activate! (t <;toggle>))
  (slot-set! t 'state (not (slot-ref t 'state)))
   t)

(define-method (activate! (n-t <;nth-toggle>))
  (let ((counter (+ 1 (slot-ref n-t 'counter))))
    (slot-set! n-t 'counter counter)
    (if (>;= counter (slot-ref n-t 'count-max))
      (begin (slot-set! n-t 'state (not (slot-ref n-t 'state)))
             (slot-set! n-t 'counter 0)))
     n-t))


(define-method (main (l <;list>))
  (let ((n (catch #t (lambda () (string->;number (cadr l)))
                     (lambda ex 1))))
    (let ((tog (make <;toggle> #:state #t)))
      (do ((i 1 (+ i 1))) ((= i n))
          (value? (activate! tog)))
      (print-bool (value? (activate! tog))))
    (let ((ntog (make <;nth-toggle> #:state #t #:count-max 3)))
      (do ((i 1 (+ i 1))) ((= i n))
          (value? (activate! ntog)))
      (print-bool (value? (activate! ntog))))))
methcall.ici
// $Id: methcall.ici,v 1.0 2003/01/03 12:02: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 < n; ++i)
    val = toggle:activate():value();
printf(val ? "true\n" : "false\n");

ntoggle := NthToggle:new(val, 3);
for (i = 0; i < n; ++i)
    val = ntoggle:activate():value();
printf(val ? "true\n" : "false\n");
methcall.java
// $Id: methcall.java,v 1.3 2000/12/24 22:04:51 doug Exp $
// http://www.bagley.org/~doug/shootout/
// Collection class code is from my friend Phil Chu, Thanks Phil!

import java.io.*;
import java.util.*;
import java.text.*;

class Toggle {
    boolean state = true;
    public Toggle(boolean start_state) {
    this.state = start_state;
    }
    public boolean value() {
    return(this.state);
    }
    public Toggle activate() {
    this.state = !this.state;
    return(this);
    }
}

class NthToggle extends Toggle {
    int count_max = 0;
    int counter = 0;

    public NthToggle(boolean start_state, int max_counter) {
    super(start_state);
    this.count_max = max_counter;
    this.counter = 0;
    }
    public Toggle activate() {
    this.counter += 1;
    if (this.counter >= this.count_max) {
        this.state = !this.state;
        this.counter = 0;
    }
    return(this);
    }
}

public class methcall {
    public static void main(String args[]) throws IOException {
    int n = Integer.parseInt(args[0]);

    boolean val = true;
    Toggle toggle = new Toggle(val);
    for (int i=0; i<n; i++) {
        val = toggle.activate().value();
    }
    System.out.println((val) ? "true" : "false");

    val = true;
    NthToggle ntoggle = new NthToggle(true, 3);
    for (int i=0; i<n; i++) {
        val = ntoggle.activate().value();
    }
    System.out.println((val) ? "true" : "false");
    }
}
methcall.jscript
// -*- mode: java -*-
// $Id: methcall.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// From David Hedbor
// modified by Aldo Calpini <dada@perl.it> for Win32

function Toggle(start_state) {
  this.bool = start_state;
  
  this.value = ToggleValue;
  this.activate = ToggleActivate;
  
}
function ToggleValue () {
  return this.bool;
}
function ToggleActivate () {
  this.bool = !this.bool;
  return this;
}


function NthToggle (start_state, max_counter) {
  this.base = Toggle;
  this.base(start_state);
  this.count_max = max_counter;
  this.count = 0;

  this.activate = NthToggleActivate;

}
NthToggle.prototype = new Toggle;

function NthToggleActivate () {
if (++this.count >= this.count_max) {
  this.bool = !this.bool;
  this.count = 0;
}
return this;
}

var n, i;
ARGS = WScript.Arguments;
if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {
  n = 1;
}

var val = true;
var toggle = new Toggle(val);
for (i=0; i<n; i++) {
  val = toggle.activate().value();
}
WScript.Echo(toggle.value() ? "true" : "false");

val = true;
var ntoggle = new NthToggle(val, 3);
for (i=0; i<n; i++) {
  val = ntoggle.activate().value();
}
WScript.Echo(ntoggle.value() ? "true" : "false");

methcall.lcc
/* -*- mode: c -*-
 * $Id: methcall.gcc,v 1.3 2001/06/14 12:58:39 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>

#define true  1
#define false 0


#define TOGGLE \
    char state; \
    char (*value)(struct Toggle *); \
    struct Toggle *(*activate)(struct Toggle *)

#define DESTROY  free

typedef struct Toggle {
    TOGGLE;
} Toggle;

char toggle_value(Toggle *this) {
    return(this->state);
}
Toggle *toggle_activate(Toggle *this) {
    this->state = !this->state;
    return(this);
}
Toggle *init_Toggle(Toggle *this, char start_state) {
    this->state = start_state;
    this->value = toggle_value;
    this->activate = toggle_activate;
    return(this);
}
Toggle *new_Toggle(char start_state) {
    Toggle *this = (Toggle *)malloc(sizeof(Toggle));
    return(init_Toggle(this, start_state));
}


typedef struct NthToggle {
    TOGGLE;
    int count_max;
    int counter;
} NthToggle;

NthToggle *nth_toggle_activate(NthToggle *this) {
    if (++this->counter >= this->count_max) {
    this->state = !this->state;
    this->counter = 0;
    }
    return(this);
}
NthToggle *init_NthToggle(NthToggle *this, int max_count) {
    this->count_max = max_count;
    this->counter = 0;
    this->activate = (Toggle *(*)(Toggle *))nth_toggle_activate;
    return(this);
}
NthToggle *new_NthToggle(char start_state, int max_count) {
    NthToggle *this = (NthToggle *)malloc(sizeof(NthToggle));
    this = (NthToggle *)init_Toggle((Toggle *)this, start_state);
    return(init_NthToggle(this, max_count));
}


int main(int argc, char *argv[]) {
    int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
    Toggle *tog;
    NthToggle *ntog;
    char val = true;

    tog = new_Toggle(true);
    for (i=0; i<n; i++) {
    val = tog->activate(tog)->value(tog);
    }
    fputs(val ? "true\n" : "false\n", stdout);
    DESTROY(tog);
    
    val = true;
    ntog = new_NthToggle(val, 3);
    for (i=0; i<n; i++) {
    val = ntog->activate(ntog)->value(ntog);
    }
    fputs(val ? "true\n" : "false\n", stdout);
    DESTROY(ntog);
    return 0;
}
methcall.lua
-- $Id: methcall.lua,v 1.2 2000/12/24 22:04:51 doug Exp $
-- http://www.bagley.org/~doug/shootout/

-- set up a simple object inheritance
settagmethod(tag{}, "index", function (object, field)
    if field == "parent" then return nil end
    if type(object.parent) ~= "table" then return nil end
    return object.parent[field]
end)


--------------------------------------------------------------
-- Toggle module
--------------------------------------------------------------

function new_Toggle(start_state)
    return {
    bool = start_state,
    value = function (self)
        return self.bool
    end,
    activate = function(self)
        self.bool = not self.bool
        return self
    end,
    }
end

--------------------------------------------------------------
-- NthToggle module
--------------------------------------------------------------

function new_NthToggle(start_state, max_counter)
    return {
    parent = new_Toggle(start_state),
    count_max = max_counter,
    counter = 0,
    activate = function (self)
        self.counter = self.counter + 1
        if self.counter >= self.count_max then
        self.parent:activate()
        self.counter = 0
        end
        return self
    end
    }
end

-----------------------------------------------------------
-- main
-----------------------------------------------------------

function main ()
    local N = tonumber((arg and arg[1])) or 1

    local val = 1
    local toggle = new_Toggle(val)
    for i=1,N do
    val = toggle:activate():value()
    end
    if toggle:value() then write("true\n") else write("false\n") end
    
    val = 1
    local ntoggle = new_NthToggle(val, 3)
    for i=1,N do
    val = ntoggle:activate():value()
    end
    if ntoggle:value() then write("true\n") else write("false\n") end
end

main()
methcall.lua5
-- $Id: methcall.lua,v 1.2 2000/12/24 22:04:51 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- contributed by Roberto Ierusalimschy

--------------------------------------------------------------
-- Toggle class
--------------------------------------------------------------

Toggle = {}

function Toggle:value ()
  return self.state
end

function Toggle:activate ()
  self.state = not self.state
  return self
end

function Toggle:new (start_state)
  local o = {state = start_state}
  self.__index =self
  setmetatable(o, self)
  return o
end


--------------------------------------------------------------
-- NthToggle class
--------------------------------------------------------------

NthToggle = Toggle:new()

function NthToggle:activate ()
  self.counter = self.counter + 1
  if self.counter >= self.count_max then
    self.state = not self.state
    self.counter = 0
  end
  return self
end

function NthToggle:new (start_state, max_counter)
  local o = Toggle.new(self, start_state)
  o.count_max = max_counter
  o.counter = 0
  return o
end


-----------------------------------------------------------
-- main
-----------------------------------------------------------

function main ()
  local N = tonumber((arg and arg[1])) or 1

  local val = 1
  local toggle = Toggle:new(val)
  for i=1,N do
    val = toggle:activate():value()
  end
  print(val and "true" or "false")
    
  val = 1
  local ntoggle = NthToggle:new(val, 3)
  for i=1,N do
    val = ntoggle:activate():value()
  end
  print(val and "true" or "false")
end

main()

methcall.mercury
%% $Id: methcall.mercury,v 1.1 2001/07/28 22:15:46 doug Exp $
%% http://www.bagley.org/~doug/shootout/
%% from Fergus Henderson

% "This test uses a base class Toggle, which implements a simple boolean
% flip-flop device and a derived class NthToggle, which only flips every
% Nth time it is activated."

:- module mytest.
:- interface.
:- import_module io.

:- pred main(io__state::di, io__state::uo) is det.

:- implementation.
:- import_module bool, int, string, list.

% "The base Toggle class should define a boolean
% (or integer) field to hold a true/false value. It should define methods
% to access the value, and to activate the toggle (flip it's value)."

:- type toggle ---> toggle(toggle_value::bool).

:- typeclass toggle(T) where [
    func value(T) = bool,
    func 'value :='(T, bool) = T,
    func activate(T) = T
].

:- instance toggle(toggle) where [
    func(value/1) is toggle_value,
    func('value :='/2) is 'toggle_value :=',
    activate(toggle(yes)) = toggle(no),
    activate(toggle(no)) = toggle(yes)
].

% "The derived NthToggle class should inherit the boolean field, and add a
% counter and limit field. It should override the activate method so that
% the boolean state is flipped after the activate method is called count
% times. The constructor for NthToggle should use the constructor for
% Toggle to inherit the boolean field and value() method."

% Mercury doesn't have any direct support for inheritence of fields,
% so we need to use composition instead.

:- type nth_toggle ---> nth_toggle(base::toggle, counter::int, limit::int).

:- func make_nth_toggle(bool, int) = nth_toggle.
make_nth_toggle(Val, Max) = nth_toggle(toggle(Val), 0, Max).

% If the nth_toggle class added its own methods, then we'd want to
% create a derived typeclass like this:
%
% :- typeclass nth_toggle(T) <= toggle(T) where [ ... ].
% :- instance nth_toggle(nth_toggle) where [ ... ].
%
% But nth_toggle doesn't add any new methods, so we don't need that.
% We just need to make it an instance of the base class,
% delegating the field accessors.

:- instance toggle(nth_toggle) where [
    value(T) = T^base^value,
    'value :='(T, V) = T^base^value := V,
    (activate(T) = NewT :-
        C = T^counter + 1,
        (if C >= T^limit then
            NewT = (T^counter := 0)^base := activate(T^base)
        else
            NewT = T^counter := C
        ))
].

main -->
    io__command_line_arguments(Args),
    { N = (if Args = [Arg], to_int(Arg, N0) then N0 else 1) },

    { loop(N, yes, toggle(yes),
        (pred(_V0::in, T0::in, V::out, T::out) is det :-
            T = T0^activate, V = T^value),
        Value1, _Toggle1) },
    write_string(if Value1 = yes then "true" else "false"), nl,

    { loop(N, yes, make_nth_toggle(yes, 3),
        (pred(_V0::in, T0::in, V::out, T::out) is det :-
            T = T0^activate, V = T^value),
        Value2, _Toggle2) },
    write_string(if Value2 = yes then "true" else "false"), nl.

:- pred loop(int, T1, T2, pred(T1, T2, T1, T2), T1, T2).
:- mode loop(in, in, in, pred(in, in, out, out) is det, out, out) is det.
loop(N, V0, T0, P, V, T) :-
    (if N = 0 then
        V = V0, T = T0
    else
        P(V0, T0, V1, T1),
        loop(N - 1, V1, T1, P, V, T)
    ).

% Alternatively, it can be written a little more elegantly as
%
% main -->
%     io__command_line_arguments(Args),
%     { N = (if Args = [Arg], to_int(Arg, N0) then N0 else 1) },
% 
%     { {Value1, _Toggle1} = repeat_n(N, {yes, toggle(yes)},
%         (func({_V0, T0}) = {V, T} :- T = T0^activate, V = T^value)) },
%     write_string(if Value1 = yes then "true" else "false"), nl,
% 
%     { {Value2, _Toggle2} = repeat_n(N, {yes, make_nth_toggle(yes, 3)},
%         (func({_V0, T0}) = {V, T} :- T = T0^activate, V = T^value)) },
%     write_string(if Value2 = yes then "true" else "false"), nl.
% 
% :- func repeat_n(int, T, func(T) = T) = T.
% repeat_n(N, X, F) =
%     (if N = 0 then X
%     else repeat_n(N - 1, F(X), F)).
%
% but the earlier code above is a little more efficient.
methcall.mingw32
/* -*- mode: c -*-
 * $Id: methcall.gcc,v 1.3 2001/06/14 12:58:39 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>

#define true  1
#define false 0


#define TOGGLE \
    char state; \
    char (*value)(struct Toggle *); \
    struct Toggle *(*activate)(struct Toggle *)

#define DESTROY  free

typedef struct Toggle {
    TOGGLE;
} Toggle;

char toggle_value(Toggle *this) {
    return(this->state);
}
Toggle *toggle_activate(Toggle *this) {
    this->state = !this->state;
    return(this);
}
Toggle *init_Toggle(Toggle *this, char start_state) {
    this->state = start_state;
    this->value = toggle_value;
    this->activate = toggle_activate;
    return(this);
}
Toggle *new_Toggle(char start_state) {
    Toggle *this = (Toggle *)malloc(sizeof(Toggle));
    return(init_Toggle(this, start_state));
}


typedef struct NthToggle {
    TOGGLE;
    int count_max;
    int counter;
} NthToggle;

NthToggle *nth_toggle_activate(NthToggle *this) {
    if (++this->counter >= this->count_max) {
    this->state = !this->state;
    this->counter = 0;
    }
    return(this);
}
NthToggle *init_NthToggle(NthToggle *this, int max_count) {
    this->count_max = max_count;
    this->counter = 0;
    this->activate = (Toggle *(*)(Toggle *))nth_toggle_activate;
    return(this);
}
NthToggle *new_NthToggle(char start_state, int max_count) {
    NthToggle *this = (NthToggle *)malloc(sizeof(NthToggle));
    this = (NthToggle *)init_Toggle((Toggle *)this, start_state);
    return(init_NthToggle(this, max_count));
}


int main(int argc, char *argv[]) {
    int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
    Toggle *tog;
    NthToggle *ntog;
    char val = true;

    tog = new_Toggle(true);
    for (i=0; i<n; i++) {
    val = tog->activate(tog)->value(tog);
    }
    fputs(val ? "true\n" : "false\n", stdout);
    DESTROY(tog);
    
    val = true;
    ntog = new_NthToggle(val, 3);
    for (i=0; i<n; i++) {
    val = ntog->activate(ntog)->value(ntog);
    }
    fputs(val ? "true\n" : "false\n", stdout);
    DESTROY(ntog);
    return 0;
}
methcall.nice
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a methcall.jar methcall

To run:
   java -jar methcall.jar 1000000
*/


import ackermann; // reuse toSingleInt


void main(String[] args){
   let n =  toSingleInt(args);
   boolean val;

   Toggle toggle = new Toggle();
   for(var i=1; i= t.toggleTrigger){
      t.state = !t.state;
      t.count = 0;
   }
   return t; 
}



methcall.ocaml
(*
 * $Id: methcall.ocaml,v 1.6 2001/01/08 03:08:35 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * from: Benedikt Rosenau
 * with contributions from Markus Mottl
 *)

let print_bool b = print_endline (string_of_bool b)

class toggle start_state = object (self)
  val mutable state = start_state

  method value = state
  method activate = state <- not state; self
end

class nth_toggle start_state max_counter = object (self)
  inherit toggle start_state

  val count_max = max_counter
  val mutable counter = 0

  method activate =
    counter <- counter + 1;
    if counter >= count_max
    then begin
      state <- not state;
      counter <- 0
    end;
    self
end

let _ =
  let n =
    try int_of_string Sys.argv.(1)
    with Invalid_argument _ -> 1 in
  let tog = new toggle true in
  for i = 2 to n do
    ignore tog#activate#value
  done;
  print_bool tog#activate#value;
  let ntog = new nth_toggle true 3 in
  for i = 2 to n do
    ignore ntog#activate#value
  done;
  print_bool ntog#activate#value
methcall.ocamlb
(*
 * $Id: methcall.ocaml,v 1.6 2001/01/08 03:08:35 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * from: Benedikt Rosenau
 * with contributions from Markus Mottl
 *)

let print_bool b = print_endline (string_of_bool b)

class toggle start_state = object (self)
  val mutable state = start_state

  method value = state
  method activate = state <- not state; self
end

class nth_toggle start_state max_counter = object (self)
  inherit toggle start_state

  val count_max = max_counter
  val mutable counter = 0

  method activate =
    counter <- counter + 1;
    if counter >= count_max
    then begin
      state <- not state;
      counter <- 0
    end;
    self
end

let _ =
  let n =
    try int_of_string Sys.argv.(1)
    with Invalid_argument _ -> 1 in
  let tog = new toggle true in
  for i = 2 to n do
    ignore tog#activate#value
  done;
  print_bool tog#activate#value;
  let ntog = new nth_toggle true 3 in
  for i = 2 to n do
    ignore ntog#activate#value
  done;
  print_bool ntog#activate#value
methcall.oz
%%% $Id: methcall.oz,v 1.0 2002/08/19 16:18:00 dada Exp $
%%% http://dada.perl.it/shootout/
%%% 
%%% contributed by Isaac Gouy

%%  Usage: start from command line with
%%     ozc -x methcall.oz -o methcall.oz.exe
%%     methcall.oz.exe 1000000

functor
import System Application

define

class Toggle
   attr state: true
   meth state(V)
     V = @state
   end
   meth activate
      state <- {Not @state}
   end
   meth init(State)
      state <- State
   end
end

class NthToggle from Toggle
   attr trigger:0 count:0
   meth activate
      count <- @count + 1
      if @count >= @trigger then
     state <- {Not @state}
     count <- 0
      end
   end
   meth init(State Trigger)
      Toggle,init(State)
      trigger <- Trigger
      count <- 0
   end
end

proc {ShowBool X}
   if X then {System.showInfo "true"}
   else {System.showInfo "false"} end
end

fun {MethodSends N T}
   local V in
      if N==0 then {T state(V)} V
      else
     {T activate}{T state(V)}
     {MethodSends N-1 T}
      end
   end
end

in 
   local Args N in
      [Args] = {Application.getArgs plain}
      N = {String.toInt Args}
      {ShowBool {MethodSends N {New Toggle init(true)}}}
      {ShowBool {MethodSends N {New NthToggle init(true 3)}}}   
   end
   {Application.exit 0}
end

methcall.perl
#!/usr/local/bin/perl
# $Id: methcall.perl,v 1.5 2001/05/27 16:44:24 doug Exp $
# http://www.bagley.org/~doug/shootout/
# with help from Ben Tilly

package Toggle;

sub new {
    my($class, $start_state) = @_;
    bless( { Bool => $start_state }, $class );
}

sub value {
    (shift)->{Bool};
}

sub activate {
    my $self = shift;
    $self->{Bool} ^= 1;
    return($self);
}


package NthToggle;
our @ISA = qw(Toggle);

sub new {
    my($class, $start_state, $max_counter) = @_;
    my $self = $class->SUPER::new($start_state);
    $self->{CountMax} = $max_counter;
    $self->{Counter} = 0;
    return($self);
}

sub activate {
    my $self = shift;
    if (++$self->{Counter} >= $self->{CountMax}) {
    $self->{Bool} ^= 1;
    $self->{Counter} = 0;
    }
    return($self);
}


package main;

sub main {
    my $NUM = $ARGV[0];
    $NUM = 1 if ($NUM < 1);

    my $val = 1;
    my $toggle = Toggle->new($val);
    for (1..$NUM) {
    $val = $toggle->activate->value;
    }
    print (($val) ? "true\n" : "false\n");

    $val = 1;
    my $ntoggle = NthToggle->new($val, 3);
    for (1..$NUM) {
    $val = $ntoggle->activate->value;
    }
    print (($val) ? "true\n" : "false\n");
}

main();
methcall.pike
#!/usr/local/bin/pike// -*- mode: pike -*-
// $Id: methcall.pike,v 1.3 2000/12/24 22:04:51 doug Exp $
// http://www.bagley.org/~doug/shootout/

class Toggle {
    int bool;

    object create (int start_state) {
    bool = start_state;
    }

    int value () {
    return(bool);
    }

    object activate () {
    bool = !bool;
    return(this_object());
    }
}

class NthToggle {
    inherit Toggle;

    int count_max, count;

    object create (int start_state, int max_counter) {
    ::create(start_state);
    count_max = max_counter;
    count = 0;
    }

    object activate () {
    if (++count >= count_max) {
        bool = !bool;
        count = 0;
    }
    return(this_object());
    }
}

void main(int argc, array(string) argv) {
    int n = (int)argv[-1];
    if (n < 1) n = 1;
      
    int val = 1;
    object toggle = Toggle(val);
    for (int i=0; i<n; i++) {
    val = toggle->activate()->value();
    }
    write((toggle->value()) ? "true\n" : "false\n");

    val = 1;
    object ntoggle = NthToggle(val, 3);
    for (int i=0; i<n; i++) {
    val = ntoggle->activate()->value();
    }
    write((ntoggle->value()) ? "true\n" : "false\n");
}


methcall.pliant
# $Id: methcall.pliant,v 1.0 2002/02/07 16:39:00 dada Exp $
# http://dada.perl.it/shootout/
# (based on methcall.pliant by pixel@mandrakesoft)

module "/pliant/language/context.pli"
module "/pliant/language/compiler.pli"

meta inherit e
  if e:size<>1
    return
  var Pointer:Type interface :> (e:0 constant Type) map Type
  if addressof:interface=null
    return
  var Pointer:Arrow c :> e:module first "pliant type"
  if c=null or entry_type:c<>Type
    return
  var Pointer:Type implementation :> c map Type
  if interface:nb_fields<>0 and implementation:nb_fields<>0
    return
  for (var Int i) 0 interface:nb_fields-1
    var Pointer:TypeField f :> interface field i
    implementation define_field f:type f:name f:initial_value
  interface maybe implementation
  e set_void_result


type Toggle
  field Bool state

function init t start_state
  arg_w Toggle t ; arg Bool start_state
  t:state := start_state

method t value -> r
  arg Toggle t ; arg Bool r
  return t:state

method t activate -> r
  arg_rw Toggle t ; arg Toggle r
  t:state := not t:state
  return t

type NthToggle
  inherit Toggle
  field Int count_max
  field Int counter

function init t start_state max_counter
  arg_w NthToggle t ; arg Bool start_state ; arg Int max_counter
  init t start_state
  t:count_max := max_counter
  t:counter := 0

method t activate -> r
  arg_rw NthToggle t ; arg NthToggle r
  t:counter += 1
  if t:counter >= t:count_max
    t:state := not t:state;
    t:counter := 0;
  return t


function doit n
  arg Int n
  var Toggle toggle
  var Bool val := true

  for (var Int i) 1 n
    val := toggle:activate:value
  console val eol

  val := true
  var NthToggle ntoggle
  init ntoggle val 3
  
  for (var Int i) 1 n
    val := ntoggle:activate:value
  console val eol


gvar Str s_n := cast ((pliant_script_args translate Address 1) map CStr) Str
if (s_n parse (gvar Int n))
  doit n
else
  console "usage: methcall.pliant <number>" eol
methcall.poplisp
;;; -*- mode: lisp -*-
;;; $Id: methcall.poplisp,v 1.0 2002/05/08 11:15:00 dada Exp $

;; OO with CLOS
(proclaim '(optimize (speed 3)(safety 0)(space 0)(debug 0)(compilation-speed 0)))

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defstruct (toggle (:constructor make-toggle ()))
    (state t :type boolean)))

(defmethod activate ((this toggle))
  (setf (toggle-state this) (not (toggle-state this)))
  this)

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defstruct (nth-toggle (:include toggle)
                         (:constructor make-nth-toggle (count-max)))
    (count-max 1 :type fixnum)
    (counter 0 :type fixnum)))

(defmethod activate ((this nth-toggle))
  (incf (nth-toggle-counter this))
  (cond ((>;= (nth-toggle-counter this)
         (nth-toggle-count-max this))
     (setf (toggle-state this) (not (toggle-state this)))
     (setf (nth-toggle-counter this) 0)))
  this)
  
(defun print-bool (b)
  (format t (if b "true~%" "false~%")))

(let ((n (parse-integer (or (car pop11::poparglist) "1")))
(val))
(declare (fixnum n val))
(let ((tog (make-toggle)))
  (dotimes (i n)
(declare (fixnum i))
(setq val (toggle-state (activate tog))))
  (print-bool (toggle-state tog))
(let ((ntog (make-nth-toggle 3)))
  (dotimes (i n)
(declare (fixnum i))
(setq val (toggle-state (activate ntog))))
  (print-bool (toggle-state ntog)))))
methcall.python
#!/usr/local/bin/python
# http://www.bagley.org/~doug/shootout/

import sys

class Toggle:
    def __init__(self, start_state):
        self.bool = start_state
    def value(self):
        return(self.bool)
    def activate(self):
        self.bool = not self.bool
        return(self)

class NthToggle(Toggle):
    def __init__(self, start_state, max_counter):
        Toggle.__init__(self, start_state)
        self.count_max = max_counter
        self.counter = 0
    def activate(self):
        self.counter += 1
        if (self.counter >= self.count_max):
            self.bool = not self.bool
            self.counter = 0
        return(self)


def main():
    NUM = int(sys.argv[1])
    if NUM < 1:
        NUM = 1

    val = 1
    toggle = Toggle(val)
    for i in xrange(0,NUM):
    val = toggle.activate().value()
    if val:
        print "true"
    else:
        print "false"

    val = 1
    ntoggle = NthToggle(val, 3)
    for i in xrange(0,NUM):
        val = ntoggle.activate().value()
    if val:
        print "true"
    else:
        print "false"

main()
methcall.ruby
#!/usr/local/bin/ruby
# -*- mode: ruby -*-
# $Id: methcall.ruby,v 1.2 2000/12/24 22:04:51 doug Exp $
# http://www.bagley.org/~doug/shootout/

class Toggle
    def initialize(start_state)
    @bool = start_state
    self
    end
    def value()
    @bool
    end
    def activate()
    @bool = !@bool
    self
    end
end

class NthToggle < Toggle
    def initialize(start_state, max_counter)
    super(start_state)
    @count_max = max_counter
    @counter = 0
    self
    end
    def activate()
    @counter += 1
    if (@counter >= @count_max) then
        @bool = !@bool
        @counter = 0
    end
    self
    end
end

def main()
    n = Integer(ARGV.shift || 1)

    val = 1
    toggle = Toggle.new(val)
    n.times do
    val = toggle.activate().value()
    end
    if val then puts "true" else puts "false" end

    val = 1
    ntoggle = NthToggle.new(val, 3)
    n.times do
    val = ntoggle.activate().value()
    end
    if val then puts "true" else puts "false" end
end

main()

methcall.se
-- -*- mode: eiffel -*-
-- $Id: methcall.se,v 1.2 2001/05/23 18:29:16 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Steve Thompson

-- <LOC-OFF>
indexing
   description: "This class is the entry point for the method call test" 
   author : Steve Thompson
   email  : "Steve_Thompson@prodigy.net"
   date   : February 18, 2001
   compile: "compile -clean -boost -no_split -O3 main.e -o main"
   run    : "main 400000"
-- <LOC-ON>
   
class METHCALL
   
creation make
   
feature -- Creation
   
   make is
      local
     index: INTEGER
     n: INTEGER
     toggle: TOGGLE
     value: BOOLEAN
     nth_toggle: NTH_TOGGLE
      do
     if argument_count = 0 then 
        n := 1
     else
        n := argument(1).to_integer
     end
     
     value := True
     !!toggle.make(value)
     from index := 0 until index = n loop
        value := toggle.activate.value
        index := index + 1
     end

     if value then print("true%N") else print("false%N") end
        value := True
        !!nth_toggle.make(value, 3)
     from index := 0 until index = n loop
        value := nth_toggle.activate.value
        index := index + 1
     end
     if value then print("true%N") else print("false%N") end
        
      end -- make
   
end
methcall.smlnj
(* -*- mode: sml -*-
 * $Id: methcall.smlnj,v 1.2 2001/07/09 00:25:28 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * from Stephen Weeks
 *)


structure Test : sig
    val main : (string * string list) -> OS.Process.status
end = struct

fun for (start, stop, f) =
   let
      fun loop i =
     if i > stop
        then ()
     else (f i; loop (i + 1))
   in
      loop start
   end

structure Toggle =
   struct
      datatype 'a t = T of {
                state: 'a ref,
                value: 'a t -> 'a,
                activate: 'a t -> 'a t
                }

      fun new state =
     T {state = ref state,
        value = fn T {state, ...} => !state,
        activate = fn this as T {state, ...} => (state := not(!state); this)}

      fun activate (this as T {activate, ...}) = activate this
      fun value (this as T {value, ...}) = value this
   end

structure Ntoggle =
   struct
      datatype 'a t = T of {
                state: 'a ref,
                value: 'a t -> 'a,
                activate: 'a t -> 'a t,
                countMax: int,
                counter: int ref
                }

      fun new (state, countMax) =
     T {
        state = ref state,
        value = fn T {state, ...} => !state,
        activate = (fn this as T {state, counter, countMax, ...} =>
            let
               val newCounter = 1 + !counter
               val _ = counter := newCounter
               val _ = 
                  if !counter >= countMax
                 then (state := not(!state);
                       counter := 0)
                  else ()
            in
               this
            end),
        countMax = countMax,
        counter = ref 0
        }

      fun activate (this as T {activate, ...}) = activate this
      fun value (this as T {value, ...}) = value this
   end

fun atoi s = case Int.fromString s of SOME num => num | NONE => 0
fun printl [] = print "\n" | printl(h::t) = ( print h ; printl t )
   
fun main (name, args) =
   let
      val n = atoi (hd (args @ ["1"]))
      val v = ref true
      val tog = Toggle.new true
      val _ = for (0, n - 1, fn _ => v := Toggle.value (Toggle.activate tog))
      val _ = print (if !v then "true\n" else "false\n")
      val _ = v := true
      val ntog = Ntoggle.new (!v, 3)
      val _ = for (0, n - 1, fn _ => v := Ntoggle.value (Ntoggle.activate ntog))
      val _ = print (if !v then "true\n" else "false\n")
   in
      OS.Process.success
   end
end

val _ = SMLofNJ.exportFn("methcall", Test.main);
methcall.vbscript
Class Toggle

    Public Bool

    Public Property Get value() 
        value = Bool
    End Property 

    Public Property Let value(v) 
        Bool = v
    End Property 

    Public Sub activate()
        If Bool Then
            Bool = False
        Else
            Bool = True
        End If
    End Sub

End Class 

Class NthToggle

    Public Bool
    Private Counter
    Public CountMax
    
    Public Property Get value() 
        value = Bool
    End Property 

    Public Property Let value(v) 
        Bool = v
    End Property 
    
    Public Sub activate()
        Counter = Counter + 1
        If Counter >= CountMax Then
            If Bool Then
                Bool = False
            Else
                Bool = True
            End If
            Counter = 0
        End If
    End Sub

End Class

NUM = WScript.Arguments(0)
If NUM < 1 Then NUM = 1
val = 1
Set oToggle = New Toggle
oToggle.Bool = val
For I = 1 To NUM
    oToggle.Activate
    val = oToggle.Value
Next
If val Then
    WScript.Echo "true"
Else
    WScript.Echo "false"
End If

val = 1
Set onToggle = New NthToggle
onToggle.Bool = val
onToggle.CountMax = 3
For I = 1 To NUM
    onToggle.Activate
    val = onToggle.Value
Next
If val Then
    WScript.Echo "true"
Else
    WScript.Echo "false"
End If

methcall.vc
/* -*- mode: c -*-
 * $Id: methcall.gcc,v 1.3 2001/06/14 12:58:39 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>

#define true  1
#define false 0


#define TOGGLE \
    char state; \
    char (*value)(struct Toggle *); \
    struct Toggle *(*activate)(struct Toggle *)

#define DESTROY  free

typedef struct Toggle {
    TOGGLE;
} Toggle;

char toggle_value(Toggle *this) {
    return(this->state);
}
Toggle *toggle_activate(Toggle *this) {
    this->state = !this->state;
    return(this);
}
Toggle *init_Toggle(Toggle *this, char start_state) {
    this->state = start_state;
    this->value = toggle_value;
    this->activate = toggle_activate;
    return(this);
}
Toggle *new_Toggle(char start_state) {
    Toggle *this = (Toggle *)malloc(sizeof(Toggle));
    return(init_Toggle(this, start_state));
}


typedef struct NthToggle {
    TOGGLE;
    int count_max;
    int counter;
} NthToggle;

NthToggle *nth_toggle_activate(NthToggle *this) {
    if (++this->counter >= this->count_max) {
    this->state = !this->state;
    this->counter = 0;
    }
    return(this);
}
NthToggle *init_NthToggle(NthToggle *this, int max_count) {
    this->count_max = max_count;
    this->counter = 0;
    this->activate = (Toggle *(*)(Toggle *))nth_toggle_activate;
    return(this);
}
NthToggle *new_NthToggle(char start_state, int max_count) {
    NthToggle *this = (NthToggle *)malloc(sizeof(NthToggle));
    this = (NthToggle *)init_Toggle((Toggle *)this, start_state);
    return(init_NthToggle(this, max_count));
}


int main(int argc, char *argv[]) {
    int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
    Toggle *tog;
    NthToggle *ntog;
    char val = true;

    tog = new_Toggle(true);
    for (i=0; i<n; i++) {
    val = tog->activate(tog)->value(tog);
    }
    fputs(val ? "true\n" : "false\n", stdout);
    DESTROY(tog);
    
    val = true;
    ntog = new_NthToggle(val, 3);
    for (i=0; i<n; i++) {
    val = ntog->activate(ntog)->value(ntog);
    }
    fputs(val ? "true\n" : "false\n", stdout);
    DESTROY(ntog);
    return 0;
}
methcall.vc++
// -*- mode: c++ -*-
// $Id: methcall.g++,v 1.7 2001/06/28 14:25:35 doug Exp $
// http://www.bagley.org/~doug/shootout/

// with some help from Bill Lear

// [dada] 2001-09-19 had to add 'int i' at the beggining of main for VC++

#include <stdlib.h>
#include <iostream>

using namespace std;

class Toggle {
public:
    Toggle(bool start_state) : state(start_state) { }
    virtual bool value() {
    return(state);
    }
    Toggle& activate() {
    state = !state;
    return(*this);
    }
    bool state;
};

class NthToggle : public Toggle {
public:
    NthToggle(bool start_state, int max_counter) :
    Toggle(start_state), count_max(max_counter), counter(0) {
    }
    NthToggle& activate() {
    if (++this->counter >= this->count_max) {
        state = !state;
        counter = 0;
    }
    return(*this);
    }
private:
    int count_max;
    int counter;
};

int
main(int argc, char *argv[]) {
    int i;
    int n = ((argc == 2) ? atoi(argv[1]) : 1);

    bool val = true;
    Toggle *toggle = new Toggle(val);
    for (i=0; i<n; i++) {
    val = toggle->activate().value();
    }
    cout << ((val) ? "true" : "false") << endl;
    delete toggle;

    val = true;
    NthToggle *ntoggle = new NthToggle(val, 3);
    for (i=0; i<n; i++) {
    val = ntoggle->activate().value();
    }
    cout << ((val) ? "true" : "false") << endl;
    delete ntoggle;

    return 0;
}
methcall.vpascal
program methcall;

uses SysUtils;

type TToggle = class 
    private
        value : boolean;

    public
        property Bool : boolean read value write value;
        procedure Activate;
end;    

type TNthToggle = class 
    constructor Create;
    private
        value : boolean;
        counter : integer;
        cmax : integer;
    public
        property CountMax : integer read cmax write cmax;
        property Bool : boolean read value write value;
        procedure Activate;
end;

constructor TNthToggle.Create;
begin
    counter := 0;
end;

procedure TToggle.Activate;
begin
    if value = True Then
        value := False
    else
        value := True;
end;

procedure TNthToggle.Activate;
begin
    counter := counter + 1;
    if counter >= cmax Then begin
        if value = True Then
            value := False
        Else
            value := True;
        counter := 0;
    end;
end;


var 
    NUM, i : integer;
    val : boolean;
    oToggle : TToggle;
    onToggle : TNthToggle;
begin
    if ParamCount = 0 then
        NUM := 1
    else
        NUM := StrToInt(ParamStr(1));
        
    if NUM < 1 then NUM := 1;

    val := True;
    oToggle := TToggle.Create;    
    oToggle.Bool := val;
    For i := 1 to NUM do
    begin
        oToggle.Activate;
        val := oToggle.Bool;
    end;
    If val = True Then
        WriteLn('true')
    else
        WriteLn('false');

    val := True;
    onToggle := TNthToggle.Create;
    onToggle.Bool := val;
    onToggle.CountMax := 3;
    For i := 1 to NUM do
    begin
        onToggle.Activate;
        val := onToggle.Bool;
    end;
    If val = True Then
        WriteLn('true')
    else
        WriteLn('false');
end.