combinatorics 0.1.0 → 0.2.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.
data/.document CHANGED
@@ -1,11 +1,10 @@
1
1
  # .document is used by rdoc and yard to know how to generate documentation
2
2
  # for example, it can be used to control how rdoc gets built when you do `gem install foo`
3
3
 
4
- README.rdoc
5
4
  lib/**/*.rb
6
- bin/*
7
5
 
8
6
  # Files below this - are treated as 'extra files', and aren't parsed for ruby code
9
7
  -
10
- features/**/*.feature
11
- LICENSE
8
+ LICENSE.md
9
+ README.md
10
+ ChangeLog.md
data/ChangeLog.md CHANGED
@@ -1,3 +1,9 @@
1
+ ### 0.2.0 / 2010-10-03
2
+
3
+ * Added {Range#&}.
4
+ * Added {Range#upto}.
5
+ * Added {Range#downto}.
6
+
1
7
  ### 0.1.0 / 2010-10-02
2
8
 
3
9
  * Initial release:
data/README.md CHANGED
@@ -12,6 +12,7 @@ A collection of modules and methods for performing
12
12
  ## Features
13
13
 
14
14
  * Adds `powerset` to {Array} and {Set}.
15
+ * Added {Range#&}, {Range#upto} and {Range#downto}.
15
16
  * Adds Haskell/Python style list comprehensions via {Array#comprehension}.
16
17
 
17
18
  ## Examples
@@ -50,6 +51,17 @@ Power-set of a {Set}:
50
51
  #<Set: {"ab", "cd"}>,
51
52
  #<Set: {"ab", "cd", "ef"}>]
52
53
 
54
+ Find the intersecting sub-range between two ranges:
55
+
56
+ (1..50) & (20..100)
57
+ # => (20..50)
58
+
59
+ Enumerate over every sub-range between two ranges:
60
+
61
+ (1..5).upto(2..10).to_a
62
+ # => [1..5, 1..6, 1..7, 1..8, 1..9, 1..10,
63
+ 2..5, 2..6, 2..7, 2..8, 2..9, 2..10]
64
+
53
65
  List comprehensions:
54
66
 
55
67
  require 'combinatorics/list_comprehension'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{combinatorics}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.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-02}
12
+ s.date = %q{2010-10-03}
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 = [
@@ -29,6 +29,8 @@ Gem::Specification.new do |s|
29
29
  "VERSION",
30
30
  "combinatorics.gemspec",
31
31
  "lib/combinatorics.rb",
32
+ "lib/combinatorics/extensions.rb",
33
+ "lib/combinatorics/extensions/range.rb",
32
34
  "lib/combinatorics/list_comprehension.rb",
33
35
  "lib/combinatorics/power_set.rb",
34
36
  "lib/combinatorics/power_set/extensions.rb",
@@ -37,6 +39,7 @@ Gem::Specification.new do |s|
37
39
  "lib/combinatorics/power_set/mixin.rb",
38
40
  "spec/.rspec",
39
41
  "spec/combinatorics_spec.rb",
42
+ "spec/extensions/range_spec.rb",
40
43
  "spec/list_comprehension_spec.rb",
41
44
  "spec/power_set/array_spec.rb",
42
45
  "spec/power_set/mixin_examples.rb",
@@ -51,6 +54,7 @@ Gem::Specification.new do |s|
51
54
  s.test_files = [
52
55
  "spec/combinatorics_spec.rb",
53
56
  "spec/list_comprehension_spec.rb",
57
+ "spec/extensions/range_spec.rb",
54
58
  "spec/spec_helper.rb",
55
59
  "spec/power_set/set_spec.rb",
56
60
  "spec/power_set/array_spec.rb",
data/lib/combinatorics.rb CHANGED
@@ -1,2 +1,3 @@
1
+ require 'combinatorics/extensions'
1
2
  require 'combinatorics/list_comprehension'
2
3
  require 'combinatorics/power_set'
@@ -0,0 +1 @@
1
+ require 'combinatorics/extensions/range'
@@ -0,0 +1,97 @@
1
+ class Range
2
+
3
+ #
4
+ # Finds the intersecting sub-range.
5
+ #
6
+ # @param [Range] other
7
+ # The other range.
8
+ #
9
+ # @return [Range]
10
+ # The intersecting sub-range.
11
+ #
12
+ # @example
13
+ # (1..100) & (20..200)
14
+ # # => 20..100
15
+ #
16
+ # @since 0.2.0
17
+ #
18
+ def &(other)
19
+ Range.new(
20
+ [self.first, other.first].max,
21
+ [self.last, other.last].min
22
+ )
23
+ end
24
+
25
+ #
26
+ # Iterates over every sub-range up to the other range.
27
+ #
28
+ # @param [Range] other
29
+ # The upper bounding range.
30
+ #
31
+ # @yield [subrange]
32
+ # The given block will be passed every sub-range between the two ranges.
33
+ #
34
+ # @yieldparam [Range] subrange
35
+ # A sub-range bounded by the beginning of the range and the ending of
36
+ # the other range.
37
+ #
38
+ # @return [Enumerator]
39
+ # If no block is given, an enumerator object will be returned.
40
+ #
41
+ # @example
42
+ # (1..5).upto(2..7).to_a
43
+ # # => [1..5, 1..6, 1..7, 2..5, 2..6, 2..7]
44
+ #
45
+ # @since 0.2.0
46
+ #
47
+ def upto(other)
48
+ return enum_for(:upto,other) unless block_given?
49
+
50
+ unless other.kind_of?(Range)
51
+ raise(TypeError,"bad value for range",caller)
52
+ end
53
+
54
+ self.first.upto(other.first) do |start|
55
+ self.last.upto(other.last) do |stop|
56
+ yield (start..stop)
57
+ end
58
+ end
59
+ end
60
+
61
+ #
62
+ # Iterates over every sub-range down to the other range.
63
+ #
64
+ # @param [Range] other
65
+ # The lower bounding range.
66
+ #
67
+ # @yield [subrange]
68
+ # The given block will be passed every sub-range between the two ranges.
69
+ #
70
+ # @yieldparam [Range] subrange
71
+ # A sub-range bounded by the beginning of the other range and the
72
+ # ending of the range.
73
+ #
74
+ # @return [Enumerator]
75
+ # If no block is given, an enumerator object will be returned.
76
+ #
77
+ # @example
78
+ # (2..7).downto(1..5).to_a
79
+ # # => [2..7, 2..6, 2..5, 1..7, 1..6, 1..5]
80
+ #
81
+ # @since 0.2.0
82
+ #
83
+ def downto(other)
84
+ return enum_for(:downto,other) unless block_given?
85
+
86
+ unless other.kind_of?(Range)
87
+ raise(TypeError,"bad value for range",caller)
88
+ end
89
+
90
+ self.first.downto(other.first) do |start|
91
+ self.last.downto(other.last) do |stop|
92
+ yield (start..stop)
93
+ end
94
+ end
95
+ end
96
+
97
+ end
@@ -4,6 +4,13 @@ module Combinatorics
4
4
  #
5
5
  # Calculates the power-set of an Enumerable object.
6
6
  #
7
+ # @yield [subset]
8
+ # If a block is given, it will be passed each sub-set from the
9
+ # power-set.
10
+ #
11
+ # @yieldparam [Array] subset
12
+ # A sub-set from the power-set.
13
+ #
7
14
  # @return [Array]
8
15
  # The power set.
9
16
  #
@@ -24,9 +31,12 @@ module Combinatorics
24
31
  inject([self.class.new]) do |power_set,element|
25
32
  sub_set = []
26
33
 
27
- power_set.each do |i|
28
- sub_set << i
29
- sub_set << i + [element]
34
+ power_set.each do |previous_set|
35
+ new_set = previous_set + [element]
36
+ yield new_set if block_given?
37
+
38
+ sub_set << previous_set
39
+ sub_set << new_set
30
40
  end
31
41
 
32
42
  sub_set
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+ require 'combinatorics/extensions/range'
3
+
4
+ describe Range do
5
+ describe "&" do
6
+ it "should pick the maximum beginning value" do
7
+ ((100..200) & (150..200)).first.should == 150
8
+ end
9
+
10
+ it "should pick the mimum ending value" do
11
+ ((100..150) & (100..200)).last.should == 150
12
+ end
13
+ end
14
+
15
+ describe "upto" do
16
+ subject { 1..10 }
17
+
18
+ it "should iterate over every beginning value" do
19
+ subject.upto(5..10).to_a.should == [
20
+ (1..10),
21
+ (2..10),
22
+ (3..10),
23
+ (4..10),
24
+ (5..10)
25
+ ]
26
+ end
27
+
28
+ it "should iterate over every ending value" do
29
+ subject.upto(1..15).to_a.should == [
30
+ (1..10),
31
+ (1..11),
32
+ (1..12),
33
+ (1..13),
34
+ (1..14),
35
+ (1..15)
36
+ ]
37
+ end
38
+
39
+ it "should not iterate up to lower bounding ranges" do
40
+ subject.upto(0..5).to_a.should be_empty
41
+ end
42
+ end
43
+
44
+ describe "downto" do
45
+ subject { 5..15 }
46
+
47
+ it "should iterate over every beginning value" do
48
+ subject.downto(1..15).to_a.should == [
49
+ (5..15),
50
+ (4..15),
51
+ (3..15),
52
+ (2..15),
53
+ (1..15)
54
+ ]
55
+ end
56
+
57
+ it "should iterate over every ending value" do
58
+ subject.downto(5..10).to_a.should == [
59
+ (5..15),
60
+ (5..14),
61
+ (5..13),
62
+ (5..12),
63
+ (5..11),
64
+ (5..10)
65
+ ]
66
+ end
67
+
68
+ it "should not iterate down to upward bounding ranges" do
69
+ subject.downto(10..20).to_a.should be_empty
70
+ end
71
+ end
72
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.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-02 00:00:00 -07:00
17
+ date: 2010-10-03 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -84,6 +84,8 @@ files:
84
84
  - VERSION
85
85
  - combinatorics.gemspec
86
86
  - lib/combinatorics.rb
87
+ - lib/combinatorics/extensions.rb
88
+ - lib/combinatorics/extensions/range.rb
87
89
  - lib/combinatorics/list_comprehension.rb
88
90
  - lib/combinatorics/power_set.rb
89
91
  - lib/combinatorics/power_set/extensions.rb
@@ -92,6 +94,7 @@ files:
92
94
  - lib/combinatorics/power_set/mixin.rb
93
95
  - spec/.rspec
94
96
  - spec/combinatorics_spec.rb
97
+ - spec/extensions/range_spec.rb
95
98
  - spec/list_comprehension_spec.rb
96
99
  - spec/power_set/array_spec.rb
97
100
  - spec/power_set/mixin_examples.rb
@@ -132,6 +135,7 @@ summary: Bringing (more) Combinatorics to Ruby
132
135
  test_files:
133
136
  - spec/combinatorics_spec.rb
134
137
  - spec/list_comprehension_spec.rb
138
+ - spec/extensions/range_spec.rb
135
139
  - spec/spec_helper.rb
136
140
  - spec/power_set/set_spec.rb
137
141
  - spec/power_set/array_spec.rb