I've run into the situation a few times now where I've had "a b c" on  
the stack, and wanted to perform "a c foo" followed by "b c bar"-- 
effectively a cleave followed by a spread. I had been doing this to  
get that effect:

[ [ foo ] curry ]
[ [ bar ] curry ] bi bi*

This could also work:

[ dup swapd ] 2dip 2bi*

but that double nesting and repeated curry is an eyesore, and swapd is  
evil, especially as part of a three-punch shuffling combo. So I threw  
the following words into combinators.lib:

: bi, ( obj quot quot -- quot' quot' )
     [ [ curry ] curry ] bi@ bi ; inline
: tri, ( obj quot quot quot -- quot' quot' quot' )
     [ [ curry ] curry ] tri@ tri ; inline

So you can now say:

[ foo ] [ bar ] bi, bi*

Which reads nicely: "cleave the top value, then spread the next two  
values".

Say you have the inverse problem: you have "a b c" on the stack, and  
you need to "a b foo" followed by "a c bar". With these words:

: bi*, ( obj obj quot quot -- quot' quot' )
     [ [ curry ] curry ] bi@ bi* ; inline
: tri*, ( obj obj obj quot quot quot -- quot' quot' quot' )
     [ [ curry ] curry ] tri@ tri* ; inline

you can say:

[ foo ] [ bar ] bi*, bi

"bi@," and "tri@," are also defined.

For the theorists, the nbi/ntri words could be defined as repeated  
applications of bi,/tri,:

: 2bi bi, bi ;
: 3bi bi, bi, bi ;
: 4bi bi, bi, bi, bi ; ! etc.

That trick doesn't quite work for nbi*/ntri*/nbi@/ntri@ -- "bi*, bi*"  
transposes its arguments relative to "2bi*":

a b c d [ + ] [ - ] 2bi* ! -- a+b c-d
a b c d [ + ] [ - ] bi*, bi* ! -- a+c b-d

That's actually kind of handy, since either stack order could be useful.

-Joe

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to