comprise 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -0
- data/lib/comprise/list_comprehension.rb +2 -2
- data/lib/comprise/version.rb +1 -1
- data/spec/comprise/list_comprehension_spec.rb +8 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -41,6 +41,14 @@ listcomp(y: -> { (1..5).map { |i| i * 2 } }, x: 1..2) { x * y }.to_a
|
|
41
41
|
=> [2, 4, 4, 8, 6, 12, 8, 16, 10, 20]
|
42
42
|
```
|
43
43
|
|
44
|
+
Comprehensions can also be self referential; provided that your using a lambda or a Proc & your
|
45
|
+
referencing lists that have already been evaluated.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
listcomp(x: 1..3, y: -> { 1..x }, z: -> { [x + y] }).to_a
|
49
|
+
# => [[1, 1, 2], [2, 1, 3], [2, 2, 4], [3, 1, 4], [3, 2, 5], [3, 3, 6]]
|
50
|
+
```
|
51
|
+
|
44
52
|
*Important note: In Ruby 2.0.x Comprise makes use of Lazy emuerators.*
|
45
53
|
|
46
54
|
## Contributing
|
@@ -7,7 +7,7 @@ module Comprise
|
|
7
7
|
@context = Struct.new(*lists.keys)
|
8
8
|
@enumerator = vars.inject(enum) do |enum, var|
|
9
9
|
if var.respond_to? :call
|
10
|
-
enum.flat_map { |other| var.
|
10
|
+
enum.flat_map { |other| @context.new(*other).instance_exec(&var).map { |x| other + [x] } }
|
11
11
|
else
|
12
12
|
enum.flat_map { |other| var.map { |x| other + [x] } }
|
13
13
|
end
|
@@ -19,7 +19,7 @@ module Comprise
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def map
|
22
|
-
@enumerator.map { |values| @context.new(*values).
|
22
|
+
@enumerator.map { |values| @context.new(*values).instance_exec(&Proc.new) }
|
23
23
|
end
|
24
24
|
|
25
25
|
def inspect
|
data/lib/comprise/version.rb
CHANGED
@@ -35,4 +35,12 @@ describe Comprise::ListComprehension do
|
|
35
35
|
end.to_a
|
36
36
|
}.not_to raise_error
|
37
37
|
end
|
38
|
+
|
39
|
+
it "allow lists to reference earlier lists, requires lambda or Proc (i.e. deferred execution)" do
|
40
|
+
listcomp(x: 1..3, y: -> { 1..x }, z: -> { [x + y] }).to_a.should == [
|
41
|
+
[1,1,2],
|
42
|
+
[2,1,3], [2,2,4],
|
43
|
+
[3,1,4], [3,2,5], [3,3,6]
|
44
|
+
]
|
45
|
+
end
|
38
46
|
end
|