ruby-nuggets 0.9.9 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (244) hide show
  1. checksums.yaml +4 -4
  2. metadata +20 -324
  3. data/COPYING +0 -663
  4. data/ChangeLog +0 -7
  5. data/README +0 -62
  6. data/Rakefile +0 -47
  7. data/lib/nuggets.rb +0 -73
  8. data/lib/nuggets/all.rb +0 -36
  9. data/lib/nuggets/all_mixins.rb +0 -34
  10. data/lib/nuggets/ansicolor2css.rb +0 -125
  11. data/lib/nuggets/argv/option.rb +0 -3
  12. data/lib/nuggets/argv/option_mixin.rb +0 -85
  13. data/lib/nuggets/array/boost.rb +0 -5
  14. data/lib/nuggets/array/boost_mixin.rb +0 -78
  15. data/lib/nuggets/array/combination.rb +0 -63
  16. data/lib/nuggets/array/correlation.rb +0 -5
  17. data/lib/nuggets/array/correlation_mixin.rb +0 -64
  18. data/lib/nuggets/array/flatten_once.rb +0 -57
  19. data/lib/nuggets/array/flush.rb +0 -5
  20. data/lib/nuggets/array/flush_mixin.rb +0 -47
  21. data/lib/nuggets/array/format.rb +0 -66
  22. data/lib/nuggets/array/hashify.rb +0 -5
  23. data/lib/nuggets/array/hashify_mixin.rb +0 -48
  24. data/lib/nuggets/array/histogram.rb +0 -5
  25. data/lib/nuggets/array/histogram_mixin.rb +0 -170
  26. data/lib/nuggets/array/in_order.rb +0 -51
  27. data/lib/nuggets/array/limit.rb +0 -5
  28. data/lib/nuggets/array/limit_mixin.rb +0 -57
  29. data/lib/nuggets/array/mean.rb +0 -5
  30. data/lib/nuggets/array/mean_mixin.rb +0 -203
  31. data/lib/nuggets/array/median.rb +0 -5
  32. data/lib/nuggets/array/median_mixin.rb +0 -74
  33. data/lib/nuggets/array/mode.rb +0 -5
  34. data/lib/nuggets/array/mode_mixin.rb +0 -70
  35. data/lib/nuggets/array/monotone.rb +0 -84
  36. data/lib/nuggets/array/only.rb +0 -42
  37. data/lib/nuggets/array/rand.rb +0 -45
  38. data/lib/nuggets/array/regression.rb +0 -5
  39. data/lib/nuggets/array/regression_mixin.rb +0 -150
  40. data/lib/nuggets/array/runiq.rb +0 -5
  41. data/lib/nuggets/array/runiq_mixin.rb +0 -53
  42. data/lib/nuggets/array/shuffle.rb +0 -133
  43. data/lib/nuggets/array/standard_deviation.rb +0 -5
  44. data/lib/nuggets/array/standard_deviation_mixin.rb +0 -51
  45. data/lib/nuggets/array/to_hash.rb +0 -65
  46. data/lib/nuggets/array/variance.rb +0 -5
  47. data/lib/nuggets/array/variance_mixin.rb +0 -82
  48. data/lib/nuggets/cli.rb +0 -8
  49. data/lib/nuggets/content_type.rb +0 -98
  50. data/lib/nuggets/dotted_decimal.rb +0 -60
  51. data/lib/nuggets/enumerable/agrep.rb +0 -80
  52. data/lib/nuggets/enumerable/all_any_extended.rb +0 -67
  53. data/lib/nuggets/enumerable/minmax.rb +0 -103
  54. data/lib/nuggets/env/set.rb +0 -3
  55. data/lib/nuggets/env/set_mixin.rb +0 -68
  56. data/lib/nuggets/env/user_encoding.rb +0 -3
  57. data/lib/nuggets/env/user_encoding_mixin.rb +0 -55
  58. data/lib/nuggets/env/user_home.rb +0 -3
  59. data/lib/nuggets/env/user_home_mixin.rb +0 -57
  60. data/lib/nuggets/file/ext.rb +0 -5
  61. data/lib/nuggets/file/ext_mixin.rb +0 -88
  62. data/lib/nuggets/file/replace.rb +0 -5
  63. data/lib/nuggets/file/replace_mixin.rb +0 -56
  64. data/lib/nuggets/file/sub.rb +0 -5
  65. data/lib/nuggets/file/sub_mixin.rb +0 -96
  66. data/lib/nuggets/file/which.rb +0 -5
  67. data/lib/nuggets/file/which_mixin.rb +0 -73
  68. data/lib/nuggets/hash/at.rb +0 -69
  69. data/lib/nuggets/hash/deep_fetch.rb +0 -5
  70. data/lib/nuggets/hash/deep_fetch_mixin.rb +0 -75
  71. data/lib/nuggets/hash/deep_merge.rb +0 -5
  72. data/lib/nuggets/hash/deep_merge_mixin.rb +0 -54
  73. data/lib/nuggets/hash/idmap.rb +0 -5
  74. data/lib/nuggets/hash/idmap_mixin.rb +0 -42
  75. data/lib/nuggets/hash/in_order.rb +0 -44
  76. data/lib/nuggets/hash/insert.rb +0 -54
  77. data/lib/nuggets/hash/nest.rb +0 -5
  78. data/lib/nuggets/hash/nest_mixin.rb +0 -79
  79. data/lib/nuggets/hash/only.rb +0 -53
  80. data/lib/nuggets/hash/seen.rb +0 -5
  81. data/lib/nuggets/hash/seen_mixin.rb +0 -59
  82. data/lib/nuggets/hash/unroll.rb +0 -5
  83. data/lib/nuggets/hash/unroll_mixin.rb +0 -89
  84. data/lib/nuggets/hash/zip.rb +0 -5
  85. data/lib/nuggets/hash/zip_mixin.rb +0 -160
  86. data/lib/nuggets/i18n.rb +0 -156
  87. data/lib/nuggets/integer/factorial.rb +0 -57
  88. data/lib/nuggets/integer/length.rb +0 -5
  89. data/lib/nuggets/integer/length_mixin.rb +0 -50
  90. data/lib/nuggets/integer/map.rb +0 -5
  91. data/lib/nuggets/integer/map_mixin.rb +0 -43
  92. data/lib/nuggets/integer/to_binary_s.rb +0 -39
  93. data/lib/nuggets/io/agrep.rb +0 -44
  94. data/lib/nuggets/io/interact.rb +0 -5
  95. data/lib/nuggets/io/interact_mixin.rb +0 -160
  96. data/lib/nuggets/io/modes.rb +0 -122
  97. data/lib/nuggets/io/null.rb +0 -5
  98. data/lib/nuggets/io/null_mixin.rb +0 -41
  99. data/lib/nuggets/io/redirect.rb +0 -5
  100. data/lib/nuggets/io/redirect_mixin.rb +0 -51
  101. data/lib/nuggets/lazy_attr.rb +0 -45
  102. data/lib/nuggets/log_parser.rb +0 -71
  103. data/lib/nuggets/log_parser/apache.rb +0 -102
  104. data/lib/nuggets/log_parser/rails.rb +0 -220
  105. data/lib/nuggets/lsi.rb +0 -8
  106. data/lib/nuggets/midos.rb +0 -8
  107. data/lib/nuggets/mysql.rb +0 -8
  108. data/lib/nuggets/net/success.rb +0 -60
  109. data/lib/nuggets/numeric/between.rb +0 -2
  110. data/lib/nuggets/numeric/duration.rb +0 -101
  111. data/lib/nuggets/numeric/limit.rb +0 -63
  112. data/lib/nuggets/numeric/signum.rb +0 -53
  113. data/lib/nuggets/numeric/to_multiple.rb +0 -62
  114. data/lib/nuggets/object/blank.rb +0 -20
  115. data/lib/nuggets/object/blank_mixin.rb +0 -100
  116. data/lib/nuggets/object/boolean.rb +0 -5
  117. data/lib/nuggets/object/boolean_mixin.rb +0 -62
  118. data/lib/nuggets/object/eigenclass.rb +0 -2
  119. data/lib/nuggets/object/ghost_class.rb +0 -2
  120. data/lib/nuggets/object/metaclass.rb +0 -2
  121. data/lib/nuggets/object/msend.rb +0 -5
  122. data/lib/nuggets/object/msend_mixin.rb +0 -44
  123. data/lib/nuggets/object/silence.rb +0 -5
  124. data/lib/nuggets/object/silence_mixin.rb +0 -45
  125. data/lib/nuggets/object/singleton_class.rb +0 -5
  126. data/lib/nuggets/object/singleton_class_mixin.rb +0 -96
  127. data/lib/nuggets/object/uniclass.rb +0 -2
  128. data/lib/nuggets/object/virtual_class.rb +0 -2
  129. data/lib/nuggets/pluggable.rb +0 -92
  130. data/lib/nuggets/proc/bind.rb +0 -5
  131. data/lib/nuggets/proc/bind_mixin.rb +0 -52
  132. data/lib/nuggets/range/quantile.rb +0 -5
  133. data/lib/nuggets/range/quantile_mixin.rb +0 -43
  134. data/lib/nuggets/rdf/compression.rb +0 -6
  135. data/lib/nuggets/rdf/prefix.rb +0 -6
  136. data/lib/nuggets/rdf/turtle.rb +0 -8
  137. data/lib/nuggets/rdf/turtle/reader.rb +0 -6
  138. data/lib/nuggets/rdf/uri.rb +0 -6
  139. data/lib/nuggets/ruby.rb +0 -236
  140. data/lib/nuggets/statistics.rb +0 -12
  141. data/lib/nuggets/statistics_mixins.rb +0 -12
  142. data/lib/nuggets/string/camelscore.rb +0 -5
  143. data/lib/nuggets/string/camelscore_mixin.rb +0 -117
  144. data/lib/nuggets/string/capitalize_first.rb +0 -47
  145. data/lib/nuggets/string/case.rb +0 -82
  146. data/lib/nuggets/string/evaluate.rb +0 -5
  147. data/lib/nuggets/string/evaluate_mixin.rb +0 -48
  148. data/lib/nuggets/string/msub.rb +0 -85
  149. data/lib/nuggets/string/nsub.rb +0 -66
  150. data/lib/nuggets/string/sub_with_md.rb +0 -112
  151. data/lib/nuggets/string/wc.rb +0 -5
  152. data/lib/nuggets/string/wc_mixin.rb +0 -96
  153. data/lib/nuggets/string/word_wrap.rb +0 -77
  154. data/lib/nuggets/string/xor.rb +0 -5
  155. data/lib/nuggets/string/xor_mixin.rb +0 -60
  156. data/lib/nuggets/tempfile/open.rb +0 -58
  157. data/lib/nuggets/uri/content_type.rb +0 -5
  158. data/lib/nuggets/uri/content_type_mixin.rb +0 -48
  159. data/lib/nuggets/uri/exist.rb +0 -5
  160. data/lib/nuggets/uri/exist_mixin.rb +0 -57
  161. data/lib/nuggets/uri/redirect.rb +0 -5
  162. data/lib/nuggets/uri/redirect_mixin.rb +0 -102
  163. data/lib/nuggets/util/ansicolor2css.rb +0 -4
  164. data/lib/nuggets/util/cli.rb +0 -4
  165. data/lib/nuggets/util/content_type.rb +0 -4
  166. data/lib/nuggets/util/dotted_decimal.rb +0 -3
  167. data/lib/nuggets/util/i18n.rb +0 -4
  168. data/lib/nuggets/util/lazy_attr.rb +0 -4
  169. data/lib/nuggets/util/log_parser.rb +0 -4
  170. data/lib/nuggets/util/log_parser/apache.rb +0 -4
  171. data/lib/nuggets/util/log_parser/rails.rb +0 -4
  172. data/lib/nuggets/util/midos.rb +0 -4
  173. data/lib/nuggets/util/mysql.rb +0 -4
  174. data/lib/nuggets/util/pluggable.rb +0 -4
  175. data/lib/nuggets/util/ruby.rb +0 -4
  176. data/lib/nuggets/version.rb +0 -27
  177. data/spec/nuggets/array/boost_spec.rb +0 -50
  178. data/spec/nuggets/array/combination_spec.rb +0 -25
  179. data/spec/nuggets/array/correlation_spec.rb +0 -81
  180. data/spec/nuggets/array/flatten_once_spec.rb +0 -16
  181. data/spec/nuggets/array/flush_spec.rb +0 -43
  182. data/spec/nuggets/array/format_spec.rb +0 -52
  183. data/spec/nuggets/array/hashify_spec.rb +0 -41
  184. data/spec/nuggets/array/histogram_spec.rb +0 -87
  185. data/spec/nuggets/array/in_order_spec.rb +0 -13
  186. data/spec/nuggets/array/limit_spec.rb +0 -62
  187. data/spec/nuggets/array/mean_spec.rb +0 -203
  188. data/spec/nuggets/array/median_spec.rb +0 -77
  189. data/spec/nuggets/array/mode_spec.rb +0 -57
  190. data/spec/nuggets/array/monotone_spec.rb +0 -30
  191. data/spec/nuggets/array/only_spec.rb +0 -26
  192. data/spec/nuggets/array/regression_spec.rb +0 -54
  193. data/spec/nuggets/array/runiq_spec.rb +0 -25
  194. data/spec/nuggets/array/standard_deviation_spec.rb +0 -33
  195. data/spec/nuggets/array/to_hash_spec.rb +0 -28
  196. data/spec/nuggets/array/variance_spec.rb +0 -106
  197. data/spec/nuggets/dotted_decimal_spec.rb +0 -27
  198. data/spec/nuggets/enumerable/all_any_extended_spec.rb +0 -31
  199. data/spec/nuggets/enumerable/minmax_spec.rb +0 -21
  200. data/spec/nuggets/env/set_spec.rb +0 -29
  201. data/spec/nuggets/env/user_encoding_spec.rb +0 -38
  202. data/spec/nuggets/env/user_home_spec.rb +0 -42
  203. data/spec/nuggets/file/ext_spec.rb +0 -38
  204. data/spec/nuggets/file/replace_spec.rb +0 -95
  205. data/spec/nuggets/file/sub_spec.rb +0 -149
  206. data/spec/nuggets/file/which_spec.rb +0 -22
  207. data/spec/nuggets/hash/at_spec.rb +0 -19
  208. data/spec/nuggets/hash/deep_fetch_spec.rb +0 -159
  209. data/spec/nuggets/hash/deep_merge_spec.rb +0 -78
  210. data/spec/nuggets/hash/in_order_spec.rb +0 -12
  211. data/spec/nuggets/hash/insert_spec.rb +0 -13
  212. data/spec/nuggets/hash/nest_spec.rb +0 -102
  213. data/spec/nuggets/hash/only_spec.rb +0 -29
  214. data/spec/nuggets/hash/seen_spec.rb +0 -36
  215. data/spec/nuggets/hash/unroll_spec.rb +0 -68
  216. data/spec/nuggets/i18n_spec.rb +0 -13
  217. data/spec/nuggets/integer/factorial_spec.rb +0 -10
  218. data/spec/nuggets/integer/length_spec.rb +0 -18
  219. data/spec/nuggets/integer/map_spec.rb +0 -19
  220. data/spec/nuggets/integer/to_binary_s_spec.rb +0 -19
  221. data/spec/nuggets/numeric/duration_spec.rb +0 -25
  222. data/spec/nuggets/numeric/limit_spec.rb +0 -16
  223. data/spec/nuggets/numeric/signum_spec.rb +0 -16
  224. data/spec/nuggets/numeric/to_multiple_spec.rb +0 -16
  225. data/spec/nuggets/object/blank_spec.rb +0 -34
  226. data/spec/nuggets/object/boolean_spec.rb +0 -23
  227. data/spec/nuggets/object/msend_spec.rb +0 -25
  228. data/spec/nuggets/object/silence_spec.rb +0 -36
  229. data/spec/nuggets/object/singleton_class_spec.rb +0 -51
  230. data/spec/nuggets/proc/bind_spec.rb +0 -28
  231. data/spec/nuggets/range/quantile_spec.rb +0 -33
  232. data/spec/nuggets/string/camelscore_spec.rb +0 -114
  233. data/spec/nuggets/string/capitalize_first_spec.rb +0 -13
  234. data/spec/nuggets/string/case_spec.rb +0 -31
  235. data/spec/nuggets/string/evaluate_spec.rb +0 -24
  236. data/spec/nuggets/string/msub_spec.rb +0 -20
  237. data/spec/nuggets/string/nsub_spec.rb +0 -13
  238. data/spec/nuggets/string/sub_with_md_spec.rb +0 -25
  239. data/spec/nuggets/string/wc_spec.rb +0 -73
  240. data/spec/nuggets/string/word_wrap_spec.rb +0 -81
  241. data/spec/nuggets/string/xor_spec.rb +0 -57
  242. data/spec/nuggets/uri/content_type_spec.rb +0 -42
  243. data/spec/nuggets/uri/exist_spec.rb +0 -49
  244. data/spec/spec_helper.rb +0 -36
