modalsupport 0.5.0 → 0.5.1

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.5.1
@@ -42,5 +42,107 @@ module Enumerable
42
42
  end
43
43
  end
44
44
 
45
+ if_ruby_version(:<, '1.8.7') do
46
+ require 'generator'
47
+
48
+ # Paralell iteration through multiple enumerable objects
49
+ # (1..3).paralell_each([:a,:b,:c],(10..12)).to_a
50
+ # => [[1, :a, 10], [2, :b, 11], [3, :c, 12]]
51
+ def paralell_each(*enums)
52
+ if block_given?
53
+ iterators = ([self]+enums).map{|e| Generator.new(e)}
54
+ loop {
55
+ begin
56
+ yield iterators.map{|e| e.next}
57
+ rescue EOFError
58
+ break
59
+ end
60
+ }
61
+ else
62
+ enum_for(:paralell_each, *enums)
63
+ end
64
+ end
65
+
66
+ # Cross-product iteration through multiple enumerable objects
67
+ # (1..4).cross_each([:a,:b,:c],(11..12)).to_a
68
+ # => [[1, :a, 11], [1, :a, 12], [1, :b, 11], [1, :b, 12], [1, :c, 11], [1, :c, 12],
69
+ # [2, :a, 11], [2, :a, 12], [2, :b, 11], [2, :b, 12], [2, :c, 11], [2, :c, 12],
70
+ # [3, :a, 11], [3, :a, 12], [3, :b, 11], [3, :b, 12], [3, :c, 11], [3, :c, 12],
71
+ # [4, :a, 11], [4, :a, 12], [4, :b, 11], [4, :b, 12], [4, :c, 11], [4, :c, 12]]
72
+ def cross_each(*enumerables)
73
+ if block_given?
74
+ enumerators = ([self]+enumerables).map{|e| Generator.new(e)}
75
+ values = enumerators.map{|e| e.next rescue nil}
76
+ yield values.dup
77
+ i = values.size - 1
78
+ loop do
79
+ if enumerators[i].next?
80
+ values[i] = enumerators[i].next
81
+ yield values.dup # we could leave dupping up to the user, but .to_a would fail
82
+ i = values.size - 1
83
+ else
84
+ enumerators[i].rewind
85
+ values[i] = enumerators[i].next
86
+ i -= 1
87
+ if i<0
88
+ break
89
+ end
90
+ end
91
+ end
92
+ else
93
+ enum_for(:cross_each, *enumerables)
94
+ end
95
+ end
96
+
97
+ end
98
+
99
+ if_ruby_version(:>=, '1.8.7') do
100
+ # Paralell iteration through multiple enumerable objects
101
+ # (1..3).paralell_each([:a,:b,:c],(10..12)).to_a
102
+ # => [[1, :a, 10], [2, :b, 11], [3, :c, 12]]
103
+ def paralell_each(*enums)
104
+ if block_given?
105
+ iterators = ([self]+enums).map{|e| e.to_enum}
106
+ loop {
107
+ yield iterators.map{|e| e.next}
108
+ }
109
+ else
110
+ enum_for(:paralell_each, *enums)
111
+ end
112
+ end
113
+
114
+ # Cross-product iteration through multiple enumerable objects
115
+ # (1..4).cross_each([:a,:b,:c],(11..12)).to_a
116
+ # => [[1, :a, 11], [1, :a, 12], [1, :b, 11], [1, :b, 12], [1, :c, 11], [1, :c, 12],
117
+ # [2, :a, 11], [2, :a, 12], [2, :b, 11], [2, :b, 12], [2, :c, 11], [2, :c, 12],
118
+ # [3, :a, 11], [3, :a, 12], [3, :b, 11], [3, :b, 12], [3, :c, 11], [3, :c, 12],
119
+ # [4, :a, 11], [4, :a, 12], [4, :b, 11], [4, :b, 12], [4, :c, 11], [4, :c, 12]]
120
+ def cross_each(*enumerables)
121
+ if block_given?
122
+ enumerators = ([self]+enumerables).map{|e| e.to_enum}
123
+ values = enumerators.map{|e| e.next rescue nil}
124
+ yield values.dup
125
+ i = values.size - 1
126
+ loop do
127
+ begin
128
+ values[i] = enumerators[i].next
129
+ rescue StopIteration
130
+ enumerators[i].rewind
131
+ values[i] = enumerators[i].next
132
+ i -= 1
133
+ if i<0
134
+ break
135
+ end
136
+ else
137
+ yield values.dup # we could leave dupping up to the user, but .to_a would fail
138
+ i = values.size - 1
139
+ end
140
+ end
141
+ else
142
+ enum_for(:cross_each, *enumerables)
143
+ end
144
+ end
145
+ end
146
+
45
147
 
