comprise 1.1.0 → 1.1.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/README.md +1 -1
- data/lib/comprise/list_comprehension.rb +17 -14
- data/lib/comprise/version.rb +1 -1
- data/spec/comprise/list_comprehension_spec.rb +5 -0
- metadata +1 -1
data/README.md
CHANGED
|
@@ -49,7 +49,7 @@ listcomp(x: 1..3, y: -> { 1..x }, z: -> { [x + y] }).to_a
|
|
|
49
49
|
# => [[1, 1, 2], [2, 1, 3], [2, 2, 4], [3, 1, 4], [3, 2, 5], [3, 3, 6]]
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
*Important note: In Ruby 2.0.x Comprise makes use of Lazy
|
|
52
|
+
*Important note: In Ruby 2.0.x Comprise makes use of Lazy enumerators.*
|
|
53
53
|
|
|
54
54
|
## Contributing
|
|
55
55
|
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
module Comprise
|
|
2
2
|
class ListComprehension
|
|
3
3
|
def initialize(lists)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
@
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
enum.flat_map { |other| @context.new(*other).instance_exec(&var).map { |x| other + [x] } }
|
|
4
|
+
generators = lists.values
|
|
5
|
+
@details_for_inspect = lists.keys.zip(generators.map(&:class))
|
|
6
|
+
@context_klass = Struct.new(*lists.keys)
|
|
7
|
+
@enumerator = generators.inject(init_enumerator(generators.shift)) do |enumerator, generator|
|
|
8
|
+
if generator.respond_to? :call
|
|
9
|
+
enumerator.flat_map { |other| new_context(other).instance_exec(&generator).map { |x| other + [x] } }
|
|
11
10
|
else
|
|
12
|
-
|
|
11
|
+
enumerator.flat_map { |other| generator.map { |x| other + [x] } }
|
|
13
12
|
end
|
|
14
13
|
end
|
|
15
14
|
end
|
|
@@ -19,18 +18,22 @@ module Comprise
|
|
|
19
18
|
end
|
|
20
19
|
|
|
21
20
|
def map
|
|
22
|
-
@enumerator.map { |values|
|
|
21
|
+
@enumerator.map { |values| new_context(values).instance_exec(&Proc.new) }
|
|
23
22
|
end
|
|
24
23
|
|
|
25
24
|
def inspect
|
|
26
|
-
"#<#{self.class.name}:#{self.object_id}>"
|
|
25
|
+
"#<#{self.class.name}:#{self.object_id} generators:#{ @details_for_inspect }>"
|
|
27
26
|
end
|
|
28
27
|
|
|
29
28
|
private
|
|
30
|
-
def
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
def new_context(values)
|
|
30
|
+
@context_klass.new(*values)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def init_enumerator(generator)
|
|
34
|
+
enumerator = generator.respond_to?(:call) ? generator.call : generator
|
|
35
|
+
enumerator = enumerator.lazy if enumerator.respond_to? :lazy
|
|
36
|
+
enumerator.map { |x| [x] }
|
|
34
37
|
end
|
|
35
38
|
end
|
|
36
39
|
end
|
data/lib/comprise/version.rb
CHANGED
|
@@ -43,4 +43,9 @@ describe Comprise::ListComprehension do
|
|
|
43
43
|
[3,1,4], [3,2,5], [3,3,6]
|
|
44
44
|
]
|
|
45
45
|
end
|
|
46
|
+
|
|
47
|
+
it "exposes a little context via #inspect" do
|
|
48
|
+
comp = listcomp(x: [], y: 0..0, z: ->{ []})
|
|
49
|
+
comp.inspect.should == "#<Comprise::ListComprehension:#{comp.object_id} generators:[[:x, Array], [:y, Range], [:z, Proc]]>"
|
|
50
|
+
end
|
|
46
51
|
end
|