Hi,

Eric nicely gave me his solution :

DEFER: combinations

 : (combinations) [ 1 tail ] dip combinations ;

 : prefix-each [ prefix ] curry map ;

 : combinations ( seq n -- seqs )
    {
        { [ dup 0 = ] [ 2drop { { } } ] }
        { [ over empty? ] [ 2drop { } ] }
        { [ t ] [
            [ [ 1- (combinations) ] [ drop first ] 2bi prefix-each ]
            [ (combinations) ] 2bi append
        ] }
    } cond ;

It looks nice :
- only three conditions,
- very few shuffle words
- a good name
- prefix-each makes sens

OK lets try

[ 20 20 <array> 20  [ among ] with each ] time
=> "end > sequence" error

I missed something (Eric didn't !)
I add a condition to my def that becomes

: columnize ( seq -- seq )
     [ 1array ] map
; inline

: among ( seq n -- seqs )
     2dup swap length
     {
         { [ over 1 = ] [ 3drop columnize ] }
         { [ over 0 = ] [ 2drop 2drop {  } ] }
         { [ 2dup < ] [ 2drop [ 1 cut ] dip
                          [ 1- among [ append ] with map  ]
                          [ among append ] 2bi
                        ] }
         { [ 2dup = ] [ 3drop 1array ] }
         { [ 2dup > ] [ 2drop 2drop {  } ] }
     } cond
;


so again
 [ 20 20 <array> 20  [ among ] with each ] time
=> 8953 ms run / 4142 ms GC time

and after
[ 20 20 <array> 20  [ combinations ] with each ] time
=> 20500 ms run / 6359 ms GC time

I tried several times to be sure !

for "10 10 <array> 10" results are equals to 16 ms but for "large number" Eric's nice word is twice slower than mine.

WHY ?

Are there rules to optimise a word ?
On what point  must we focus to build efficient code ?

JF








-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to