46
148
  end
data/modalsupport.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{modalsupport}
8
- s.version = "0.5.0"
8
+ s.version = "0.5.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Javier Goizueta"]
@@ -37,9 +37,11 @@ Gem::Specification.new do |s|
37
37
  "modalsupport.gemspec",
38
38
  "test/helper.rb",
39
39
  "test/test_bracket_constructor.rb",
40
+ "test/test_cross_each.rb",
40
41
  "test/test_grep.rb",
41
42
  "test/test_gsub.rb",
42
43
  "test/test_match.rb",
44
+ "test/test_paralell_each.rb",
43
45
  "test/test_product.rb",
44
46
  "test/test_relative_path.rb",
45
47
  "test/test_slice.rb",
@@ -54,9 +56,11 @@ Gem::Specification.new do |s|
54
56
  s.test_files = [
55
57
  "test/helper.rb",
56
58
  "test/test_bracket_constructor.rb",
59
+ "test/test_cross_each.rb",
57
60
  "test/test_grep.rb",
58
61
  "test/test_gsub.rb",
59
62
  "test/test_match.rb",
63
+ "test/test_paralell_each.rb",
60
64
  "test/test_product.rb",
61
65
  "test/test_relative_path.rb",
62
66
  "test/test_slice.rb",
@@ -0,0 +1,17 @@
1
+ require 'helper'
2
+
3
+ class TestCrossEach < Test::Unit::TestCase
4
+
5
+ context "Enumerable objects" do
6
+
7
+ should "have a multiple cross-product iterator" do
8
+ assert_equal [[1, :a, 11], [1, :a, 12], [1, :b, 11], [1, :b, 12], [1, :c, 11], [1, :c, 12],
9
+ [2, :a, 11], [2, :a, 12], [2, :b, 11], [2, :b, 12], [2, :c, 11], [2, :c, 12],
10
+ [3, :a, 11], [3, :a, 12], [3, :b, 11], [3, :b, 12], [3, :c, 11], [3, :c, 12],
11
+ [4, :a, 11], [4, :a, 12], [4, :b, 11], [4, :b, 12], [4, :c, 11], [4, :c, 12]],
12
+ (1..4).cross_each([:a,:b,:c],(11..12)).to_a
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,14 @@
1
+ require 'helper'
2
+
3
+ class TestParalellEach < Test::Unit::TestCase
4
+
5
+ context "Enumerable objects" do
6
+
7
+ should "have a multiple paralell iterator" do
8
+ assert_equal [[1, :a, 10], [2, :b, 11], [3, :c, 12]],
9
+ (1..3).paralell_each([:a,:b,:c],(10..12)).to_a
10
+ end
11
+
12
+ end
13
+
14
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modalsupport
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 0
10
- version: 0.5.0
9
+ - 1
10
+ version: 0.5.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Javier Goizueta
@@ -62,9 +62,11 @@ files:
62
62
  - modalsupport.gemspec
63
63
  - test/helper.rb
64
64
  - test/test_bracket_constructor.rb
65
+ - test/test_cross_each.rb
65
66
  - test/test_grep.rb
66
67
  - test/test_gsub.rb
67
68
  - test/test_match.rb
69
+ - test/test_paralell_each.rb
68
70
  - test/test_product.rb
69
71
  - test/test_relative_path.rb
70
72
  - test/test_slice.rb
@@ -107,9 +109,11 @@ summary: simple extensions to core classes
107
109
  test_files:
108
110
  - test/helper.rb
109
111
  - test/test_bracket_constructor.rb
112
+ - test/test_cross_each.rb
110
113
  - test/test_grep.rb
111
114
  - test/test_gsub.rb
112
115
  - test/test_match.rb
116
+ - test/test_paralell_each.rb
113
117
  - test/test_product.rb
114
118
  - test/test_relative_path.rb
115
119
  - test/test_slice.rb