Hello,

Consider this cond example:

10 20 30
{ { [ dup 30 = ]      [ + + ] }
  { [ 2dup + 100 > ]  [ - - ] }
  { [ 3dup + + 70 = ] [ * * ] }
  { [ t ]             [ / / ] }
} cond

With cond, each predicate has to be careful to maintain the stack for the next 
branch. If the expression portion of the predicate has an inferable effect, 
the computer should be able to do this work for us. It would be nice if we 
could just write:

10 20 30
{ { [ 30 = ]     [ + + ] }
  { [ + 100 > ]  [ - - ] }
  { [ + + 70 = ] [ * * ] }
  { [ t ]        [ / / ] }
} switch

Indeed we can... Here's the switch macro:

MACRO: switch ( quot -- )
  [ [ preserving ] [ ] bi* ] assoc-map
  [ , cond ]
  bake ;

Here I am showing off the bi* spread combinator. 'preserving' is a cool word: 
it takes a predicate and turns it into a quotation which "preserves" it's 
arguments.

( scratchpad ) 5 6 [ > ] preserving call
----------
5
6
f
----------

Let's see what 'preserving' does to a quot.

( scratchpad ) [ > ] preserving
----------
[ [ > ] 2 nkeep 3 nrot ]
----------
( scratchpad ) 

Here it is:

: preserving ( predicate -- quot )
  dup infer effect-in
  dup 1+ swap rot
  [ , , nkeep , nrot ]
  bake ;

Ed

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to