math-binpack: Some cleanups recommended on IRC.

db4
John Benediktsson 2008-10-01 23:44:45 -07:00
parent 9cd9a98ce9
commit 1c76a6865a
1 changed files with 6 additions and 5 deletions

View File

@ -6,16 +6,17 @@ USING: sequences kernel arrays vectors accessors assocs sorting math math.functi
IN: math.binpack
: (binpack) ( bins item -- )
swap dup [ [ second ] map sum ] map swap zip sort-keys values first push ;
[ [ values sum ] map ] keep
zip sort-keys values first push ;
: binpack ( assoc n -- bins )
[ sort-values reverse [ length ] keep swap ] dip
[ / ceiling ] keep swap <array> [ <vector> ] map
swap [ dupd (binpack) ] each ;
[ sort-values <reversed> dup length ] dip
tuck / ceiling <array> [ <vector> ] map
tuck [ (binpack) ] curry each ;
: binpack* ( items n -- bins )
[ dup zip ] dip binpack [ keys ] map ;
: binpack! ( items quot n -- bins )
[ dup ] 2dip [ map zip ] dip binpack [ keys ] map ;
[ dupd map zip ] dip binpack [ keys ] map ;