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