]> gitweb.factorcode.org Git - factor.git/blob - extra/reports/noise/noise.factor
Merge branch 'master' into redis
[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: wlet noise body>> noise ;\r
85 \r
86 M: lambda noise body>> noise ;\r
87 \r
88 M: object noise drop { 0 0 } ;\r
89 \r
90 M: quotation noise [ noise ] map vsum { 1/4 1/2 } v+ ;\r
91 \r
92 M: array noise [ noise ] map vsum ;\r
93 \r
94 : noise-factor ( x y -- z ) / 100 * >integer ;\r
95 \r
96 : quot-noise-factor ( quot -- n )\r
97     #! For very short words, noise doesn't count so much\r
98     #! (so dup foo swap bar isn't penalized as badly).\r
99     noise first2 {\r
100         { [ over 4 <= ] [ [ drop 0 ] dip ] }\r
101         { [ over 15 >= ] [ [ 2 * ] dip ] }\r
102         [ ]\r
103     } cond\r
104     {\r
105         ! short words are easier to read\r
106         { [ dup 10 <= ] [ [ 2 / ] dip ] }\r
107         { [ dup 5 <= ] [ [ 3 / ] dip ] }\r
108         ! long words are penalized even more\r
109         { [ dup 25 >= ] [ [ 2 * ] dip 20 max ] }\r
110         { [ dup 20 >= ] [ [ 5/3 * ] dip ] }\r
111         { [ dup 15 >= ] [ [ 3/2 * ] dip ] }\r
112         [ ]\r
113     } cond noise-factor ;\r
114 \r
115 GENERIC: word-noise-factor ( word -- factor )\r
116 \r
117 M: word word-noise-factor\r
118     def>> quot-noise-factor ;\r
119 \r
120 M: lambda-word word-noise-factor\r
121     "lambda" word-prop quot-noise-factor ;\r
122 \r
123 : flatten-generics ( words -- words' )\r
124     [\r
125         dup generic? [ "methods" word-prop values ] [ 1array ] if\r
126     ] map concat ;\r
127 \r
128 : noisy-words ( -- alist )\r
129     all-words flatten-generics\r
130     [ dup word-noise-factor ] { } map>assoc\r
131     sort-values reverse ;\r
132 \r
133 : noise. ( alist -- )\r
134     standard-table-style [\r
135         [\r
136             [ [ pprint-cell ] [ pprint-cell ] bi* ] with-row\r
137         ] assoc-each\r
138     ] tabular-output ;\r
139 \r
140 : vocab-noise-factor ( vocab -- factor )\r
141     words flatten-generics\r
142     [ word-noise-factor dup 20 < [ drop 0 ] when ] map\r
143     [ 0 ] [\r
144         [ [ sum ] [ length 5 max ] bi /i ]\r
145         [ supremum ]\r
146         bi +\r
147     ] if-empty ;\r
148 \r
149 : noisy-vocabs ( -- alist )\r
150     vocabs [ dup vocab-noise-factor ] { } map>assoc\r
151     sort-values reverse ;\r
152 \r
153 : noise-report ( -- )\r
154     "NOISY WORDS:" print\r
155     noisy-words 80 head noise.\r
156     nl\r
157     "NOISY VOCABS:" print\r
158     noisy-vocabs 80 head noise. ;\r
159 \r
160 MAIN: noise-report\r