ruby-nuggets 0.0.4.171 → 0.0.5.179

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.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.0.4
5
+ This documentation refers to ruby-nuggets version 0.0.5
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -30,11 +30,28 @@ class Array
30
30
  # call-seq:
31
31
  # array.shuffle => new_array
32
32
  #
33
- # Shuffles _array_ in random order.
33
+ # Shuffles _array_ in random order. Select a different shuffling algorithm:
34
+ # <tt>Array.send(:alias_method, :shuffle, :shuffle_kfy)</tt>.
34
35
  def shuffle
35
36
  sort_by { Kernel.rand }
36
37
  end
37
38
 
39
+ # call-seq:
40
+ # array.shuffle_knuth => new_array
41
+ #
42
+ # Non-destructive version of #shuffle_knuth!.
43
+ def shuffle_knuth
44
+ dup.shuffle_knuth!
45
+ end
46
+
47
+ # call-seq:
48
+ # array.shuffle_kfy => new_array
49
+ #
50
+ # Non-destructive version of #shuffle_kfy!.
51
+ def shuffle_kfy
52
+ dup.shuffle_kfy!
53
+ end
54
+
38
55
  # call-seq:
39
56
  # array.shuffle! => array
40
57
  #
@@ -43,6 +60,32 @@ class Array
43
60
  replace shuffle
44
61
  end
45
62
 
63
+ # call-seq:
64
+ # array.shuffle_knuth! => array
65
+ #
66
+ # Shuffles _array_ in random order using Knuth's algorithm.
67
+ def shuffle_knuth!
68
+ 0.upto(length - 2) { |i|
69
+ n = i + rand(length - i)
70
+ self[i], self[n] = self[n], self[i]
71
+ }
72
+
73
+ self
74
+ end
75
+
76
+ # call-seq:
77
+ # array.shuffle_kfy! => array
78
+ #
79
+ # Shuffles _array_ in random order using the Knuth-Fisher-Yates algorithm.
80
+ def shuffle_kfy!
81
+ (length - 1).downto(0) { |i|
82
+ n = rand(i + 1)
83
+ self[n], self[i] = self[i], self[n]
84
+ }
85
+
86
+ self
87
+ end
88
+
46
89
  end
47
90
 
48
91
  if $0 == __FILE__
@@ -52,6 +95,39 @@ if $0 == __FILE__
52
95
  p a.shuffle
53
96
  p a.shuffle
54
97
 
98
+ p a.shuffle_knuth
99
+ p a.shuffle_kfy
100
+
55
101
  a.shuffle!
56
102
  p a
103
+
104
+ require File.join(File.dirname(__FILE__), '..', 'integer', 'factorial')
105
+ require File.join(File.dirname(__FILE__), '..', 'enumerable', 'minmax')
106
+
107
+ a = %w[a b c]
108
+ n = 100_000
109
+ m = a.length.f!
110
+ e = n / m.to_f
111
+ puts '%d / %d / %d / %.2f' % [a.length, n, m, e]
112
+
113
+ algorithms = %w[shuffle shuffle_knuth shuffle_kfy]
114
+ max = algorithms.max(:length)
115
+
116
+ algorithms.each { |algorithm|
117
+ score = Hash.new { |h, k| h[k] = 0 }
118
+
119
+ n.times {
120
+ score[a.send(algorithm)] += 1
121
+ }
122
+
123
+ x2 = 0
124
+ score.sort.each { |key, value|
125
+ x = value - e
126
+ y = x**2 / e
127
+ #puts '%s: %d (% .2f/%.2f)' % [key, value, x, y]
128
+
129
+ x2 += y
130
+ }
131
+ puts "%-#{max}s = %.2f (%.2f)" % [algorithm, x2, x2 / m]
132
+ }
57
133
  end
@@ -4,7 +4,7 @@
4
4
  # A component of ruby-nuggets, some extensions to the Ruby programming #
