]> gitweb.factorcode.org Git - factor.git/blob - extra/reports/noise/noise.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / extra / reports / noise / noise.factor
1 ! Copyright (C) 2008 Slava Pestov.\r
2 ! See http://factorcode.org/license.txt for BSD license.\r
3 USING: accessors assocs math kernel shuffle generalizations\r
4 words quotations arrays combinators sequences math.vectors\r
5 io.styles prettyprint vocabs sorting io generic\r
6 math.statistics math.order locals.types\r
7 locals.definitions ;\r
8 IN: reports.noise\r
9 \r
10 : badness ( word -- n )\r
11     H{\r
12         { -nrot 5 }\r
13         { -roll 4 }\r
14         { -rot 3 }\r
15         { bi@ 1 }\r
16         { 2curry 1 }\r
17         { 2drop 1 }\r
18         { 2dup 1 }\r
19         { 2keep 1 }\r
20         { 2nip 2 }\r
21         { 2over 4 }\r
22         { 2swap 3 }\r
23         { 3curry 2 }\r
24         { 3drop 1 }\r
25         { 3dup 2 }\r
26         { 3keep 3 }\r
27         { 4drop 2 }\r
28         { 4dup 3 }\r
29         { compose 1/2 }\r
30         { curry 1/3 }\r
31         { dip 1 }\r
32         { 2dip 2 }\r
33         { drop 1/3 }\r
34         { dup 1/3 }\r
35         { if 1/3 }\r
36         { when 1/4 }\r
37         { unless 1/4 }\r
38         { when* 1/3 }\r
39         { unless* 1/3 }\r
40         { ?if 1/2 }\r
41         { cond 1/2 }\r
42         { case 1/2 }\r
43         { keep 1 }\r
44         { napply 2 }\r
45         { ncurry 3 }\r
46         { ndip 5 }\r
47         { ndrop 2 }\r
48         { ndup 3 }\r
49         { nip 2 }\r
50         { nkeep 5 }\r
51         { npick 6 }\r
52         { nrot 5 }\r
53         { ntuck 6 }\r
54         { nwith 4 }\r
55         { over 2 }\r
56         { pick 4 }\r
57         { roll 4 }\r
58         { rot 3 }\r
59         { spin 3 }\r
60         { swap 1 }\r
61         { swapd 3 }\r
62         { tuck 2 }\r
63         { with 1/2 }\r
64 \r
65         { bi 1/2 }\r
66         { tri 1 }\r
67         { bi* 1/2 }\r
68         { tri* 1 }\r
69 \r
70         { cleave 2 }\r
71         { spread 2 }\r
72     } at 0 or ;\r
73 \r
74 : vsum ( pairs -- pair ) { 0 0 } [ v+ ] reduce ;\r
75 \r
76 GENERIC: noise ( obj -- pair )\r
77 \r
78 M: word noise badness 1 2array ;\r
79 \r
80 M: wrapper noise wrapped>> noise ;\r
81 \r
82 M: let noise body>> noise ;\r
83 \r
84 M: lambda noise body>> noise ;\r
85 \r
86 M: object noise drop { 0 0 } ;\r
87 \r
88 M: quotation noise [ noise ] map vsum { 1/4 1/2 } v+ ;\r
89 \r
90 M: array noise [ noise ] map vsum ;\r
91 \r
92 : noise-factor ( x y -- z ) / 100 * >integer ;\r
93 \r
94 : quot-noise-factor ( quot -- n )\r
95     #! For very short words, noise doesn't count so much\r
96     #! (so dup foo swap bar isn't penalized as badly).\r
97     noise first2 {\r
98         { [ over 4 <= ] [ [ drop 0 ] dip ] }\r
99         { [ over 15 >= ] [ [ 2 * ] dip ] }\r
100         [ ]\r
101     } cond\r
102     {\r
103         ! short words are easier to read\r
104         { [ dup 10 <= ] [ [ 2 / ] dip ] }\r
105         { [ dup 5 <= ] [ [ 3 / ] dip ] }\r
106         ! long words are penalized even more\r
107         { [ dup 25 >= ] [ [ 2 * ] dip 20 max ] }\r
108         { [ dup 20 >= ] [ [ 5/3 * ] dip ] }\r
109         { [ dup 15 >= ] [ [ 3/2 * ] dip ] }\r
110         [ ]\r
111     } cond noise-factor ;\r
112 \r
113 GENERIC: word-noise-factor ( word -- factor )\r
114 \r
115 M: word word-noise-factor\r
116     def>> quot-noise-factor ;\r
117 \r
118 M: lambda-word word-noise-factor\r
119     "lambda" word-prop quot-noise-factor ;\r
120 \r
121 : flatten-generics ( words -- words' )\r
122     [\r
123         dup generic? [ "methods" word-prop values ] [ 1array ] if\r
124     ] map concat ;\r
125 \r
126 : noisy-words ( -- alist )\r
127     all-words flatten-generics\r
128     [ dup word-noise-factor ] { } map>assoc\r
129     sort-values reverse ;\r
130 \r
131 : noise. ( alist -- )\r
132     standard-table-style [\r
133         [\r
134             [ [ pprint-cell ] [ pprint-cell ] bi* ] with-row\r
135         ] assoc-each\r
136     ] tabular-output ;\r
137 \r
138 : vocab-noise-factor ( vocab -- factor )\r
139     words flatten-generics\r
140     [ word-noise-factor dup 20 < [ drop 0 ] when ] map\r
141     [ 0 ] [\r
142         [ [ sum ] [ length 5 max ] bi /i ]\r
143         [ supremum ]\r
144         bi +\r
145     ] if-empty ;\r
146 \r
147 : noisy-vocabs ( -- alist )\r
148     vocabs [ dup vocab-noise-factor ] { } map>assoc\r
149     sort-values reverse ;\r
150 \r
151 : noise-report ( -- )\r
152     "NOISY WORDS:" print\r
153     noisy-words 80 head noise.\r
154     nl\r
155     "NOISY VOCABS:" print\r
156     noisy-vocabs 80 head noise. ;\r
157 \r
158 MAIN: noise-report\r