Paste: Poker Hands Benchmark

Author: elasticdog
Mode: factor
Date: Sat, 20 Feb 2010 19:17:23
Plain Text |
! Copyright (c) 2009 Aaron Schaefer. All rights reserved.
! The contents of this file are licensed under the Simplified BSD License
! A copy of the license is available at http://factorcode.org/license.txt
USING: accessors arrays formatting fry kernel math math.combinatorics poker
    poker.private sequences sequences.product ;
IN: benchmark.poker-hands

! Test the evaluator by generating all 2,598,960 possible five card hands,
! calculating each hand's distinct value, and displaying the frequency count of
! each hand type.

! Most of the time is spent generating the 52 5 nCk combinations of hands.

<PRIVATE

: print-frequency ( seq -- )
    [ [ VALUE_STR nth swap "%15s: %d\n" printf ] each-index ] keep
    sum [ "" "TOTAL" ] dip "%'=25s\n%15s: %d\n" printf ;

PRIVATE>

: poker-hands ( -- )
    <deck> 5 9 0 <array> [
        '[ hand-value value>rank _ [ 1 + ] change-nth ] each-combination
    ] keep print-frequency ;

MAIN: poker-hands

New Annotation

Summary:
Author:
Mode:
Body: