comprise 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.md +52 -0
- data/comprise.gemspec +22 -0
- data/lib/comprise/list_comprehension.rb +48 -0
- data/lib/comprise/version.rb +3 -0
- data/lib/comprise.rb +2 -0
- data/spec/comprise/list_comprehension_spec.rb +38 -0
- metadata +54 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Chris Lowder
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Comprise
|
2
|
+
|
3
|
+
Comprise brings basic List Comprehension functionality to Ruby.
|
4
|
+
|
5
|
+
> “A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical set-builder notation (set comprehension) as distinct from the use of map and filter functions.” - Wikipedia
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'comprise'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install comprise
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Building a comprehension goes something like:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
comp = Comprise::ListComprehension.new(y: -> { (1..5).map { |i| i * 2 } }, x: 1..2)
|
27
|
+
# => #<Comprise::ListComprehension:70101319648040>
|
28
|
+
|
29
|
+
comp.to_a
|
30
|
+
# => [[2, 1], [2, 2], [4, 1], [4, 2], [6, 1], [6, 2], [8, 1], [8, 2], [10, 1], [10, 2]]
|
31
|
+
|
32
|
+
comp.map { x * y }.to_a # to_a to trigger lazy enumeration in Ruby 2.0
|
33
|
+
# => [2, 4, 4, 8, 6, 12, 8, 16, 10, 20]
|
34
|
+
```
|
35
|
+
|
36
|
+
Comprise also adds the Kernel method `listcomp`, so all that could be done more simply with the
|
37
|
+
one-liner:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
listcomp(y: -> { (1..5).map { |i| i * 2 } }, x: 1..2) { x * y }.to_a
|
41
|
+
=> [2, 4, 4, 8, 6, 12, 8, 16, 10, 20]
|
42
|
+
```
|
43
|
+
|
44
|
+
*Important note: In Ruby 2.0.x Comprise makes use of Lazy emuerators.*
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/comprise.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/comprise/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Chris Lowder"]
|
6
|
+
gem.email = ["clowder@gmail.com"]
|
7
|
+
gem.description = %q{Comprise brings basic List Comprehension functionality to Ruby.}
|
8
|
+
gem.summary = %q{List Comprehension for Ruby.}
|
9
|
+
gem.homepage = "https://clowder.github.com/comprise"
|
10
|
+
|
11
|
+
gem.files = %w{LICENSE
|
12
|
+
README.md
|
13
|
+
comprise.gemspec
|
14
|
+
lib/comprise.rb
|
15
|
+
lib/comprise/list_comprehension.rb
|
16
|
+
lib/comprise/version.rb
|
17
|
+
spec/comprise/list_comprehension_spec.rb}
|
18
|
+
gem.test_files = %w{spec/comprise/list_comprehension_spec.rb}
|
19
|
+
gem.name = "comprise"
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
gem.version = Comprise::VERSION
|
22
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Comprise
|
2
|
+
class ListComprehension
|
3
|
+
def initialize(lists)
|
4
|
+
vars = lists.values
|
5
|
+
enum = init_enum(vars.shift)
|
6
|
+
|
7
|
+
@context = Struct.new(*lists.keys)
|
8
|
+
@enumerator = vars.inject(enum) do |enum, var|
|
9
|
+
if var.respond_to? :call
|
10
|
+
enum.flat_map { |other| var.call.map { |x| other + [x] } }
|
11
|
+
else
|
12
|
+
enum.flat_map { |other| var.map { |x| other + [x] } }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_a
|
18
|
+
@enumerator.to_a
|
19
|
+
end
|
20
|
+
|
21
|
+
def map
|
22
|
+
@enumerator.map { |values| @context.new(*values).instance_eval(&Proc.new) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def inspect
|
26
|
+
"#<#{self.class.name}:#{self.object_id}>"
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def init_enum(var)
|
31
|
+
var = var.respond_to?(:call) ? var.call : var
|
32
|
+
var = var.lazy if var.respond_to? :lazy
|
33
|
+
var.map { |x| [x] }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module Kernel
|
39
|
+
def listcomp(lists)
|
40
|
+
comprehension = Comprise::ListComprehension.new(lists)
|
41
|
+
|
42
|
+
if block_given?
|
43
|
+
comprehension.map(&Proc.new)
|
44
|
+
else
|
45
|
+
comprehension
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/comprise.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.expand_path('../../../lib/comprise', __FILE__)
|
2
|
+
|
3
|
+
describe Comprise::ListComprehension do
|
4
|
+
it "works" do
|
5
|
+
listcomp(x: -> { [1,2,3] }, y: -> { [4,5,6] }).to_a.should == [
|
6
|
+
[1,4], [1,5], [1,6],
|
7
|
+
[2,4], [2,5], [2,6],
|
8
|
+
[3,4], [3,5], [3,6]
|
9
|
+
]
|
10
|
+
end
|
11
|
+
|
12
|
+
it "yields to a block if passed" do
|
13
|
+
listcomp(x: -> { [1,2,3] }, y: -> { [4,5,6] }) do
|
14
|
+
[x,y]
|
15
|
+
end.to_a.should == [[1, 4], [1, 5], [1, 6],
|
16
|
+
[2, 4], [2, 5], [2, 6],
|
17
|
+
[3, 4], [3, 5], [3, 6]]
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns an enumerable" do
|
21
|
+
listcomp(y: -> { 1..3 }, x: -> { 4..6 }) { x*y }.select { |i| i%6==0 }.to_a.should == [6,12,12,18]
|
22
|
+
end
|
23
|
+
|
24
|
+
it "works with enumerable objects" do
|
25
|
+
listcomp(x: 1..2, y: [3,4]).to_a.should == [[1,3], [1,4],
|
26
|
+
[2,3], [2,4]]
|
27
|
+
end
|
28
|
+
|
29
|
+
it "doesn't explode on more complex examples" do
|
30
|
+
expect {
|
31
|
+
listcomp(y: -> { (1..5).map { |i| i * 2 } },
|
32
|
+
x: -> { (1..10).select { |i| i % 2 > 0 } },
|
33
|
+
z: 1..10) do
|
34
|
+
x+y+z
|
35
|
+
end.to_a
|
36
|
+
}.not_to raise_error
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: comprise
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Lowder
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Comprise brings basic List Comprehension functionality to Ruby.
|
15
|
+
email:
|
16
|
+
- clowder@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- LICENSE
|
22
|
+
- README.md
|
23
|
+
- comprise.gemspec
|
24
|
+
- lib/comprise.rb
|
25
|
+
- lib/comprise/list_comprehension.rb
|
26
|
+
- lib/comprise/version.rb
|
27
|
+
- spec/comprise/list_comprehension_spec.rb
|
28
|
+
homepage: https://clowder.github.com/comprise
|
29
|
+
licenses: []
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.11
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: List Comprehension for Ruby.
|
52
|
+
test_files:
|
53
|
+
- spec/comprise/list_comprehension_spec.rb
|
54
|
+
has_rdoc:
|