Object Instantiation Back to the Win32 Shootout
Back to dada's perl lab

[The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform Details]   [Acknowledgements]   [Scorecard]  
All Source For Object Instantiation
objinst.bcc
/* -*- mode: c -*-
 * $Id: objinst.gcc,v 1.3 2001/07/04 03:36:08 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

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


enum {false, true};

#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;

    tog = new_Toggle(true);
    for (i=0; i<5; i++) {
    fputs((tog->activate(tog)->value(tog)) ? "true\n" : "false\n", stdout);
    }
    DESTROY(tog);
    for (i=0; i<n; i++) {
    tog = new_Toggle(true);
    DESTROY(tog);
    }
    
    fputs("\n", stdout);

    ntog = new_NthToggle(true, 3);
    for (i=0; i<8; i++) {
    fputs((ntog->activate(ntog)->value(ntog)) ? "true\n" : "false\n", stdout);
    }
    DESTROY(ntog);
    for (i=0; i<n; i++) {
    ntog = new_NthToggle(true, 3);
    DESTROY(ntog);
    }
    return 0;
}
objinst.bigforth
\ -*- mode: forth -*-
\ $Id: objinst.bigforth,v 1.3 2001/06/25 20:54:09 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 swap 0 ?do
    drop dup toggle with activate value endwith dup flag.
    loop
    drop ;

: main 
    true Toggle new 5 mainloop
    NUM 0 ?do
    true Toggle new toggle with dispose endwith \ like the C version
    loop
    cr
    3 true NthToggle new 8 mainloop
    NUM 0 ?do
    3 true NthToggle new toggle with dispose endwith \ like the C version
    loop ;

main bye
objinst.csharp
// $Id: objinst.csharp,v 1.0 2002/02/14 13:27: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) {
        int n;

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

        Toggle toggle1 = new Toggle(true);
        for (int i=0; i<5; i++) {
            Console.WriteLine((toggle1.activate().value()) ? "true" : "false");
        }

        for (int i=0; i<n; i++) {
            Toggle toggle = new Toggle(true);
        }
        Console.WriteLine();
        
        
        NthToggle ntoggle1 = new NthToggle(true, 3);
        for (int i=0; i<8; i++) {
            Console.WriteLine((ntoggle1.activate().value()) ? "true" : "false");
        }
        for (int i=0; i<n; i++) {
            NthToggle toggle = new NthToggle(true, 3);
        }
        return(0);
    }
}

objinst.cygperl
#!/usr/local/bin/perl 
# $Id: objinst.perl,v 1.6 2001/06/29 23:12:37 doug Exp $
# http://www.bagley.org/~doug/shootout/

use strict;


package Toggle;

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

sub value {
    my $self = shift;
    return($self->{Bool});
}

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


package NthToggle;
@NthToggle::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] > 0) ? $ARGV[0] : 1;

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

    print "\n";

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

main();

objinst.delphi
program objInst;



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;
    o: TToggle;
    o2: TNthToggle;
begin
  NUM :=1;
  if (ParamCount=1) then Val(ParamStr(1),NUM,code);

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

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

objinst.gcc
/* -*- mode: c -*-
 * $Id: objinst.gcc,v 1.3 2001/07/04 03:36:08 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

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


enum {false, true};

#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;

    tog = new_Toggle(true);
    for (i=0; i<5; i++) {
    fputs((tog->activate(tog)->value(tog)) ? "true\n" : "false\n", stdout);
    }
    DESTROY(tog);
    for (i=0; i<n; i++) {
    tog = new_Toggle(true);
    DESTROY(tog);
    }
    
    fputs("\n", stdout);

    ntog = new_NthToggle(true, 3);
    for (i=0; i<8; i++) {
    fputs((ntog->activate(ntog)->value(ntog)) ? "true\n" : "false\n", stdout);
    }
    DESTROY(ntog);
    for (i=0; i<n; i++) {
    ntog = new_NthToggle(true, 3);
    DESTROY(ntog);
    }
    return 0;
}
objinst.gforth
\ -*- mode: forth -*-
\ $Id: objinst.gforth,v 1.2 2001/06/25 00:22:55 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  { class n }
    true class heap-new true n 0 ?do
    drop dup activate value dup flag.
    loop
    drop ;

: main 
    Toggle 5 mainloop
    NUM 0 ?do
    true Toggle heap-new free drop \ like the C version
    loop
    cr
    3 NthToggle 8 mainloop
    NUM 0 ?do
    3 true NthToggle heap-new free drop \ like the C version
    loop ;

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

-- Annotated Ada Reference Manual ISO/IEC 8652:1995: http://www.ada-auth.org/

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

procedure ObjInst is    --  3.451
   pragma Suppress (Discriminant_Check);
   pragma Suppress (Access_Check);
   package CH renames Ada.Characters.Handling;
   use type Ada.Tags.Tag;

   package Toggles is
      type A_Rec is
         tagged record                 --  'tagged' allows fields to be added
            Value       : Boolean := True;
         end record;

      type B_Rec is new A_Rec with
         record
            Flip_Period : Positive := 1;
            Count       : Natural := 0;
         end record;

      type A_Recs_Family_Ptr is access all A_Rec'Class;

      function Activate (X : A_Recs_Family_Ptr) return A_Recs_Family_Ptr;
      pragma Inline (Activate);
   end Toggles;

   package body Toggles is
      function Activate (X : A_Recs_Family_Ptr) return A_Recs_Family_Ptr is
         pragma Suppress (Range_Check);
         pragma Suppress (Tag_Check);
      begin                   --  X is a ptr: no "in out" parms in a function
         if X.all'Tag = A_Rec'Tag then                         --  A_Rec case
            X.all.Value := not X.all.Value;
         else
            declare                       --  Make visible the 2 B_Rec fields
               pragma Suppress (Tag_Check);
               B        : B_Rec renames B_Rec (X.all);
            begin
               B.Count := B.Count + 1;
               if B.Count = B.Flip_Period then
                  B.Count := 0;
                  B.Value := not B.Value;
               end if;
            end;
         end if;
         return X;
      end Activate;
   end Toggles;

   A        : aliased Toggles.A_Rec;      --  "aliased", since X'Access is used
   B        : aliased Toggles.B_Rec;      --  "access all" above for aliased
   N        : Positive := 1;
   Res      : Boolean;
begin
   begin
      N := Positive'Value (Ada.Command_Line.Argument (1));
   exception
      when Constraint_Error => null;
   end;
   A := Toggles.A_Rec'(Value => True);
   for K in 1 .. 5 loop
      Res := Toggles.Activate (A'Access).Value;
      Text_IO.Put_Line (CH.To_Lower (Boolean'Image (Res)));
   end loop;
   for Iter in 1 .. N loop
      A := Toggles.A_Rec'(Value => True);
   end loop;
   Text_IO.New_Line;
   B := Toggles.B_Rec'(Value => True, Flip_Period => 3, Count => 0);
   for K in 1 .. 8 loop
      Res := Toggles.Activate (B'Access).Value;
      Text_IO.Put_Line (CH.To_Lower (Boolean'Image (Res)));
   end loop;
   for Iter in 1 .. N loop
      B := Toggles.B_Rec'(Value => True, Flip_Period => 3, Count => 0);
   end loop;
end ObjInst;
objinst.guile
#!/usr/local/bin/guile-oops \
-e main -s
!#

(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 (value? (t <;toggle>)) (slot-ref t 'state))

(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 (args <;list>))
  (let ((n (or (and (= (length args) 2) (string->;number (cadr args))) 1)))
    (let ((tog (make <;toggle> #:state #t)))
      (do ((i 0 (+ i 1))) ((= i 5))
    (print-bool (value? (activate! tog)))))
    (do ((i 0 (+ i 1))) ((= i n))
      (make <;toggle> #:state #t))
    (newline)
    (let ((ntog (make <;nth-toggle> #:state #t #:count-max 3)))
      (do ((i 0 (+ i 1))) ((= i 8))
    (print-bool (value? (activate! ntog)))))
    (do ((i 0 (+ i 1))) ((= i n))
      (make <;nth-toggle> #:state #t #:count-max 3))))
objinst.ici
// $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);
objinst.java
// $Id: objinst.java,v 1.2 2000/12/24 22:04:57 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 objinst {
    public static void main(String args[]) throws IOException {
    int n = Integer.parseInt(args[0]);
    Toggle toggle1 = new Toggle(true);
    for (int i=0; i<5; i++) {
        System.out.println((toggle1.activate().value()) ? "true" : "false");
    }
    for (int i=0; i<n; i++) {
        Toggle toggle = new Toggle(true);
    }

    System.out.println("");
    
    NthToggle ntoggle1 = new NthToggle(true, 3);
    for (int i=0; i<8; i++) {
        System.out.println((ntoggle1.activate().value()) ? "true" : "false");
    }
    for (int i=0; i<n; i++) {
        NthToggle toggle = new NthToggle(true, 3);
    }
    }
}
objinst.lcc
/* -*- mode: c -*-
 * $Id: objinst.gcc,v 1.3 2001/07/04 03:36:08 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

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


enum {false, true};

#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;

    tog = new_Toggle(true);
    for (i=0; i<5; i++) {
    fputs((tog->activate(tog)->value(tog)) ? "true\n" : "false\n", stdout);
    }
    DESTROY(tog);
    for (i=0; i<n; i++) {
    tog = new_Toggle(true);
    DESTROY(tog);
    }
    
    fputs("\n", stdout);

    ntog = new_NthToggle(true, 3);
    for (i=0; i<8; i++) {
    fputs((ntog->activate(ntog)->value(ntog)) ? "true\n" : "false\n", stdout);
    }
    DESTROY(ntog);
    for (i=0; i<n; i++) {
    ntog = new_NthToggle(true, 3);
    DESTROY(ntog);
    }
    return 0;
}
objinst.lua
-- $Id: objinst.lua,v 1.3 2001/07/11 17:18:08 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- with help from Roberto Ierusalimschy

-- set up a simple object inheritance
settagmethod(tag{}, "index", function (object, field)
    if field ~= "parent" then
        local p = object.parent
        return p and p[field]
    end
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 toggle = new_Toggle(1)
    for i=1,5 do
    toggle:activate()
    if toggle:value() then write("true\n") else write("false\n") end
    end
    for i=1,N do
    toggle = new_Toggle(1)
    end

    write("\n")

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

main()
objinst.lua5
-- $Id: objinst.lua,v 1.3 2001/07/11 17:18:08 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 toggle = Toggle:new(1)
    for i=1,5 do
      toggle:activate()
      print(toggle:value() and "true" or "false")
    end
    for i=1,N do
      toggle = Toggle:new(1)
    end

    print("")

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

main()

objinst.mercury
%% $Id: objinst.mercury,v 1.1 2001/07/29 00:07:28 doug Exp $
%% http://www.bagley.org/~doug/shootout/
%% from Fergus Henderson

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

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

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

:- 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)
].

:- 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).

:- 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) },
    { Toggle1 = toggle(yes) },
    loop(5, (pred(T0::in, T::out, di, uo) is det -->
            { T = T0^activate },
            write_string(if T^value = yes then "true" else "false"),
            nl),
        Toggle1, Toggle2),
    loop(N, (pred(_T0::in, T::out, di, uo) is det -->
            { T = toggle(yes) }),
        Toggle2, _Toggle3),
    nl,

    { Toggle4 = make_nth_toggle(yes, 3) },
    loop(8, (pred(T0::in, T::out, di, uo) is det -->
            { T = T0^activate },
            write_string(if T^value = yes then "true" else "false"),
            nl),
        Toggle4, Toggle5),
    loop(N, (pred(_T0::in, T::out, di, uo) is det -->
            { T = make_nth_toggle(yes, 3) }),
        Toggle5, _Toggle6).

:- pred loop(int, pred(T1, T1, T2, T2), T1, T1, T2, T2).
:- mode loop(in, pred(in, out, di, uo) is det, in, out, di, uo) is det.
loop(N, P, X0, X) -->
    (if { N = 0 } then
        { X = X0 }
    else
        P(X0, X1),
        loop(N - 1, P, X1, X)
    ).

objinst.mingw32
/* -*- mode: c -*-
 * $Id: objinst.gcc,v 1.3 2001/07/04 03:36:08 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

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


enum {false, true};

#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;

    tog = new_Toggle(true);
    for (i=0; i<5; i++) {
    fputs((tog->activate(tog)->value(tog)) ? "true\n" : "false\n", stdout);
    }
    DESTROY(tog);
    for (i=0; i<n; i++) {
    tog = new_Toggle(true);
    DESTROY(tog);
    }
    
    fputs("\n", stdout);

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

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

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


import methcall; // reuse Toggle & NToggle & toSingleInt


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

   Toggle toggle = new Toggle();
   for(var i=0; i<5; i++) println( toggle.activate().value() );
   for(var i=0; i
objinst.ocaml
(*
 * $Id: objinst.ocaml,v 1.8 2001/07/28 21:52:59 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * from: Benedikt Rosenau
 * with contributions from Markus Mottl
 *)

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 n = if Array.length Sys.argv > 1 then int_of_string Sys.argv.(1) else 1
let tog = new toggle true;;
for i = 1 to 5 do Printf.printf "%b\n" tog#activate#value done;
for i = 1 to n do ignore (new toggle true) done;
print_newline ();
let ntog = new nth_toggle true 3 in
for i = 1 to 8 do Printf.printf "%b\n" ntog#activate#value done;
for i = 1 to n do ignore (new nth_toggle true 3) done
objinst.ocamlb
(*
 * $Id: objinst.ocaml,v 1.8 2001/07/28 21:52:59 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * from: Benedikt Rosenau
 * with contributions from Markus Mottl
 *)

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 n = if Array.length Sys.argv > 1 then int_of_string Sys.argv.(1) else 1
let tog = new toggle true;;
for i = 1 to 5 do Printf.printf "%b\n" tog#activate#value done;
for i = 1 to n do ignore (new toggle true) done;
print_newline ();
let ntog = new nth_toggle true 3 in
for i = 1 to 8 do Printf.printf "%b\n" ntog#activate#value done;
for i = 1 to n do ignore (new nth_toggle true 3) done
objinst.oz
%%% $Id: objinst.oz,v 1.0 2002/08/19 16:16:00 dada Exp $
%%% http://dada.perl.it/shootout/
%%% 
%%% contributed by Isaac Gouy

%% Uses local variables within the Object Instantiation
%% loops. It might be quicker to create a Cell outside 
%% of the loop and use {Assign T {New Toggle init(true)}}
%%
%%  Usage: start from command line with
%%     ozc -x objinst.oz -o objinst.oz.exe
%%     objinst.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

in 
   local Args N T1 T2 in
      [Args] = {Application.getArgs plain}
      N = {String.toInt Args}

      T1 = {New Toggle init(true)}
      for I in 1..5 do
         local V in {T1 activate}{T1 state(V)}{ShowBool V} end
      end
      {System.showInfo ""}
      for I in 1..N do
         local T in T = {New Toggle init(true)} end
      end
   
      T2 = {New NthToggle init(true 3)}
      for I in 1..8 do
         local V in {T2 activate}{T2 state(V)}{ShowBool V} end
      end
      {System.showInfo ""}
      for I in 1..N do
         local T in T = {New NthToggle init(true 3)} end
      end
   end
   {Application.exit 0}
end

objinst.perl
#!/usr/local/bin/perl 
# $Id: objinst.perl,v 1.6 2001/06/29 23:12:37 doug Exp $
# http://www.bagley.org/~doug/shootout/

use strict;


package Toggle;

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

sub value {
    my $self = shift;
    return($self->{Bool});
}

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


package NthToggle;
@NthToggle::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] > 0) ? $ARGV[0] : 1;

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

    print "\n";

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

main();

objinst.pike
#!/usr/local/bin/pike// -*- mode: pike -*-
// $Id: objinst.pike,v 1.3 2000/12/24 22:04:57 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;
      
    object toggle = Toggle(1);
    for (int i=0; i<5; i++) {
    toggle->activate();
    write((toggle->value()) ? "true\n" : "false\n");
    }
    for (int i=0; i<n; i++) {
    object toggle = Toggle(1);
    }

    write("\n");

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


objinst.pliant
# $Id: objinst.pliant,v 1.0 2002/02/07 18:22: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 main n
  arg Int n
  var Toggle toggle
  var Bool val := true
  var NthToggle ntoggle

  init toggle true
  for (var Int i) 1 5
    console toggle:activate:value eol

  for (var Int i) 1 n
    init toggle true
    
  console eol
  
  init ntoggle true 3
  for (var Int i) 1 8
    console ntoggle:activate:value eol
  
  for (var Int i) 1 n
    init ntoggle true 3
    void

gvar Str s_n := cast ((pliant_script_args translate Address 1) map CStr) Str
if (s_n parse (gvar Int n))
  main n
else
  console "usage: objinst.pliant <number>" eol

objinst.poplisp
;;; -*- mode: lisp -*-
;;; $Id: objinst.poplisp,v 1.0 2002/05/08 11:16: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"))))
(declare (fixnum n))
(let ((tog (make-toggle)))
  (dotimes (i 5)
(declare (fixnum i))
(print-bool (toggle-state (activate tog)))))
(dotimes (i n)
  (make-toggle))

(format t "~%")

(let ((ntog (make-nth-toggle 3)))
  (dotimes (i 8)
(declare (fixnum i))
(print-bool (toggle-state (activate ntog)))))
(dotimes (i n)
  (declare (fixnum i))
  (make-nth-toggle 3)))

objinst.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

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

    print ""

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

main()
objinst.ruby
#!/usr/local/bin/ruby
# -*- mode: ruby -*-
# $Id: objinst.ruby,v 1.2 2000/12/24 22:04:57 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)

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

    puts

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

main()

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

-- <LOC-OFF>
indexing
   description: "This class is the entry point for the instantiation performance 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 OBJINST
   
creation make
   
feature -- Creation
   
   make is
      local
     index: INTEGER
     n: INTEGER
     toggle: TOGGLE
     nth_toggle: NTH_TOGGLE
      do
     if argument_count = 0 then 
        n := 1
     else
        n := argument(1).to_integer
     end
     
     !!toggle.make(True)
     from index := 0 until index = 5 loop
        print(toggle.activate.value)
        print("%N")
        index := index + 1
     end
     from index := 0 until index = n loop
        !!toggle.make(True)
        index := index + 1
     end
     
     print("%N")
     
     !!nth_toggle.make(True, 3)
     from index := 0 until index = 8 loop
        print(nth_toggle.activate.value)
        print("%N")
        index := index + 1
     end
     from index := 0 until index = n loop
        !!nth_toggle.make(True, 3)
        index := index + 1
     end
     
      end -- make
   
end
objinst.smlnj
(* -*- mode: sml -*-
 * $Id: objinst.smlnj,v 1.3 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, 4, fn _ =>
           print (if Toggle.value (Toggle.activate tog)
                 then "true\n"
              else "false\n"))

          
      val r = ref (Toggle.new false)
      val _ = for (0, n - 1, fn _ => r := Toggle.new true)
      val _ = Toggle.activate (!r)


      val _ = print "\n"
      val ntog = Ntoggle.new (true, 3)
      val _ = for (0, 7, fn _ =>
           print (if Ntoggle.value (Ntoggle.activate ntog)
                 then "true\n"
              else "false\n"))


      val r2 = ref (Ntoggle.new (true, 3))
      val _ = for (0, n - 1, fn _ => r2 := Ntoggle.new (true, 3))
      val _ = Ntoggle.activate (!r2)

   in
      OS.Process.success
   end
end

val _ = SMLofNJ.exportFn("objinst", Test.main);
objinst.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

Set oToggle = New Toggle
oToggle.Bool = 1
For A = 1 To 5
    oToggle.Activate
    If oToggle.Value Then    
        WScript.Echo "true"
    Else
        WScript.Echo "false"
    End If
Next
For A = 1 To NUM
    Set oToggle = New Toggle
    oToggle.Bool = 1
Next

WScript.Echo

Set onToggle = New NthToggle
onToggle.Bool = 1
onToggle.CountMax = 3
For A = 1 To 8
    onToggle.Activate
    If onToggle.Value Then    
        WScript.Echo "true"
    Else
        WScript.Echo "false"
    End If
Next
For A = 1 To NUM
    Set onToggle = New NthToggle
    onToggle.Bool = 1
    onToggle.CountMax = 3
Next
objinst.vc
/* -*- mode: c -*-
 * $Id: objinst.gcc,v 1.3 2001/07/04 03:36:08 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

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


enum {false, true};

#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;

    tog = new_Toggle(true);
    for (i=0; i<5; i++) {
    fputs((tog->activate(tog)->value(tog)) ? "true\n" : "false\n", stdout);
    }
    DESTROY(tog);
    for (i=0; i<n; i++) {
    tog = new_Toggle(true);
    DESTROY(tog);
    }
    
    fputs("\n", stdout);

    ntog = new_NthToggle(true, 3);
    for (i=0; i<8; i++) {
    fputs((ntog->activate(ntog)->value(ntog)) ? "true\n" : "false\n", stdout);
    }
    DESTROY(ntog);
    for (i=0; i<n; i++) {
    ntog = new_NthToggle(true, 3);
    DESTROY(ntog);
    }
    return 0;
}