Speed up best-hands a bit using reduce and add a test

db4
Aaron Schaefer 2009-05-07 21:23:58 -04:00
parent d9e4f6e9cb
commit f465a013d7
3 changed files with 12 additions and 3 deletions

View File

@ -116,3 +116,10 @@ PRIVATE>
[ <combo> [ choose [0,b) ] keep ] dip
'[ _ apply-combination @ ] each ; inline
: map-combinations ( seq k quot -- )
[ <combo> [ choose [0,b) ] keep ] dip
'[ _ apply-combination @ ] map ; inline
: reduce-combinations ( seq k identity quot -- result )
[ -rot ] dip each-combination ; inline

View File

@ -26,3 +26,5 @@ IN: poker.tests
[ t ] [ "7C 5D 4H 3S 2C" "7D 5D 4D 3C 2S" [ <hand> ] bi@ = ] unit-test
[ f ] [ "7C 5D 4H 3S 2C" "7D 5D 4D 3C 2S" [ <hand> ] bi@ eq? ] unit-test
[ 190 ] [ "AS KD JC KH 2D 2S KH" best-hand value>> ] unit-test

View File

@ -179,7 +179,7 @@ PRIVATE>
TUPLE: hand
{ cards sequence }
{ value integer } ;
{ value integer initial: 9999 } ;
M: hand <=> [ value>> ] compare ;
M: hand equal?
@ -189,8 +189,8 @@ M: hand equal?
parse-cards dup hand-value hand boa ;
: best-hand ( str -- hand )
parse-cards 5 all-combinations
[ dup hand-value hand boa ] map infimum ;
parse-cards 5 hand new
[ dup hand-value hand boa min ] reduce-combinations ;
: >cards ( hand -- str )
cards>> [ card>string ] map " " join ;