@@ -1,5 +0,0 @@
1
- require 'nuggets/array/histogram_mixin'
2
-
3
- class Array
4
- include Nuggets::Array::HistogramMixin
5
- end
@@ -1,170 +0,0 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2011 Jens Wille #
8
- # #
9
- # Authors: #
10
- # Jens Wille <jens.wille@gmail.com> #
11
- # #
12
- # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
- # under the terms of the GNU Affero General Public License as published by #
14
- # the Free Software Foundation; either version 3 of the License, or (at your #
15
- # option) any later version. #
16
- # #
17
- # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
20
- # for more details. #
21
- # #
22
- # You should have received a copy of the GNU Affero General Public License #
23
- # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
- # #
25
- ###############################################################################
26
- #++
27
-
28
- module Nuggets
29
- class Array
30
- module HistogramMixin
31
-
32
- # Provides some default formats for #formatted_histogram.
33
- #
34
- # Example:
35
- #
36
- # (default) ab [==] 2
37
- # (percent) xyz [===] 3 (37.50%)
38
- # (numeric) 42 [==] 2
39
- # (numeric_percent) 123 [=] 1 (12.50%)
40
- #
41
- # The "numeric" variants format the item as a (decimal) number.
42
- FORMATS = {
43
- :default => '%-*s [%s]%*s %*d',
44
- :percent => '%-*s [%s]%*s %*d (%.2f%%)',
45
- :numeric => '%*d [%s]%*s %*d',
46
- :numeric_percent => '%*d [%s]%*s %*d (%.2f%%)'
47
- }
48
-
49
- # Encapsulates a #histogram item and provides the following attributes (see
50
- # also #annotated_histogram):
51
- #
52
- # item:: The original item
53
- # freq:: The item's frequency in the collection
54
- # percentage:: The percentage of the item's frequency in the collection
55
- # max_freq:: The maximum frequency in the collection
56
- # max_freq_length:: The maximum frequency's "width"
57
- # max_item_length:: The maximum item length in the collection
58
- HistogramItem = ::Struct.new(
59
- :item, :freq, :max_freq, :max_freq_length, :max_item_length, :percentage
60
- )
61
-
62
- # call-seq:
63
- # array.histogram => aHash
64
- # array.histogram { |x| ... } => aHash
65
- #
66
- # Calculates the {frequency histogram}[http://en.wikipedia.org/wiki/Histogram]
67
- # of the values in _array_. Returns a Hash that maps any value, or the result
68
- # of the value yielded to the block, to its frequency.
69
- def histogram
70
- hist = ::Hash.new(0)
71
- each { |x| hist[block_given? ? yield(x) : x] += 1 }
72
- hist
73
- end
74
-
75
- # call-seq:
76
- # array.probability_mass_function => aHash
77
- # array.probability_mass_function { |x| ... } => aHash
78
- #
79
- # Calculates the {probability mass function}[http://en.wikipedia.org/wiki/Probability_mass_function]
80
- # (normalized histogram) of the values in _array_. Returns a Hash that
81
- # maps any value, or the result of the value yielded to the block, to
82
- # its probability (via #histogram).
83
- def probability_mass_function(&block)
84
- hist, n = histogram(&block), size.to_f
85
- hist.each { |k, v| hist[k] = v / n }
86
- end
87
-
88
- alias_method :pmf, :probability_mass_function
89
-
90
- # call-seq:
91
- # array.annotated_histogram => anArray
92
- # array.annotated_histogram { |hist_item| ... } => aHash
93
- #
94
- # Calculates the #histogram for _array_ and yields each histogram item
95
- # (see HistogramItem) to the block or returns an Array of the histogram
96
- # items.
97
- def annotated_histogram
98
- hist, items = histogram, []
99
-
100
- percentage = size / 100.0
101
-
102
- max_freq = hist.values.max
103
- max_freq_length = max_freq.to_s.length
104
-
105
- max_item_length = hist.keys.map { |item| item.to_s.length }.max
106
-
107
- # try to sort the histogram hash
108
- begin
109
- hist = hist.sort
110
- rescue ::ArgumentError
111
- end
112
-
113
- hist.each { |item, freq|
114
- hist_item = HistogramItem.new(
115
- item, freq, max_freq, max_freq_length, max_item_length, freq / percentage
116
- )
117
-
118
- block_given? ? yield(hist_item) : items << hist_item
119
- }
120
-
121
- block_given? ? hist : items
122
- end
123
-
124
- # call-seq:
125
- # array.formatted_histogram([format[, indicator]]) => aString
126
- #
127
- # Returns the #histogram of _array_ as a formatted String according to
128
- # +format+, using +indicator+ to draw the frequency bar.
129
- #
130
- # +format+ may be a Symbol indicating one of the provided default formats
131
- # (see FORMATS) or a format String (see Kernel#sprintf) that will receive
132
- # the following arguments (in order):
133
- #
134
- # 1. +max_item_length+ (Integer)
135
- # 1. +item+ (String)
136
- # 1. "frequency_bar" (String)
137
- # 1. "padding" (String)
138
- # 1. +max_freq_length+ (Integer)
139
- # 1. +freq+ (Integer)
140
- # 1. +percentage+ (Float, optional)
141
- #
142
- # See HistogramItem for further details on the individual arguments.
143
- def formatted_histogram(format = :default, indicator = '=')
144
- format = FORMATS[format] if FORMATS.key?(format)
145
- raise ::TypeError, "String expected, got #{format.class}" unless format.is_a?(::String)
146
-
147
- include_percentage = format.include?('%%')
148
- indicator_length = indicator.length
149
-
150
- lines = []
151
-
152
- annotated_histogram { |hist|
153
- arguments = [
154
- hist.max_item_length, hist.item, # item (padded)
155
- indicator * hist.freq, # indicator bar
156
- (hist.max_freq - hist.freq) * indicator_length, '', # indicator padding
157
- hist.max_freq_length, hist.freq # frequency (padded)
158
- ]
159
-
160
- arguments << hist.percentage if include_percentage # percentage (optional)
161
-
162
- lines << format % arguments
163
- }
164
-
165
- lines.join("\n")
166
- end
167
-
168
- end
169
- end
170
- end
@@ -1,51 +0,0 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2011 Jens Wille #
8
- # #
9
- # Authors: #
10
- # Jens Wille <jens.wille@gmail.com> #
11
- # #
12
- # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
- # under the terms of the GNU Affero General Public License as published by #
14
- # the Free Software Foundation; either version 3 of the License, or (at your #
15
- # option) any later version. #
16
- # #
17
- # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
20
- # for more details. #
21
- # #
22
- # You should have received a copy of the GNU Affero General Public License #
23
- # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
- # #
25
- ###############################################################################
26
- #++
27
-
28
- class Array
29
-
30
- # call-seq:
31
- # array.in_order(*ordered) => new_array
32
- #
33
- # Force order, but ignore non-existing and keep remaining.
34
- #
35
- # Examples:
36
- # [:created_at, :email, :login, :updated_at].in_order(:login, :email) #=> [:login, :email, :created_at, :updated_at]
37
- # [:created_at, :email, :login, :updated_at].in_order(:email, :address) #=> [:email, :created_at, :login, :updated_at]
38
- def in_order(*ordered)
39
- ordered &= self
40
- ordered + (self - ordered)
41
- end
42
-
43
- # call-seq:
44
- # array.in_order!(*ordered) => array
45
- #
46
- # Destructive version of #in_order.
47
- def in_order!(*ordered)
48
- replace(in_order(*ordered))
49
- end
50
-
51
- end
@@ -1,5 +0,0 @@
1
- require 'nuggets/array/limit_mixin'
2
-
3
- class Array
4
- include Nuggets::Array::LimitMixin
5
- end
@@ -1,57 +0,0 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2011 Jens Wille #
8
- # #
9
- # Authors: #
10
- # Jens Wille <jens.wille@gmail.com> #
11
- # #
12
- # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
- # under the terms of the GNU Affero General Public License as published by #
14
- # the Free Software Foundation; either version 3 of the License, or (at your #
15
- # option) any later version. #
16
- # #
17
- # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
20
- # for more details. #
21
- # #
22
- # You should have received a copy of the GNU Affero General Public License #
23
- # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
- # #
25
- ###############################################################################
26
- #++
27
-
28
- module Nuggets
29
- class Array
30
- module LimitMixin
31
-
32
- # call-seq:
33
- # array.limit(min, max) => anArray
34
- #
35
- # Returns a new array of all distinct values in _array_ limited to +min+
36
- # and +max+ (cf. Numeric#limit). If +uniq+ is +true+, resulting duplicates
37
- # will be removed.
38
- def limit(min, max, uniq = true)
39
- limited = cap(min..max)
40
- limited.uniq! if uniq
41
- limited
42
- end
43
-
44
- alias_method :between, :limit
45
-
46
- def cap(max)
47
- if max.respond_to?(:begin)
48
- min, max = max.begin, max.end
49
- map { |item| item.limit(min, max) }
50
- else
51
- map { |item| item.max(max) }
52
- end
53
- end
54
-
55
- end
56
- end
57
- end
@@ -1,5 +0,0 @@
1
- require 'nuggets/array/mean_mixin'
2
-
3
- class Array
4
- include Nuggets::Array::MeanMixin
5
- end
@@ -1,203 +0,0 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2013 Jens Wille #
8
- # #
9
- # Authors: #
10
- # Jens Wille <jens.wille@gmail.com> #
11
- # #
12
- # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
- # under the terms of the GNU Affero General Public License as published by #
14
- # the Free Software Foundation; either version 3 of the License, or (at your #
15
- # option) any later version. #
16
- # #
17
- # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
20
- # for more details. #
21
- # #
22
- # You should have received a copy of the GNU Affero General Public License #
23
- # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
- # #
25
- ###############################################################################
26
- #++
27
-
28
- module Nuggets
29
- class Array
30
- module MeanMixin
31
-
32
- # call-seq:
33
- # array.generalized_mean(exponent) => aFloat
34
- # array.generalized_mean(exponent) { |x| ... } => aFloat
35
- #
36
- # Calculates the {generalized mean}[http://en.wikipedia.org/wiki/Generalized_mean]
37
- # of the values in _array_ for +exponent+. Returns the #geometric_mean if
38
- # +exponent+ is zero.
39
- #
40
- # An optional block may be passed to provide a weight for each value.
41
- # Defaults to 1. Returns +nil+ if the sum of all weights is zero (this
42
- # includes _array_ being empty).
43
- def generalized_mean(exponent, &block)
44
- return geometric_mean(&block) if exponent.zero?
45
-
46
- total, weights = 0, 0
47
-
48
- each { |x|
49
- weight = block ? block[x] : 1
50
-
51
- total += weight * x ** exponent
52
- weights += weight
53
- }
54
-
55
- (total / weights.to_f) ** (1.0 / exponent) unless weights.zero?
56
- end
57
-
58
- alias_method :power_mean, :generalized_mean
59
- alias_method :minkowski_mean, :generalized_mean
60
-
61
- # call-seq:
62
- # array.arithmetic_mean => aFloat
63
- # array.arithmetic_mean { |x| ... } => aFloat
64
- #
65
- # Calculates the {arithmetic mean}[http://en.wikipedia.org/wiki/Arithmetic_mean]
66
- # of the values in _array_.
67
- #
68
- # An optional block may be passed to provide a weight for each value.
69
- # Defaults to 1. Returns +nil+ if the sum of all weights is zero (this
70
- # includes _array_ being empty).
71
- def arithmetic_mean(&block)
72
- generalized_mean(1, &block)
73
- end
74
-
75
- alias_method :mean, :arithmetic_mean
76
- alias_method :average, :arithmetic_mean
77
- alias_method :avg, :arithmetic_mean
78
-
79
- # call-seq:
80
- # array.root_mean_square => aFloat
81
- # array.root_mean_square { |x| ... } => aFloat
82
- #
83
- # Calculates the {root mean square}[http://en.wikipedia.org/wiki/Root_mean_square]
84
- # (quadratic mean) of the values in _array_.
85
- #
86
- # An optional block may be passed to provide a weight for each value.
87
- # Defaults to 1. Returns +nil+ if the sum of all weights is zero (this
88
- # includes _array_ being empty).
89
- def root_mean_square(&block)
90
- generalized_mean(2, &block)
91
- end
92
-
93
- alias_method :rms, :root_mean_square
94
- alias_method :quadratic_mean, :root_mean_square
95
-
96
- # call-seq:
97
- # array.harmonic_mean => aFloat
98
- # array.harmonic_mean { |x| ... } => aFloat
99
- #
100
- # Calculates the {harmonic mean}[http://en.wikipedia.org/wiki/Harmonic_mean]
101
- # of the values in _array_.
102
- #
103
- # An optional block may be passed to provide a weight for each value.
104
- # Defaults to 1. Returns +nil+ if the sum of all weights is zero (this
105
- # includes _array_ being empty).
106
- def harmonic_mean(&block)
107
- generalized_mean(-1, &block)
108
- end
109
-
110
- alias_method :harmean, :harmonic_mean
111
-
112
- # call-seq:
113
- # array.geometric_mean => aFloat
114
- # array.geometric_mean { |x| ... } => aFloat
115
- #
116
- # Calculates the {geometric mean}[http://en.wikipedia.org/wiki/Geometric_median]
117
- # of the values in _array_.
118
- #
119
- # An optional block may be passed to provide a weight for each value.
120
- # Defaults to 1. Returns +nil+ if the sum of all weights is zero (this
121
- # includes _array_ being empty).
122
- def geometric_mean
123
- total, weights = 1, 0
124
-
125
- each { |x|
126
- weight = block_given? ? yield(x) : 1
127
-
128
- total *= x ** weight
129
- weights += weight
130
- }
131
-
132
- total ** (1 / weights.to_f) unless weights.zero?
133
- end
134
-
135
- alias_method :geomean, :geometric_mean
136
-
137
- # call-seq:
138
- # array.report_mean => anArray
139
- # array.report_mean(method[, precision]) => anArray
140
- #
141
- # Expects _array_ to be an array of arrays ("rows") of numeric values;
142
- # the first "row" may consist of strings (labels) instead. Returns an
143
- # array of strings with the mean (according to +method+, default #mean)
144
- # of each "column", prepended with the label, if present, and appended
145
- # with the standard deviation, if available; all values are subject to
146
- # +precision+.
147
- #
148
- # If _array_ is a flat array of numeric values, it is treated as a single
149
- # "column".
150
- #
151
- # Returns +nil+ if _array_ is empty.
152
- #
153
- # Examples (with standard deviation):
154
- #
155
- # [[9.4, 34.75], [9.46, 34.68], [9.51, 34.61]].report_mean
156
- # #=> ["9.4567 +/- 0.0450", "34.6800 +/- 0.0572"]
157
- #
158
- # [[9.4, 34.75], [9.46, 34.68], [9.51, 34.61]].report_mean(:harmonic)
159
- # #=> ["9.4565 +/- 0.0450", "34.6799 +/- 0.0572"]
160
- #
161
- # [["a", "b"], [9.4, 34.75], [9.46, 34.68], [9.51, 34.61]].report_mean(nil, 2)
162
- # #=> ["a 9.46 +/- 0.04", "b 34.68 +/- 0.06"]
163
- #
164
- # CSV.read('csv', headers: true, converters: :numeric).to_a.report_mean
165
- # #=> ["a 9.4567 +/- 0.0450", "b 34.6800 +/- 0.0572"]
166
- #
167
- # [9.4, 9.46, 9.51].report_mean
168
- # #=> ["9.4567 +/- 0.0450"]
169
- #
170
- # [34.75, 34.68, 34.61].report_mean
171
- # #=> ["34.6800 +/- 0.0572"]
172
- #
173
- # [].report_mean
174
- # #=> nil
175
- def report_mean(method = nil, precision = 4)
176
- return if empty?
177
-
178
- return clone.replace(self.class.new.push(self).transpose).
179
- report_mean(method, precision) unless first.is_a?(self.class)
180
-
181
- met, sep = [method ||= :mean, 'mean'], ['', '_']
182
- lab, std = first.first.is_a?(::String), respond_to?(:std)
183
-
184
- fmt = ["%-#{precision}s", "%.#{precision}f", "+/- %.#{precision}f"]
185
-
186
- until respond_to?(method) || sep.empty?
187
- method = met.join(sep.shift)
188
- end
189
-
190
- transpose.map! { |x|
191
- i, a = [], []
192
-
193
- i << 0 and a << x.shift if lab
194
- i << 1 and a << x.send(method)
195
- i << 2 and a << x.std if std
196
-
197
- fmt.values_at(*i).join(' ') % a
198
- }
199
- end
200
-
201
- end
202
- end
203
- end