ruby_list_comprehension 0.0.8 → 0.0.9
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.
- checksums.yaml +4 -4
- data/lib/ruby_list_comprehension.rb +44 -25
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92a6a05f9854d7424aa30297fddcdcf38a41d8907158ce1ea307e0b67e9c5a46
|
4
|
+
data.tar.gz: ef703fa9300a220a78d384809edd66b6084446f5ce63d438f90ee6e437528bfa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 958de729eb7b43f1c9c143bb21a7b70050c6ebf547ccce53db30046f9425e87603fb321a198e553f1da182cb28557e7039fb0db5e6bebd7c1a80fa2587828571
|
7
|
+
data.tar.gz: da4861c121ba2782023036b2c4c232ee5258273b6ba7d9e29b6a6e3d2f5c41e60480b27e7e97886527aa82eef62ccd64a71267fbb24f507ce67d15b9a0fcfa68
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
2
|
+
|
3
3
|
class ListComprehension
|
4
4
|
attr_accessor :cache, :caching
|
5
|
-
attr_reader :c, :version
|
5
|
+
attr_reader :c, :version, :op
|
6
6
|
|
7
7
|
def [](*args)
|
8
8
|
c[*args]
|
@@ -10,70 +10,89 @@ class ListComprehension
|
|
10
10
|
|
11
11
|
def initialize
|
12
12
|
@cache = {}
|
13
|
+
@op = nil
|
13
14
|
@caching = true
|
14
15
|
@version = RUBY_VERSION
|
15
|
-
@c =
|
16
|
-
|
17
|
-
# save a pristine copy of call for caching
|
18
|
-
y = x[0..-1]
|
19
|
-
# check for cached results
|
16
|
+
@c = lambda { |x| # check for cached results
|
17
|
+
|
20
18
|
return @cache[x] if @caching && @cache[x]
|
21
|
-
#
|
22
|
-
|
23
|
-
|
19
|
+
# p "cached: #{@cache[arr].to_s}" if @cache[arr]
|
20
|
+
|
21
|
+
y = x[0..-1] # copy of original call
|
22
|
+
z = y[0..-1] # copy of original call
|
23
|
+
return [] if x.empty? || x == '' || x.nil? || eval(z[3]) == '[]'
|
24
24
|
raise 'syntax error in list comprehension' if x.length < 10
|
25
|
+
|
26
|
+
raise 'please use "x" as block parameter name for now' if x[3..5] != ' x '
|
27
|
+
|
25
28
|
arr = x.split
|
26
|
-
|
29
|
+
|
30
|
+
# check for semicolon instead of do, for now I replace to do
|
27
31
|
if arr[3][-1] == ';' || arr[4][0] == ';' || arr[5][0] == ';' && !arr.include?('do')
|
28
32
|
arr = x.to_s.sub(';', ' do ').split
|
29
33
|
end
|
30
34
|
|
31
|
-
# pre-eval to check for invalid syntax
|
35
|
+
# pre-eval to check for invalid syntax
|
32
36
|
begin
|
33
37
|
res = instance_eval(x)
|
34
38
|
return [] if res.nil?
|
35
|
-
raise 'iterable can only be range, set, or array, not hash objects' if res.is_a? Hash
|
36
39
|
rescue SyntaxError => se
|
37
|
-
raise 'incorrect syntax for list comprehension' + se.to_s
|
40
|
+
raise 'incorrect syntax for list comprehension' + "\n" + se.to_s
|
38
41
|
end
|
39
|
-
# raise 'iterable can only be range, set, or array, not hash objects' if eval(x).is_a? Hash
|
40
|
-
|
41
42
|
|
43
|
+
# check for hash to parse csv's
|
42
44
|
iterable = arr[3]
|
45
|
+
mdata = y[3...y.rindex('do')].match(/{.+[=>:].+}/)
|
46
|
+
|
47
|
+
if !mdata.nil?
|
48
|
+
if instance_eval(mdata[0]).is_a? Hash
|
49
|
+
iterable = mdata[0]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
43
53
|
if_condition = arr.include?('if') ? arr[arr.index('if') + 1...-1] : ['true']
|
44
54
|
map_condition = arr[arr.index('do') + 1...(arr.index('if') || arr.index('end'))]
|
45
|
-
|
46
|
-
|
47
|
-
|
55
|
+
|
56
|
+
if (map_condition == [arr[1]]) && (if_condition == ['true'])
|
57
|
+
@op = "each"
|
58
|
+
res = instance_eval(iterable)
|
48
59
|
return res.is_a?(Array) ? res : [res]
|
49
60
|
end
|
50
|
-
|
51
|
-
|
52
|
-
|
61
|
+
|
62
|
+
# define a method to handle the transformation to list_comp
|
63
|
+
self.class.send(:define_method, 'lc') do |arr|
|
64
|
+
if map_condition.include?(arr[1])
|
65
|
+
@op = 'filter'
|
53
66
|
return instance_eval(iterable).filter do |x|
|
54
67
|
instance_eval(if_condition.join(' '))
|
55
68
|
end
|
56
69
|
end
|
57
70
|
|
58
|
-
if if_condition
|
59
|
-
|
71
|
+
if if_condition.include?('true' || 'x') || if_condition
|
72
|
+
@op = 'map'
|
60
73
|
return instance_eval(iterable).map do |x|
|
61
74
|
instance_eval(map_condition.join(' '))
|
62
75
|
end
|
63
76
|
end
|
77
|
+
|
64
78
|
# check Ruby Version stored in @version
|
65
79
|
if @version >= '2.7.0'
|
80
|
+
@op = 'filter_map'
|
66
81
|
return instance_eval(iterable).filter_map do |x|
|
67
82
|
instance_eval(map_condition.join(' ')) if instance_eval(if_condition.join(' '))
|
68
83
|
end
|
69
84
|
else
|
85
|
+
@op = 'map&compact'
|
86
|
+
# p 'map&compact'
|
70
87
|
return instance_eval(iterable).map do |x|
|
71
88
|
instance_eval(map_condition.join(' ')) if instance_eval(if_condition.join(' '))
|
72
89
|
end.compact!
|
73
90
|
end
|
74
91
|
end
|
75
92
|
list_comp = lc(arr)
|
76
|
-
|
93
|
+
unless list_comp.is_a?(Array) || list_comp.is_a?(Hash)
|
94
|
+
list_comp = [list_comp]
|
95
|
+
end
|
77
96
|
@cache[arr] = list_comp if @caching
|
78
97
|
list_comp == [nil] ? [] : list_comp
|
79
98
|
}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_list_comprehension
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Michael
|
@@ -20,7 +20,14 @@ files:
|
|
20
20
|
homepage: https://github.com/SammoMichael/Ruby_List_Comprehension
|
21
21
|
licenses:
|
22
22
|
- MIT
|
23
|
-
metadata:
|
23
|
+
metadata:
|
24
|
+
bug_tracker_uri: https://github.com/SammoMichael/Ruby_List_Comprehension/issues
|
25
|
+
changelog_uri: https://github.com/SammoMichael/Ruby_List_Comprehension/commits/master
|
26
|
+
documentation_uri: https://github.com/SammoMichael/Ruby_List_Comprehension/blob/master/README.md
|
27
|
+
homepage_uri: https://github.com/SammoMichael/Ruby_List_Comprehension
|
28
|
+
mailing_list_uri: https://groups.example.com/bestgemever
|
29
|
+
source_code_uri: https://github.com/SammoMichael/Ruby_List_Comprehension/blob/master/lib/ruby_list_comprehension.rb
|
30
|
+
wiki_uri: https://github.com/SammoMichael/Ruby_List_Comprehension/wiki
|
24
31
|
post_install_message:
|
25
32
|
rdoc_options: []
|
26
33
|
require_paths:
|