]> gitweb.factorcode.org Git - factor.git/blob - extra/benchmark/pidigits/pidigits.factor
factor: trim using lists
[factor.git] / extra / benchmark / pidigits / pidigits.factor
1 ! Copyright (c) 2009 Aaron Schaefer. All rights reserved.
2 ! The contents of this file are licensed under the Simplified BSD License
3 ! A copy of the license is available at http://factorcode.org/license.txt
4 USING: arrays formatting io kernel math math.matrices
5 math.parser sequences ;
6 IN: benchmark.pidigits
7
8 : extract ( z x -- n )
9     [ first2 ] dip '[ first2 [ _ * ] [ + ] bi* ] bi@ /i ;
10
11 : next ( z -- n )
12     3 extract ;
13
14 : safe? ( z n -- ? )
15     [ 4 extract ] dip = ;
16
17 : >matrix ( q s r t -- z )
18     [ 2array ] 2bi@ 2array ;
19
20 : produce ( z y -- z' )
21     [ 10 ] dip -10 * 0 1 >matrix swap mdot ;
22
23 : gen-x ( x -- matrix )
24     dup 2 * 1 + [ 2 * 0 ] keep >matrix ;
25
26 : consume ( z k -- z' )
27     gen-x mdot ;
28
29 :: (padded-total) ( row col -- str n format )
30     "" row col + "%" "s\t:%d\n"
31     10 col - number>string glue ;
32
33 : padded-total ( row col -- )
34     (padded-total) '[ _ printf ] call( str n -- ) ;
35
36 :: (pidigits) ( k z n row col -- )
37     n 0 > [
38         z next :> y
39         z y safe? [
40             col 10 = [
41                 row 10 + y "\t:%d\n%d" printf
42                 k z y produce n 1 - row 10 + 1 (pidigits)
43             ] [
44                 y number>string write
45                 k z y produce n 1 - row col 1 + (pidigits)
46             ] if
47         ] [
48             k 1 + z k consume n row col (pidigits)
49         ] if
50     ] [ row col padded-total ] if ;
51
52 : pidigits ( n -- )
53     [ 1 { { 1 0 } { 0 1 } } ] dip 0 0 (pidigits) ;
54
55 : pidigits-benchmark ( -- )
56     2000 pidigits ;
57
58 MAIN: pidigits-benchmark