Hi all,

I just thought I'd share this with the list. Here is some ugly code in  
generic.standard:

: (simplify-alist) ( class i assoc -- default assoc )
     2dup length 1- = [
         nth second { } rot drop
     ] [
         3dup >r 1+ r> nth first class< [
             >r 1+ r> (simplify-alist)
         ] [
             [ nth second ] 2keep swap 1+ tail rot drop
         ] if
     ] if ;

: simplify-alist ( class assoc -- default assoc )
     dup empty? [
         2drop [ "Unreachable" throw ] { }
     ] [
         0 swap (simplify-alist)
     ] if ;

Here is a refactored version:

: simplify-alist ( class assoc -- default assoc' )
     {
         { [ dup empty? ] [ 2drop [ "Unreachable" throw ] { } ] }
         { [ dup length 1 = ] [ nip first second { } ] }
         { [ 2dup second first class< ] [ 1 tail-slice simplify- 
alist ] }
         { [ t ] [ nip [ second second ] [ 1 tail-slice ] bi ] }
     } cond ;

What changed is that I merged the two words into one, changed the  
conditional nesting into one cond, and replaced the i/assoc pair with  
a single assoc. When advancing down the list, we do '1 tail-slice' to  
chop off the first key/value pair.

Here is an alternative version; instead of using nip in a couple of  
places, I use more combinators:

: simplify-alist ( class assoc -- default assoc' )
     {
         { [ dup empty? ] [ 2drop [ "Unreachable" throw ] { } ] }
         { [ dup length 1 = ] [ [ drop ] [ first second ] bi* { } ] }
         { [ 2dup second first class< ] [ 1 tail-slice simplify- 
alist ] }
         { [ t ] [ [ drop second second ] [ drop 1 tail-slice ] 2bi ] }
     } cond ;

The last version does not have any shuffling other than drop, 2drop,  
dup, and 2dup.

Slava

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to