combinatorics 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document CHANGED
@@ -1,10 +1,4 @@
1
- # .document is used by rdoc and yard to know how to generate documentation
2
- # for example, it can be used to control how rdoc gets built when you do `gem install foo`
3
-
4
1
  lib/**/*.rb
5
-
6
- # Files below this - are treated as 'extra files', and aren't parsed for ruby code
7
2
  -
8
- LICENSE.md
9
- README.md
3
+ LICENSE.txt
10
4
  ChangeLog.md
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
data/.yardopts CHANGED
@@ -1 +1 @@
1
- --markup markdown --title 'Combinatorics Documentation' --protected --files ChangeLog.md,LICENSE.txt
1
+ --markup markdown --title 'Combinatorics Documentation' --protected
@@ -1,3 +1,25 @@
1
+ ### 0.3.0 / 2010-10-17
2
+
3
+ * Added {Combinatorics::Generator} which auto-detects the `Generator`
4
+ class.
5
+ * Added {Combinatorics::Enumerator} which auto-detects the `Enumerator`
6
+ class.
7
+ * Better detection of when enumerable values passed to
8
+ {Array#comprehension} reach their last value. This allows using
9
+ `Generator` objects that yield different values for each cycle:
10
+
11
+ multiplier = 0
12
+ gen = Generator.new { |g|
13
+ multiplier += 1
14
+ 5.times { |i| g.yield (i * multiplier) }
15
+ }
16
+
17
+ [[1,2], gen].comprehension.to_a
18
+ # => [[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,2],[2,4],[2,6],[2,8]]
19
+
20
+ * Added a benchmark for {Array#comprehension} (see the
21
+ [benchmarks](http://github.com/postmodern/combinatorics/tree/master/benchmarks) directory).
22
+
1
23
  ### 0.2.0 / 2010-10-03
2
24
 
3
25
  * Added {Range#&}.
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ begin
12
12
  gem.email = 'postmodern.mod3@gmail.com'
13
13
  gem.homepage = 'http://github.com/postmodern/combinatorics'
14
14
  gem.authors = ['Postmodern']
15
- gem.add_development_dependency 'rspec', '~> 1.3.0'
15
+ gem.add_development_dependency 'rspec', '~> 2.0.0'
16
16
  gem.add_development_dependency 'yard', '~> 0.6.0'
17
17
  gem.add_development_dependency 'jeweler', '~> 1.4.0'
18
18
  end
@@ -21,14 +21,8 @@ rescue LoadError
21
21
  puts 'Jeweler (or a dependency) not available. Install it with: gem install jeweler'
22
22
  end
23
23
 
24
- require 'spec/rake/spectask'
25
- Spec::Rake::SpecTask.new(:spec) do |spec|
26
- spec.libs += %w[lib spec]
27
- spec.spec_files = FileList['spec/**/*_spec.rb']
28
- spec.spec_opts = %w[--options .specopts]
29
- end
30
-
31
- task :spec => :check_dependencies
24
+ require 'rspec/core/rake_task'
25
+ RSpec::Core::RakeTask.new
32
26
  task :default => :spec
33
27
 
34
28
  begin
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ root_dir = File.expand_path(File.join(File.dirname(__FILE__),'..'))
4
+ lib_dir = File.join(root_dir,'lib')
5
+ $LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir)
6
+
7
+ require 'benchmark'
8
+ require 'combinatorics/list_comprehension'
9
+
10
+ list = [(1..200)] * 3
11
+
12
+ puts Benchmark.measure { list.comprehension.each { |list| } }
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{combinatorics}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Postmodern"]
12
- s.date = %q{2010-10-03}
12
+ s.date = %q{2010-10-17}
13
13
  s.description = %q{A collection of modules and methods for performing Combinatoric calculations.}
14
14
  s.email = %q{postmodern.mod3@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -20,17 +20,20 @@ Gem::Specification.new do |s|
20
20
  s.files = [
21
21
  ".document",
22
22
  ".gitignore",
23
- ".specopts",
23
+ ".rspec",
24
24
  ".yardopts",
25
25
  "ChangeLog.md",
26
26
  "LICENSE.txt",
27
27
  "README.md",
28
28
  "Rakefile",
29
29
  "VERSION",
30
+ "benchmarks/list_comprehension.rb",
30
31
  "combinatorics.gemspec",
31
32
  "lib/combinatorics.rb",
33
+ "lib/combinatorics/enumerator.rb",
32
34
  "lib/combinatorics/extensions.rb",
33
35
  "lib/combinatorics/extensions/range.rb",
36
+ "lib/combinatorics/generator.rb",
34
37
  "lib/combinatorics/list_comprehension.rb",
35
38
  "lib/combinatorics/power_set.rb",
36
39
  "lib/combinatorics/power_set/extensions.rb",
@@ -39,7 +42,9 @@ Gem::Specification.new do |s|
39
42
  "lib/combinatorics/power_set/mixin.rb",
40
43
  "spec/.rspec",
41
44
  "spec/combinatorics_spec.rb",
45
+ "spec/enumerator_spec.rb",
42
46
  "spec/extensions/range_spec.rb",
47
+ "spec/generator_spec.rb",
43
48
  "spec/list_comprehension_spec.rb",
44
49
  "spec/power_set/array_spec.rb",
45
50
  "spec/power_set/mixin_examples.rb",
@@ -52,8 +57,10 @@ Gem::Specification.new do |s|
52
57
  s.rubygems_version = %q{1.3.7}
53
58
  s.summary = %q{Bringing (more) Combinatorics to Ruby}
54
59
  s.test_files = [
55
- "spec/combinatorics_spec.rb",
60
+ "spec/enumerator_spec.rb",
61
+ "spec/combinatorics_spec.rb",
56
62
  "spec/list_comprehension_spec.rb",
63
+ "spec/generator_spec.rb",
57
64
  "spec/extensions/range_spec.rb",
58
65
  "spec/spec_helper.rb",
59
66
  "spec/power_set/set_spec.rb",
@@ -66,16 +73,16 @@ Gem::Specification.new do |s|
66
73
  s.specification_version = 3
67
74
 
68
75
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
69
- s.add_development_dependency(%q<rspec>, ["~> 1.3.0"])
76
+ s.add_development_dependency(%q<rspec>, ["~> 2.0.0"])
70
77
  s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
71
78
  s.add_development_dependency(%q<jeweler>, ["~> 1.4.0"])
72
79
  else
73
- s.add_dependency(%q<rspec>, ["~> 1.3.0"])
80
+ s.add_dependency(%q<rspec>, ["~> 2.0.0"])
74
81
  s.add_dependency(%q<yard>, ["~> 0.6.0"])
75
82
  s.add_dependency(%q<jeweler>, ["~> 1.4.0"])
76
83
  end
77
84
  else
78
- s.add_dependency(%q<rspec>, ["~> 1.3.0"])
85
+ s.add_dependency(%q<rspec>, ["~> 2.0.0"])
79
86
  s.add_dependency(%q<yard>, ["~> 0.6.0"])
80
87
  s.add_dependency(%q<jeweler>, ["~> 1.4.0"])
81
88
  end
@@ -0,0 +1,10 @@
1
+ require 'enumerator'
2
+
3
+ module Combinatorics
4
+ # auto-detects the `Enumerator` class.
5
+ Enumerator = if defined?(::Enumerator) # 1.9
6
+ ::Enumerator
7
+ elsif defined?(::Enumerable::Enumerator) # 1.8.7
8
+ ::Enumerable::Enumerator
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'generator'
2
+
3
+ module Combinatorics
4
+ # auto-detects the `Generator` class.
5
+ Generator = if defined?(::Enumerator::Generator) # 1.9
6
+ ::Enumerator::Generator
7
+ elsif defined?(::Generator) # 1.8.7
8
+ ::Generator
9
+ end
10
+ end
@@ -49,22 +49,25 @@ class Array
49
49
 
50
50
  enums = self.map do |value|
51
51
  if value.kind_of?(Enumerable)
52
- value
52
+ value.enum_for
53
53
  else
54
- (value..value)
54
+ [value].enum_for
55
55
  end
56
56
  end
57
57
 
58
- cycles = enums.map { |e| e.cycle }
59
- start = cycles.map { |e| e.next }
60
- iteration = start.dup
58
+ iteration = enums.map { |e| e.next }
61
59
 
62
60
  loop do
63
61
  yield iteration.dup
64
62
 
65
- (cycles.length - 1).downto(0) do |index|
66
- iteration[index] = cycles[index].next
67
- break unless iteration[index] == start[index]
63
+ (enums.length - 1).downto(0) do |index|
64
+ begin
65
+ iteration[index] = enums[index].next
66
+ break
67
+ rescue StopIteration
68
+ enums[index].rewind
69
+ iteration[index] = enums[index].next
70
+ end
68
71
 
69
72
  return nil if index == 0
70
73
  end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ require 'combinatorics/enumerator'
3
+
4
+ describe Combinatorics::Enumerator do
5
+ it "should auto-detect the Enumerator class" do
6
+ Combinatorics::Enumerator.should_not be_nil
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ require 'combinatorics/generator'
3
+
4
+ describe Combinatorics::Generator do
5
+ it "should auto-detect the Generator class" do
6
+ Combinatorics::Generator.should_not be_nil
7
+ end
8
+ end
@@ -1,5 +1,6 @@
1
1
  require 'spec_helper'
2
2
  require 'combinatorics/list_comprehension'
3
+ require 'combinatorics/generator'
3
4
 
4
5
  describe "Array#comprehension" do
5
6
  it "should return an Enumerator object if no block is given" do
@@ -28,10 +29,39 @@ describe "Array#comprehension" do
28
29
  end
29
30
 
30
31
  it "should iterate over the values within an enumerable value" do
31
- range = (1..10)
32
+ range = (1..5)
33
+ a = [range]
34
+
35
+ a.comprehension.to_a.should == [[1],[2],[3],[4],[5]]
36
+ end
37
+
38
+ it "should iterate over repeating values" do
39
+ range = [1,2,3,1,2,4]
32
40
  a = [range]
33
41
 
34
- a.comprehension.to_a.should == [[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]]
42
+ a.comprehension.to_a.should == [[1],[2],[3],[1],[2],[4]]
43
+ end
44
+
45
+ it "should iterate over values from a generator" do
46
+ a = [Combinatorics::Generator.new { |g| 5.times { |i| g.yield i } }]
47
+
48
+ a.comprehension.to_a.should == [[0],[1],[2],[3],[4]]
49
+ end
50
+
51
+ it "should iterate over values from a non-repeating generator" do
52
+ multiplier = 0
53
+ a = [
54
+ [1,2],
55
+ Combinatorics::Generator.new { |g|
56
+ multiplier += 1
57
+ 5.times { |i| g.yield (i * multiplier) }
58
+ }
59
+ ]
60
+
61
+ a.comprehension.to_a.should == [
62
+ [1,0],[1,1],[1,2],[1,3],[1,4],
63
+ [2,0],[2,2],[2,4],[2,6],[2,8]
64
+ ]
35
65
  end
36
66
 
37
67
  it "should ignore non-enumerable values" do
@@ -1,4 +1,4 @@
1
1
  require 'rubygems'
2
2
 
3
- gem 'rspec', '>= 1.3.0'
4
- require 'spec'
3
+ gem 'rspec', '~> 2.0.0'
4
+ require 'rspec'
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Postmodern
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-03 00:00:00 -07:00
17
+ date: 2010-10-17 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -26,10 +26,10 @@ dependencies:
26
26
  - - ~>
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
- - 1
30
- - 3
29
+ - 2
30
+ - 0
31
31
  - 0
32
- version: 1.3.0
32
+ version: 2.0.0
33
33
  type: :development
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
@@ -75,17 +75,20 @@ extra_rdoc_files:
75
75
  files:
76
76
  - .document
77
77
  - .gitignore
78
- - .specopts
78
+ - .rspec
79
79
  - .yardopts
80
80
  - ChangeLog.md
81
81
  - LICENSE.txt
82
82
  - README.md
83
83
  - Rakefile
84
84
  - VERSION
85
+ - benchmarks/list_comprehension.rb
85
86
  - combinatorics.gemspec
86
87
  - lib/combinatorics.rb
88
+ - lib/combinatorics/enumerator.rb
87
89
  - lib/combinatorics/extensions.rb
88
90
  - lib/combinatorics/extensions/range.rb
91
+ - lib/combinatorics/generator.rb
89
92
  - lib/combinatorics/list_comprehension.rb
90
93
  - lib/combinatorics/power_set.rb
91
94
  - lib/combinatorics/power_set/extensions.rb
@@ -94,7 +97,9 @@ files:
94
97
  - lib/combinatorics/power_set/mixin.rb
95
98
  - spec/.rspec
96
99
  - spec/combinatorics_spec.rb
100
+ - spec/enumerator_spec.rb
97
101
  - spec/extensions/range_spec.rb
102
+ - spec/generator_spec.rb
98
103
  - spec/list_comprehension_spec.rb
99
104
  - spec/power_set/array_spec.rb
100
105
  - spec/power_set/mixin_examples.rb
@@ -133,8 +138,10 @@ signing_key:
133
138
  specification_version: 3
134
139
  summary: Bringing (more) Combinatorics to Ruby
135
140
  test_files:
141
+ - spec/enumerator_spec.rb
136
142
  - spec/combinatorics_spec.rb
137
143
  - spec/list_comprehension_spec.rb
144
+ - spec/generator_spec.rb
138
145
  - spec/extensions/range_spec.rb
139
146
  - spec/spec_helper.rb
140
147
  - spec/power_set/set_spec.rb
data/.specopts DELETED
@@ -1 +0,0 @@
1
- --colour --format specdoc