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