Re: [chuck-users] chuck-users Digest, Vol 17, Issue 11
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
kijjasak triyanond wrote:
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!
Actually, if it's being used for instructional purposes, I feel like I should post the cleaned up version: class IntStack { class Item { 0 => int value; Item @ below; } Item bottom @=> Item top; fun void push(int value) { Item item; value => item.value; top @=> item.below; item @=> top; } fun int pop() { top.value => int value; if(!isEmpty()) { top.below @=> top; } return value; } fun int isEmpty() { return top == bottom; } } This is one of the syntactic quirks of ChucK -- the following are equivalent: Item foo; new Item @=> Item @ foo; ...which I feel like is an unnecessary ambiguity. The syntax roughly mimics C++ in both cases, but in C++ those would be quite different. -Scott
participants (2)
-
kijjasak triyanond
-
Scott Wheeler