Re: [Factor-talk] Stack Depth

2012-07-08 Thread Jim Mack
How do you know the optimizing compiler does or doesn't perform one additional read/write when the result goes to 'the' stack instead of going to a different data structure? I couldn't build a compiler, but I think the registers are being used at times, and that the stack is a metaphor. Factor

Re: [Factor-talk] Stack Depth

2012-07-07 Thread graham telfer
Thanks to everybody who gave me suggestions about the stack. I'd like to add a final comment about why I think the stack is useful in this case. Imagine you are going to chop up a carrot to cook it. There are 2 ways you might go about the job: the first is to cut off a piece and then turn

Re: [Factor-talk] Stack Depth

2012-07-06 Thread Adam
Expressed in Factor: : depth ( -- n ) datastack length ; : package-stack ( -- seq ) datastack [ depth narray ] with-datastack ; ...but just the datastack word itself will result in the same sequence ;-) On Tue, Jun 19, 2012 at 8:52 PM, Graham Telfer gakouse...@hotmail.com wrote: Doug

Re: [Factor-talk] Stack Depth

2012-06-19 Thread Graham Telfer
Doug Coleman doug.coleman@... writes: In general, you shouldn't want to do this, as all Factor words (besides the one I'm about to show you) need a fixed number of parameters at compile-time. Macros expand at compile-time, so that's one way to get around the restriction. The other way it

Re: [Factor-talk] Stack Depth

2012-06-19 Thread John Benediktsson
I want to divide an integer by 2 until it gets to 1. I have this snippet of code that goes into a loop. dup 1 [ dup 2 /i ] [ ] if When it gets to 1 I want to push the elements into a sequence. Take a look at the produce word: : divide-until-2 ( n -- seq ) [ dup 1 ] [ 2 /i dup ]

Re: [Factor-talk] Stack Depth

2012-06-19 Thread Marshall Lochbaum
I figured there would be a word designed for that already... Here's my take on this anyway. I would append the items to a sequence as the algorithm goes along: { } swap [ dup 1 ] [ dup 2 /i [ suffix ] dip ] while suffix accomplishes the task. Marshall On Tue, Jun 19, 2012 at 10:50 PM, Graham

Re: [Factor-talk] Stack Depth

2012-06-19 Thread Doug Coleman
The example for the produce word is basically your solution already. \ produce help click the example It should look like this: USING: kernel math prettyprint sequences ; 1337 [ dup 0 ] [ 2/ dup ] produce nip . { 668 334 167 83 41 20 10 5 2 1 0 } This is basically John's solution, too.

Re: [Factor-talk] Stack Depth

2012-06-19 Thread John Benediktsson
If you are concerned about performance (suffix makes a new array each time), then you can use vectors and push onto them, or use the make vocabulary which provides some convenience mechanisms for that. Or look into collector-for which the produce word uses... On Tue, Jun 19, 2012 at 8:09 PM,

Re: [Factor-talk] Stack Depth

2012-06-19 Thread Graham Telfer
Marshall Lochbaum mwlochbaum@... writes: I figured there would be a word designed for that already... Here's my take on this anyway. I would append the items to a sequence as the algorithm goes along: { } swap [ dup 1 ] [ dup 2 /i [ suffix ] dip ] while suffixaccomplishes the task.

Re: [Factor-talk] Stack Depth

2012-06-19 Thread Graham Telfer
John Benediktsson mrjbq7@... writes: I want to divide an integer by 2 until it gets to 1. I have this snippet of code that goes into a loop. dup 1 [ dup 2 /i ] [  ] if When it gets to 1 I want to push the elements into a sequence. Take a look at the produce word: : divide-until-2

Re: [Factor-talk] Stack Depth

2012-06-19 Thread Doug Coleman
How would you do this in C? Would you take the return address off the stack frame, then start calculating the division-by-2s and pushing them to the local stack frame in a loop, and then walk back up accumulating them in an array until you hit the return address, at which point you return the

Re: [Factor-talk] Stack Depth

2012-06-19 Thread Doug Coleman
Actually, here's how you can do this in Factor using the with-datastack word: USING: kernel math prettyprint sequences ; { 1337 } [ [ dup 1 ] [ 2/ dup ] while ] with-datastack It's clean looking, but it's pretty slow. Doug On Tue, Jun 19, 2012 at 8:33 PM, Doug Coleman doug.cole...@gmail.com

Re: [Factor-talk] Stack Depth

2012-06-19 Thread Graham Telfer
Doug Coleman doug.coleman@... writes: How would you do this in C? Would you take the return address off the stack frame, then start calculating the division-by-2s and pushing them to the local stack frame in a loop, and then walk back up accumulating them in an array until you hit the