Hello,

I've been working with alternative versions of these combinators:

     each
     map
     find
     subset

When using the "static stack" style, I noticed some common items in
the stack:

     ( value index seq length )

Another thing in common was words that do these:

        Check if the index is out of bounds

        Increment the index

        Get the ith item in seq and put it at the 'value' location in
        the stack.

So I now have a separate vocabulary called 'iter' which has these
words:

----------------------------------------------------------------------
: iter-done? ( value index seq length -- value index seq length ? )
3 npick over >= ; inline

: iter-increment ( value index seq length -- value index seq length )
>r >r 1+ r> r> ; inline

: iter-get ( value index seq length -- value index seq length )
>r rot drop 2dup >r >r nth-unsafe r> r> r> ; inline
----------------------------------------------------------------------

I then rewrote the combinators in terms of these. In each case I have
a "run" word which is the workhorse word. It's also the
self-documenting sweet-spot word. Here are the run words for the
combinators:

----------------------------------------------------------------------
: each-run ( pred val i seq len -- )
iter-done? [ each-done ] [ iter-get each-call iter-increment each-run ]
if ; inline

: map-run ( newseq q val i seq len -- newseq )
iter-done? [ map-done ] [ iter-get map-call map-set iter-increment map-run ]
if ; inline

: find-run ( quot value i seq len -- i value )
{ { [ iter-done? ]         [ find-not-found ] }
  { [ iter-get find-test ] [ find-found ] }
  { [ t ]                  [ iter-increment find-run ] } } cond ; inline

: subset-run ( acc quot value i seq len -- subseq )
{ { [ iter-done? ]             [ subset-done ] }
  { [ iter-get subset-match? ] [ subset-push iter-increment subset-run ] }
  { [ t ]                      [ iter-increment subset-run ] } } cond ; inline
----------------------------------------------------------------------

All of these words have good performance.

Of course, each combinator has it's own helper words and a "setup"
word which sets up the static stack.

Stack masters recommend keeping the stack short. The reason is because
long stacks can be hard to think about. However, a long but static
stack, is easy to deal with.

There are a few guidelines put forth by the masters of stack
programming. I claim that long but static stacks are an exception
to the guideline of short stacks.

Ed

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to