5
5
  # language. #
6
6
  # #
7
- # Copyright (C) 2007 Jens Wille #
7
+ # Copyright (C) 2007-2008 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Jens Wille <jens.wille@uni-koeln.de> #
@@ -32,25 +32,63 @@ class Array
32
32
  # call-seq:
33
33
  # array.to_h => aHash
34
34
  # array.to_h(value) => aHash
35
+ # array.to_h { |element| ... } => aHash
35
36
  #
36
- # If no +value+ is given, converts _array_, being an array of two-element
37
- # arrays, into a hash, preserving sub-arrays. Otherwise, maps each element
38
- # of _array_ to +value+.
37
+ # If neither +value+ nor block is given, converts _array_, taken as an
38
+ # array of key/value pairs, into a hash, preserving sub-arrays (Thus:
39
+ # <tt>hash.to_a.to_h == hash</tt>). Otherwise, maps each element of
40
+ # _array_ to +value+ or the result of the block.
41
+ #
42
+ # NOTE: This is the "nice" version. For a more speed-optimized one,
43
+ # use #to_hash_opt.
39
44
  #
40
45
  # Examples:
41
- # %w[a b c d].to_h # => { "a" => "b", "c" => "d" }
42
- # %w[a b c d].to_h(1) # => { "a" => 1, "b" => 1, "c" => 1, "d" => 1 }
43
- def to_hash(value = default = Object.new)
44
- if value == default
45
- Hash[*flatten_once]
46
- else
46
+ # [[0, 0], [1, [2, 3]]].to_h #=> { 0 => 0, 1 => [2, 3] }
47
+ # %w[a b c d].to_h #=> { "a" => "b", "c" => "d" }
48
+ # %w[a b c d].to_h(1) #=> { "a" => 1, "b" => 1, "c" => 1, "d" => 1 }
49
+ # %w[a b].to_h { |e| e * 2 } #=> { "a" => "aa", "b" => "bb" }
50
+ def to_hash(value = default = Object.new, &block)
51
+ if block ||= value != default && lambda { value }
47
52
  inject({}) { |hash, element|
48
- hash.update(element => value)
53
+ hash.update(element => block[element])
49
54
  }
55
+ else
56
+ Hash[*flatten_once]
50
57
  end
51
58
  end
52
59
  alias_method :to_h, :to_hash
53
60
 
61
+ # call-seq:
62
+ # array.to_h => aHash
63
+ # array.to_h(value) => aHash
64
+ # array.to_h { |element| ... } => aHash
65
+ #
66
+ # Same as #to_hash, but slightly optimized for speed. To use this one instead
67
+ # of #to_hash: <tt>Array.send(:alias_method, :to_h, :to_hash_opt)</tt>.
68
+ #
69
+ # Benchmark (array = (1..20).to_a, N = 100_000):
70
+ # user system total real
71
+ # to_hash: plain 4.820000 0.560000 5.380000 ( 5.600390)
72
+ # to_hash: value 12.910000 0.930000 13.840000 ( 13.938352)
73
+ # to_hash: block 13.590000 1.180000 14.770000 ( 14.810804)
74
+ # to_hash_opt: plain 4.910000 0.470000 5.380000 ( 5.416949)
75
+ # to_hash_opt: value 2.170000 0.390000 2.560000 ( 2.609034)
76
+ # to_hash_opt: block 7.090000 0.880000 7.970000 ( 8.109180)
77
+ def to_hash_opt(value = default = Object.new, &block)
78
+ if block
79
+ hash = {}
80
+ each { |element| hash[element] = block[element] }
81
+ hash
82
+ elsif value != default
83
+ hash = {}
84
+ each { |element| hash[element] = value }
85
+ hash
86
+ else
87
+ Hash[*flatten_once]
88
+ end
89
+ end
90
+ #alias_method :to_h, :to_hash_opt
91
+
54
92
  end
