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