comprendo 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +3 -0
- data/README.md +60 -0
- data/Rakefile +9 -0
- data/comprendo.gemspec +22 -0
- data/lib/comprendo.rb +27 -0
- data/lib/comprendo/version.rb +3 -0
- data/lib/list_comprehension.treetop +39 -0
- data/test/test_comprendo.rb +29 -0
- metadata +82 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
#Comprendo
|
2
|
+
|
3
|
+
###Python style list comprehensions for Ruby
|
4
|
+
|
5
|
+
Useful? Maybe. Cool? Definitely.
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
#Installation
|
10
|
+
|
11
|
+
gem install comprendo
|
12
|
+
|
13
|
+
#Usage
|
14
|
+
|
15
|
+
Comprendo overrides the backtick (\`) character to
|
16
|
+
allow python-style list comprehensions within backticks.
|
17
|
+
It does not do this globally. Since it *does not* monkeypatch,
|
18
|
+
comprendo must be included as a mixin in order to be used.
|
19
|
+
|
20
|
+
#Examples
|
21
|
+
|
22
|
+
###Including
|
23
|
+
|
24
|
+
include Comprendo
|
25
|
+
|
26
|
+
Always include comprendo in the class where it is used, mmmkay?
|
27
|
+
|
28
|
+
###Base Case
|
29
|
+
`x for x in [1,2,3]`
|
30
|
+
=> [1, 2, 3]
|
31
|
+
|
32
|
+
Pretty simple eh?
|
33
|
+
|
34
|
+
###View
|
35
|
+
`x*x for x in [1,2,3]`
|
36
|
+
=> [1, 4, 9]
|
37
|
+
|
38
|
+
The view is just ruby code.
|
39
|
+
|
40
|
+
###Conditional
|
41
|
+
`x for x in [1, 2, 3, 4] if x%2 == 1`
|
42
|
+
=> [1,3]
|
43
|
+
|
44
|
+
The conditional is just ruby code too.
|
45
|
+
|
46
|
+
`x for x in [1, 2, 3, 4] unless x%2 == 1`
|
47
|
+
=> [2,4]
|
48
|
+
|
49
|
+
Seriously, go nuts.
|
50
|
+
|
51
|
+
###Variable references
|
52
|
+
|
53
|
+
@a = [3,4,5]
|
54
|
+
@z = 4
|
55
|
+
`x + @z for x in @a`
|
56
|
+
=> [7, 8, 9]
|
57
|
+
|
58
|
+
If you want to reference variables within your comprehension, they must be
|
59
|
+
instance variables on your class.
|
60
|
+
|
data/Rakefile
ADDED
data/comprendo.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "comprendo/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "comprendo"
|
7
|
+
s.version = Comprendo::VERSION
|
8
|
+
s.authors = ["Sam Beran"]
|
9
|
+
s.email = ["sberan@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Python style list comprehensions for ruby}
|
12
|
+
s.description = %q{Python style list comprehensions for ruby}
|
13
|
+
|
14
|
+
s.rubyforge_project = "comprendo"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "treetop"
|
22
|
+
end
|
data/lib/comprendo.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'polyglot'
|
3
|
+
require 'treetop'
|
4
|
+
require 'list_comprehension'
|
5
|
+
|
6
|
+
module Comprendo
|
7
|
+
VERSION = "0.2"
|
8
|
+
|
9
|
+
def `(cmp)
|
10
|
+
@parser ||= ListComprehensionParser.new
|
11
|
+
results = @parser.parse(cmp)
|
12
|
+
throw parser.failure_reason unless results
|
13
|
+
|
14
|
+
iter = eval(results.iter.text_value)
|
15
|
+
unless results.filter.type.empty?
|
16
|
+
iter = iter.select do |a|
|
17
|
+
eval(results.target.text_value + " = a")
|
18
|
+
eval("true " + results.filter.text_value)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
iter.map do |a|
|
23
|
+
eval(results.target.text_value + " = a")
|
24
|
+
eval(results.expression.text_value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
grammar ListComprehension
|
2
|
+
rule list_comprehension
|
3
|
+
expression for target in iter filter
|
4
|
+
end
|
5
|
+
|
6
|
+
rule expression
|
7
|
+
(!for .)*
|
8
|
+
end
|
9
|
+
|
10
|
+
rule for
|
11
|
+
" for "
|
12
|
+
end
|
13
|
+
|
14
|
+
rule target
|
15
|
+
(!in .)*
|
16
|
+
end
|
17
|
+
|
18
|
+
rule in
|
19
|
+
" in "
|
20
|
+
end
|
21
|
+
|
22
|
+
rule iter
|
23
|
+
(!filter_type .)*
|
24
|
+
end
|
25
|
+
|
26
|
+
rule filter
|
27
|
+
(type:filter_type? test)
|
28
|
+
end
|
29
|
+
|
30
|
+
rule filter_type
|
31
|
+
(" if " / " unless ")
|
32
|
+
end
|
33
|
+
|
34
|
+
rule test
|
35
|
+
.*
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'comprendo'
|
3
|
+
|
4
|
+
|
5
|
+
class ComprendoTest < Test::Unit::TestCase
|
6
|
+
include Comprendo
|
7
|
+
|
8
|
+
def test_simple
|
9
|
+
assert_equal [1, 2, 3], `x for x in [1,2,3]`
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_view
|
13
|
+
assert_equal [1, 4, 9], `x*x for x in [1,2,3]`
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_if
|
17
|
+
assert_equal [1,3], `x for x in [1, 2, 3, 4] if x%2 == 1`
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_unless
|
21
|
+
assert_equal [2,4], `x for x in [1, 2, 3, 4] unless x%2 == 1`
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_var
|
25
|
+
@a = [3,4,5]
|
26
|
+
@z = 4
|
27
|
+
assert_equal [7, 8, 9], `x + @z for x in @a`
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: comprendo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Sam Beran
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-12-06 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: treetop
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
description: Python style list comprehensions for ruby
|
33
|
+
email:
|
34
|
+
- sberan@gmail.com
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
files:
|
42
|
+
- .gitignore
|
43
|
+
- Gemfile
|
44
|
+
- README.md
|
45
|
+
- Rakefile
|
46
|
+
- comprendo.gemspec
|
47
|
+
- lib/comprendo.rb
|
48
|
+
- lib/comprendo/version.rb
|
49
|
+
- lib/list_comprehension.treetop
|
50
|
+
- test/test_comprendo.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: ""
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project: comprendo
|
77
|
+
rubygems_version: 1.3.6
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Python style list comprehensions for ruby
|
81
|
+
test_files:
|
82
|
+
- test/test_comprendo.rb
|