ruby_list_comprehension 0.0.4 → 0.0.5
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 +31 -17
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 209cae3b97fefb10d73c95e41a2b96aa4ca74f585ce0297dbc56caf68880b0b7
|
4
|
+
data.tar.gz: 1dbd4792663ba7ec560989dd2505923aea9cbc8af302cf5de79c33a46e5838af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3a876cf461fbf431e8be90fd8cf416077e2abb26a566548f88197a462c241dc337773a62ae6be8c748b510de8e1343de16db47be0977ed0955c53f22460f0df
|
7
|
+
data.tar.gz: 8510c3620b25e95188e0952f18e0eb52cc126adee5a165bb4a123770cbc6e4339b841b7fad44ac6b895b13aa8fd2b38b9defd8e696387cdbac427dc200b5eda9
|
@@ -1,23 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
2
|
+
require 'benchmark'
|
3
3
|
class ListComprehension
|
4
|
-
attr_reader :c, :version
|
5
4
|
|
5
|
+
attr_reader :c, :version, :cache
|
6
6
|
def [](*args)
|
7
7
|
c[*args]
|
8
8
|
end
|
9
9
|
|
10
10
|
def initialize
|
11
|
+
@cache = {}
|
12
|
+
@caching = true
|
11
13
|
@version = RUBY_VERSION
|
12
14
|
@c = ->x {
|
13
|
-
|
15
|
+
# save a pristine copy of call for caching
|
16
|
+
y = x[0..-1]
|
17
|
+
# check for cached results
|
18
|
+
return @cache[x] if @caching && @cache[x]
|
19
|
+
# check for empty call
|
20
|
+
return [] if x.empty? || x == "" || x.nil?
|
21
|
+
# required length of keywords and spaces is 10
|
14
22
|
raise 'syntax error in list comprehension' if x.length < 10
|
15
23
|
arr = x.split
|
24
|
+
# check for semicolon instead of do, for now I replace back to do for simplicity :()
|
25
|
+
if arr[3][-1] == ';' || arr[4][0] == ';' || arr[5][0] == ';' && !arr.include?('do')
|
26
|
+
arr = x.to_s.sub(';', ' do ').split
|
27
|
+
end
|
28
|
+
|
29
|
+
# pre-eval to check for invalid syntax, hash as iterable, etc.
|
16
30
|
begin
|
17
|
-
|
18
|
-
arr = x.to_s.sub(';', ' do').split
|
19
|
-
end
|
20
|
-
res = eval(x)
|
31
|
+
res = instance_eval(x)
|
21
32
|
return [] if res.nil?
|
22
33
|
raise 'iterable can only be range, set, or array, not hash objects' if res.is_a? Hash
|
23
34
|
rescue SyntaxError => se
|
@@ -31,35 +42,38 @@ class ListComprehension
|
|
31
42
|
map_condition = arr[arr.index('do') + 1...(arr.index('if') || arr.index('end'))]
|
32
43
|
if (map_condition == [arr[1]]) && (if_condition == ['true'] || if_condition == true)
|
33
44
|
# p "no method needed"
|
34
|
-
res =
|
45
|
+
res = instance_eval(arr[3])
|
35
46
|
return res.is_a?(Array) ? res : [res]
|
36
47
|
end
|
37
48
|
self.class.send(:define_method,'lc') do |arr|
|
38
49
|
if map_condition == [arr[1]]
|
39
50
|
# p 'filter'
|
40
|
-
return
|
41
|
-
|
51
|
+
return instance_eval(iterable).filter do |x|
|
52
|
+
instance_eval(if_condition.join(' '))
|
42
53
|
end
|
43
54
|
end
|
44
55
|
|
45
56
|
if if_condition == ['true'] || if_condition == true
|
46
57
|
# p 'map'
|
47
|
-
return
|
48
|
-
|
58
|
+
return instance_eval(iterable).map do |x|
|
59
|
+
instance_eval(map_condition.join(' '))
|
49
60
|
end
|
50
61
|
end
|
51
62
|
# check Ruby Version stored in @version
|
52
63
|
if @version >= '2.7.0'
|
53
|
-
return
|
54
|
-
|
64
|
+
return instance_eval(iterable).filter_map do |x|
|
65
|
+
instance_eval(map_condition.join(' ')) if eval(if_condition.join(' '))
|
55
66
|
end
|
56
67
|
else
|
57
|
-
return
|
58
|
-
|
68
|
+
return instance_eval(iterable).map do |x|
|
69
|
+
instance_eval(map_condition.join(' ')) if eval(if_condition.join(' ')).compact!
|
59
70
|
end
|
60
71
|
end
|
61
72
|
end
|
62
|
-
|
73
|
+
list_comp = lc(arr)
|
74
|
+
list_comp = list_comp.is_a?(Array) ? list_comp : [list_comp]
|
75
|
+
@cache[arr] = list_comp
|
76
|
+
list_comp
|
63
77
|
}
|
64
78
|
end
|
65
79
|
end
|