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
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
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
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
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 ]
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
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.
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,
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.
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
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
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
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
13 matches
Mail list logo