55
93
 
56
94
  if $0 == __FILE__
@@ -64,5 +102,13 @@ if $0 == __FILE__
64
102
 
65
103
  c = %w[a b c d]
66
104
  p c
105
+ p c.to_h
67
106
  p c.to_h(1)
107
+ p c.to_h { nil }
108
+
109
+ h = { :a => 1, :b => [2, 3], :c => { :d => 4}}
110
+ p h
111
+ p h.to_a
112
+ p h.to_a.to_h
113
+ p h.to_a.to_h == h
68
114
  end
@@ -0,0 +1,47 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2008 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@uni-koeln.de> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU General Public License as published by the Free #
14
+ # Software Foundation; either version 3 of the License, or (at your option) #
15
+ # 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 General Public License for #
20
+ # more details. #
21
+ # #
22
+ # You should have received a copy of the GNU General Public License along #
23
+ # with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
28
+ class Integer
29
+
30
+ # call-seq:
31
+ # int.factorial => anInteger
32
+ #
33
+ # Calculate the factorial of +int+.
34
+ def factorial
35
+ (1..self).inject { |f, i| f * i }
36
+ end
37
+
38
+ alias_method :fac, :factorial
39
+ alias_method :f!, :factorial
40
+
41
+ end
42
+
43
+ if $0 == __FILE__
44
+ 1.upto(8) { |i|
45
+ puts "#{i}: #{i.factorial}"
46
+ }
47
+ end
@@ -72,7 +72,7 @@ class Array
72
72
  # call-seq:
73
73
  # array.vain? => true or false
74
74
  #
75
- # Returns true if any of _array_'s elements are themselves vain.
75
+ # Returns true if all of _array_'s elements are themselves vain.
76
76
  def vain?
77
77
  blank? { |a| a.delete_if { |i| i.vain? } }
78
78
  end
@@ -84,7 +84,7 @@ class Hash
84
84
  # call-seq:
85
85
  # hash.vain? => true or false
86
86
  #
87
- # Returns true if any of _hash_'s values are themselves vain.
87
+ # Returns true if all of _hash_'s values are themselves vain.
88
88
  def vain?
89
89
  blank? { |h| h.delete_if { |k, v| v.vain? } }
90
90
  end
@@ -29,7 +29,7 @@ require File.join(File.dirname(__FILE__), '..', 'object', 'singleton_class')
29
29
 
30
30
  class Proc
31
31
 
32
- # call.seq:
32
+ # call-seq:
33
33
  # proc.bind(object) => aMethod
34
34
  #
35
35
  # Straight from Rails' ActiveSupport -- effectively binds _proc_ to +object+.
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 0
7
- TINY = 4
7
+ TINY = 5
8
8
 
9
9
  class << self
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4.171
4
+ version: 0.0.5.179
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-01-10 00:00:00 +01:00
12
+ date: 2008-01-18 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,6 +24,7 @@ extra_rdoc_files:
24
24
  - COPYING
25
25
  - ChangeLog
26
26
  files:
27
+ - lib/nuggets/integer/factorial.rb
27
28
  - lib/nuggets/version.rb
28
29
  - lib/nuggets/object/singleton_class.rb
29
30
  - lib/nuggets/object/blank.rb
@@ -55,15 +56,15 @@ has_rdoc: true
55
56
  homepage: http://prometheus.rubyforge.org/ruby-nuggets
56
57
  post_install_message:
57
58
  rdoc_options:
58
- - --charset
59
- - UTF-8
60
59
  - --all
61
60
  - --title
62
61
  - ruby-nuggets Application documentation
62
+ - --line-numbers
63
63
  - --main
64
64
  - README
65
- - --line-numbers
66
65
  - --inline-source
66
+ - --charset
67
+ - UTF-8
67
68
  require_paths:
68
69
  - lib
69
70
  required_ruby_version: !ruby/object:Gem::Requirement