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> wrote:
> 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 array?
>
> That's kind of what it sounds like you're suggesting, which would be
> kind of fun but totally impractical. In C, you'd collect them
> immediately after you calculate them, too, just like you should in
> Factor.
>
> The data stack is not for storing all your data, just for threading
> arguments and return values between word calls in a predictable way
> (known stack height.)
>
> Is that helpful? :)
>
> Doug
>
> On Tue, Jun 19, 2012 at 8:26 PM, Graham Telfer <gakouse...@hotmail.com> wrote:
>> 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.
>>>
>>> Marshall
>>
>> Hi Marshall,
>>
>> Yes, this works but I feel the two tasks are conceptually different and I 
>> didn't
>> want to interleave them. I wanted to do all the division as 1 job and then 
>> just
>> push the results as the 2nd job. This way the code stays simple and properly
>> factored.
>>
>> Graham
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Live Security Virtual Conference
>> Exclusive live event will cover all the ways today's security and
>> threat landscape has changed and how IT managers can respond. Discussions
>> will include endpoint security, mobile security and the latest in malware
>> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>> _______________________________________________
>> Factor-talk mailing list
>> Factor-talk@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/factor-talk

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to