[chuck-users] chuck-users Digest, Vol 17, Issue 11

kijjasak triyanond kijjaz at gmail.com
Mon Dec 18 15:46:42 EST 2006


Oh.. yes yes & yes!
this is the OOP idea i'm not familiar with.
so this is how it works.. wow.
new item.. and new @ variable.. wow.
i was always trying to do something like this with other stuffs.
but i didn't use @ for a new pointer variable..

i had no idea hahah.. thanks Scott. this is so clear.
and it gives me a lot of new ideas & a breakthrough for more things i 
can do in ChucK!

- kij
> Well, or naturally you can write your own stack.  Here's a simple 
> integer stack:
>
> class IntStack
> {
>    class Item
>    {
>        0 => int value;
>        Item @ below;
>    }
>
>    new Item @=> Item @ bottom;
>    bottom @=> Item top;
>
>    fun void push(int value)
>    {
>        new Item @=> Item @ item;
>        value => item.value;
>        top @=> item.below;
>        item @=> top;
>    }
>
>    fun int pop()
>    {
>        top.value => int value;
>
>        if(top != bottom)
>        {
>            top.below @=> top;
>        }
>
>        return value;
>    }
>
>    fun int isEmpty()
>    {
>        return top == bottom;
>    }
> }
>
> Then the following code:
>
> IntStack stack;
>
> stack.push(1);
> stack.push(2);
> stack.push(3);
> stack.push(4);
> stack.push(5);
>
> while(!stack.isEmpty())
> {
>    <<< stack.pop() >>>;
> }
>
> Produces:
>
> 5 :(int)
> 4 :(int)
> 3 :(int)
> 2 :(int)
> 1 :(int)
>
> -Scott


More information about the chuck-users mailing list