modalsupport 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.5.2
@@ -42,107 +42,70 @@ 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
45
+ # Paralell iteration through multiple enumerable objects
46
+ # (1..3).paralell_each([:a,:b,:c],(10..12)).to_a
47
+ # => [[1, :a, 10], [2, :b, 11], [3, :c, 12]]
48
+ #
49
+ # e.paralell_each(*enums) = e.zip(*enums).each
50
+ def paralell_each(*enums, &blk)
51
+ # Note that to implement a lazy iterator we'd need
52
+ # external iterators which don't work in Rubinius, are
53
+ # implemented differently in Ruby < 1.8.7 and are slow.
54
+ # So, for the time being we generate an array and then
55
+ # iterate it.
56
+ if block_given?
57
+ zip(*enums).each(&blk)
58
+ else
59
+ enum_for(:paralell_each, *enums)
64
60
  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
61
+ end
62
+
63
+ # Equivalent to paralell_each(*enums).to_a, but more efficient
64
+ def paralell_array(*enums)
65
+ zip(*enums)
66
+ end
67
+
68
+ # Cross-product iteration through multiple enumerable objects
69
+ # (1..4).cross_each([:a,:b,:c],(11..12)).to_a
70
+ # => [[1, :a, 11], [1, :a, 12], [1, :b, 11], [1, :b, 12], [1, :c, 11], [1, :c, 12],
71
+ # [2, :a, 11], [2, :a, 12], [2, :b, 11], [2, :b, 12], [2, :c, 11], [2, :c, 12],
72
+ # [3, :a, 11], [3, :a, 12], [3, :b, 11], [3, :b, 12], [3, :c, 11], [3, :c, 12],
73
+ # [4, :a, 11], [4, :a, 12], [4, :b, 11], [4, :b, 12], [4, :c, 11], [4, :c, 12]]
74
+ #
75
+ # e.cross_each(*enums) = e.to_a.product(*enums.map{|enum| enum.to_a}).each
76
+ def cross_each(*enumerables)
77
+ if block_given?
78
+ enumerable, *rest = enumerables
79
+ self.each do |e|
80
+ if enumerables.empty?
81
+ yield [e]
82
+ else
83
+ enumerable.cross_each(*rest) do |rest_e|
84
+ yield [e]+rest_e
90
85
  end
91
86
  end
92
- else
93
- enum_for(:cross_each, *enumerables)
94
87
  end
88
+ else
89
+ enum_for(:cross_each, *enumerables)
95
90
  end
96
-
97
91
  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
92
+
93
+ # equivalent to cross_each(*enumerables).to_a, but more efficient
94
+ def cross_array(*enumerables)
95
+ # return to_a.product(*enumerables.map{|e| e.to_a})
96
+ enumerables.unshift self
97
+ result = [[]]
98
+ while !enumerables.empty?
99
+ t, result = result, []
100
+ b, *enumerables = enumerables
101
+ t.each do |a|
102
+ b.each do |n|
103
+ result << a + [n]
140
104
  end
141
- else
142
- enum_for(:cross_each, *enumerables)
143
105
  end
144
106
  end
107
+ result
145
108
  end
146
-
109
+
147
110
 
148
111
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{modalsupport}
8
- s.version = "0.5.1"
8
+ s.version = "0.5.2"
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"]
12
- s.date = %q{2010-06-09}
12
+ s.date = %q{2010-06-10}
13
13
  s.description = %q{additional support extensions to ActiveSupport and HoboSupport}
14
14
  s.email = %q{jgoizueta@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -51,7 +51,7 @@ Gem::Specification.new do |s|
51
51
  s.homepage = %q{http://github.com/jgoizueta/modalsupport}
52
52
  s.rdoc_options = ["--charset=UTF-8"]
53
53
  s.require_paths = ["lib"]
54
- s.rubygems_version = %q{1.3.7}
54
+ s.rubygems_version = %q{1.3.6}
55
55
  s.summary = %q{simple extensions to core classes}
56
56
  s.test_files = [
57
57
  "test/helper.rb",
@@ -72,7 +72,7 @@ Gem::Specification.new do |s|
72
72
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
73
73
  s.specification_version = 3
74
74
 
75
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
75
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
76
76
  s.add_development_dependency(%q<shoulda>, [">= 0"])
77
77
  else
78
78
  s.add_dependency(%q<shoulda>, [">= 0"])
@@ -12,6 +12,15 @@ class TestCrossEach < Test::Unit::TestCase
12
12
  (1..4).cross_each([:a,:b,:c],(11..12)).to_a
13
13
  end
14
14
 
15
+
16
+ should "have a multiple cross-product" do
17
+ assert_equal [[1, :a, 11], [1, :a, 12], [1, :b, 11], [1, :b, 12], [1, :c, 11], [1, :c, 12],
18
+ [2, :a, 11], [2, :a, 12], [2, :b, 11], [2, :b, 12], [2, :c, 11], [2, :c, 12],
19
+ [3, :a, 11], [3, :a, 12], [3, :b, 11], [3, :b, 12], [3, :c, 11], [3, :c, 12],
20
+ [4, :a, 11], [4, :a, 12], [4, :b, 11], [4, :b, 12], [4, :c, 11], [4, :c, 12]],
21
+ (1..4).cross_array([:a,:b,:c],(11..12))
22
+ end
15
23
  end
16
24
 
25
+
17
26
  end
@@ -8,6 +8,18 @@ class TestParalellEach < Test::Unit::TestCase
8
8
  assert_equal [[1, :a, 10], [2, :b, 11], [3, :c, 12]],
9
9
  (1..3).paralell_each([:a,:b,:c],(10..12)).to_a
10
10
  end
11
+
12
+ should "have a multiple paralell combination" do
13
+ assert_equal [[1, :a, 10], [2, :b, 11], [3, :c, 12]],
14
+ (1..3).paralell_array([:a,:b,:c],(10..12))
15
+ end
16
+
17
+ should "enumerate in parallel getting nils when an iterator runs out sooner that others" do
18
+ assert_equal [[1, :a, 10], [2, :b, 11], [3, nil, 12]],
19
+ (1..3).paralell_array([:a,:b],(10..12))
20
+ assert_equal [[1, :a, 10], [2, :b, 11], [3, nil, nil]],
21
+ (1..3).paralell_array([:a,:b],(10..11))
22
+ end
11
23
 
12
24
  end
13
25
 
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: 9
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 1
10
- version: 0.5.1
9
+ - 2
10
+ version: 0.5.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Javier Goizueta
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-09 00:00:00 +02:00
18
+ date: 2010-06-10 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  requirements: []
103
103
 
104
104
  rubyforge_project:
105
- rubygems_version: 1.3.7
105
+ rubygems_version: 1.3.6
106
106
  signing_key:
107
107
  specification_version: 3
108
108
  summary: simple extensions to core classes