ruby-nuggets 0.9.9 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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/median_mixin'
2
-
3
- class Array
4
- include Nuggets::Array::MedianMixin
5
- end
@@ -1,74 +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 MedianMixin
31
-
32
- # call-seq:
33
- # array.median([prefer]) => anObject
34
- # array.median { |left, right| ... } => anObject
35
- #
36
- # Determines the median[http://en.wikipedia.org/wiki/Median] of the values
37
- # in _array_. _array_ must be sortable.
38
- #
39
- # If _array_ contains an even number of values, a block may be passed to
40
- # decide what the "middle" (average) should be. For Numeric values, the
41
- # block is optional and the arithmetic mean will be used when no block
42
- # is passed; for other values, the block is mandatory.
43
- #
44
- # Alternatively, +prefer+ may either be set to +true+, 1, or <tt>:left</tt>
45
- # to use the left "middle", or to +false+, 2, or <tt>:right</tt> to use the
46
- # right "middle". The block will then be ignored.
47
- def median(prefer = nil)
48
- return if empty?
49
-
50
- sorted, index = sort, (size / 2.0).ceil - 1
51
-
52
- case prefer
53
- when true, 1, :left then prefer_left = true
54
- when false, 2, :right then prefer_right = true
55
- end
56
-
57
- middle1 = sorted[index]
58
- return middle1 if prefer_left || size.odd?
59
-
60
- middle2 = sorted[index + 1]
61
- return middle2 if prefer_right
62
-
63
- unless block_given?
64
- # simple arithmetic mean
65
- (middle1 + middle2) / 2.0
66
- else
67
- # make your own average
68
- yield middle1, middle2
69
- end
70
- end
71
-
72
- end
73
- end
74
- end
@@ -1,5 +0,0 @@
1
- require 'nuggets/array/mode_mixin'
2
-
3
- class Array
4
- include Nuggets::Array::ModeMixin
5
- end
@@ -1,70 +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
- require 'nuggets/array/histogram_mixin'
29
-
30
- module Nuggets
31
- class Array
32
- module ModeMixin
33
-
34
- def self.included(base)
35
- base.send :include, Nuggets::Array::HistogramMixin
36
- end
37
-
38
- # call-seq:
39
- # array.mode => anObject
40
- # array.mode(+true+) => anArray
41
- #
42
- # Returns the mode[http://en.wikipedia.org/wiki/Mode_%28statistics%29] of
43
- # the values in _array_ (via #histogram).
44
- #
45
- # If parameter +true+ is passed, an Array of all modes is returned.
46
- def mode(all = false, &block)
47
- hist, modes = histogram(&block), []
48
- freq = hist.values.max
49
-
50
- hist.each { |key, value|
51
- if value == freq
52
- modes << key
53
- break unless all
54
- end
55
- }
56
-
57
- all ? modes : modes.first
58
- end
59
-
60
- # call-seq:
61
- # array.modes => anArray
62
- #
63
- # Returns an Array of all modes of the values in _array_ (see #mode).
64
- def modes(&block)
65
- mode(true, &block)
66
- end
67
-
68
- end
69
- end
70
- end
@@ -1,84 +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.monotone?(operator) => +true+ or +false+
32
- #
33
- # Check whether _array_ is monotone according to +operator+.
34
- def monotone?(operator = nil)
35
- if [nil, true, false].include?(operator)
36
- ascending?(operator) || descending?(operator)
37
- else
38
- inject { |a, b|
39
- return false unless a.send(operator, b)
40
- b
41
- }
42
-
43
- true
44
- end
45
- end
46
- alias_method :monotonic?, :monotone?
47
-
48
- # call-seq:
49
- # array.ascending? => +true+ or +false+
50
- #
51
- # Check whether _array_ is (strictly) ascending.
52
- def ascending?(strict = false)
53
- monotone?(strict ? :< : :<=)
54
- end
55
- alias_method :increasing?, :ascending?
56
-
57
- # call-seq:
58
- # array.strictly_ascending? => +true+ or +false+
59
- #
60
- # Check whether _array_ is strictly ascending.
61
- def strictly_ascending?
62
- ascending?(true)
63
- end
64
- alias_method :strictly_increasing?, :strictly_ascending?
65
-
66
- # call-seq:
67
- # array.descending? => +true+ or +false+
68
- #
69
- # Check whether _array_ is (strictly) descending.
70
- def descending?(strict = false)
71
- monotone?(strict ? :> : :>=)
72
- end
73
- alias_method :decreasing?, :descending?
74
-
75
- # call-seq:
76
- # array.strictly_descending? => +true+ or +false+
77
- #
78
- # Check whether _array_ is strictly descending.
79
- def strictly_descending?
80
- descending?(true)
81
- end
82
- alias_method :strictly_decreasing?, :strictly_descending?
83
-
84
- end
@@ -1,42 +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.only => anObject
32
- # array.only(+true+) => anObject
33
- #
34
- # Returns the only element of _array_. Raises an IndexError if _array_'s
35
- # size is not 1, unless parameter +true+ is passed.
36
- #
37
- # Idea stolen from Gavin Sinclair's Ruby Extensions Project.
38
- def only(relax = size == 1)
39
- relax ? first : raise(::IndexError, 'not a single-element array')
40
- end
41
-
42
- end
@@ -1,45 +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.rand => anItem
32
- #
33
- # Randomly pick an item from _array_.
34
- def rand
35
- at(::Kernel.rand(size))
36
- end
37
-
38
- end
39
-
40
- if $0 == __FILE__
41
- a = %w[a b c d]
42
- p a
43
- p a.rand
44
- p a.rand
45
- end
@@ -1,5 +0,0 @@
1
- require 'nuggets/array/regression_mixin'
2
-
3
- class Array
4
- include Nuggets::Array::RegressionMixin
5
- end
@@ -1,150 +0,0 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2012 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 RegressionMixin
31
-
32
- # call-seq:
33
- # array.linear_least_squares => anArray
34
- #
35
- # Calculates the {linear least squares regression}[http://en.wikipedia.org/wiki/Simple_linear_regression]
36
- # for the <tt>{x,y}</tt> pairs in _array_. If _array_ only contains
37
- # values instead of pairs, +y+ will be the value and +x+ will be each
38
- # value's position (rank) in _array_.
39
- def linear_least_squares
40
- return [] if empty?
41
-
42
- sx, sy, sq, sp, xys = 0.0, 0.0, 0.0, 0.0, first.respond_to?(:to_ary) ?
43
- self : self.class.new(size) { |i| [i + 1, at(i)] }
44
-
45
- xys.each { |x, y| sx += x; sy += y; sq += x ** 2; sp += x * y }
46
-
47
- b = (v = sq * size - sx ** 2) == 0 ? 0 : (sp * size - sx * sy) / v
48
- a = (sy - b * sx) / size
49
-
50
- xys.map { |x, _| [x, a + b * x] }
51
- end
52
-
53
- alias_method :llsq, :linear_least_squares
54
-
55
- # call-seq:
56
- # array.linear_least_squares_incremental => anIncrementalLinearRegression
57
- #
58
- # Returns an instance of IncrementalLinearRegression for _array_; _array_
59
- # being a list of values (in contrast to #linear_least_squares, which also
60
- # accepts <tt>{x,y}</tt> pairs). Use IncrementalLinearRegression directly,
61
- # or apply this method to an empty _array_, for more control over its input
62
- # data.
63
- def linear_least_squares_incremental
64
- IncrementalLinearRegression.new(*self)
65
- end
66
-
67
- alias_method :llsqi, :linear_least_squares_incremental
68
-
69
- # Inspired by {Incremental Simple Linear Regression in Ruby}[http://blog.codewren.ch/post/31378435699].
70
- #
71
- # Use #push to add a single <tt>{x,y}</tt> pair, #add to add a list of +y+
72
- # values, and #<< to add a single +y+ value. Whenever a single +y+ value is
73
- # added, it's associated with an +x+ value of its position (rank) in the
74
- # data series.
75
- #
76
- # Call #to_a (or any Enumerable method) to work with the regression points.
77
- class IncrementalLinearRegression
78
-
79
- include ::Enumerable
80
-
81
- def initialize(*ys)
82
- clear
83
- add(*ys)
84
- end
85
-
86
- def clear
87
- @x = @y = @xx = @xy = 0.0
88
- @cnt, @slope = 0, nil
89
- self
90
- end
91
-
92
- def push(x, y)
93
- cnt, @slope = @cnt += 1, nil
94
-
95
- @x += (x - @x) / cnt
96
- @y += (y - @y) / cnt
97
- @xx += (x * x - @xx) / cnt
98
- @xy += (x * y - @xy) / cnt
99
-
100
- self
101
- end
102
-
103
- def add(*ys)
104
- ys.each { |y| self << y }
105
- self
106
- end
107
-
108
- def <<(y)
109
- push(@cnt + 1, y)
110
- end
111
-
112
- def slope
113
- @slope ||= @cnt < 2 ? 0 : (@xy - @x * @y) / (@xx - @x * @x)
114
- end
115
-
116
- def intercept
117
- at(0)
118
- end
119
-
120
- def at(x)
121
- @y + slope * (x - @x)
122
- end
123
-
124
- alias_method :[], :at
125
-
126
- def each
127
- @cnt.times { |i| yield [x = i + 1, at(x)] }
128
- self
129
- end
130
-
131
- def to_a(range = nil)
132
- range ? range.map { |x| [x, at(x)] } : super()
133
- end
134
-
135
- def to_s
136
- s, i = slope, intercept
137
-
138
- y = s == 0 ? i : begin
139
- x = s.abs == 1 ? "#{'-' if s < 0}x" : "#{s} * x"
140
- i == 0 ? x : "#{x} #{i < 0 ? '-' : '+'} #{i.abs}"
141
- end
142
-
143
- "y := #{y}".gsub(/\.0\b/, '')
144
- end
145
-
146
- end
147
-
148
- end
149
- end
150
- end