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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ruby_list_comprehension.rb +31 -17
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a09a6c55f3c18f9f89ab19ba482e94c50862cb20f251fd5c83755535e248cd64
4
- data.tar.gz: f1812c165243cd0bc3e3c1fd91415e1c1bed2bcb314a4d8c88b48d1d5e5d3683
3
+ metadata.gz: 209cae3b97fefb10d73c95e41a2b96aa4ca74f585ce0297dbc56caf68880b0b7
4
+ data.tar.gz: 1dbd4792663ba7ec560989dd2505923aea9cbc8af302cf5de79c33a46e5838af
5
5
  SHA512:
6
- metadata.gz: 88b9c43e97223089d22f1e921f1933f39d721ad82316c7cded62175254e8d538220ce4fa40e88a61ed2d0f5b1081fd7b54aed8b8d3fc7af2ee48565f10344e41
7
- data.tar.gz: 88ceb53a0fe56ba5342acdd361b18daf0ba7cbeb9a1f5a2a90cad4f1d84661c59ccb814dfbbc355834b8bb1e0d36c2a650b71b75e7edc9cda8ed7270ce3b9e6e
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
- return [] if x == ""
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
- if arr[3][-1] == ';' || arr[4][0] == ';'
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 = eval(arr[3])
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 eval(iterable).filter do |x|
41
- eval(if_condition.join(' '))
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 eval(iterable).map do |x|
48
- eval(map_condition.join(' '))
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 eval(iterable).filter_map do |x|
54
- eval(map_condition.join(' ')) if eval(if_condition.join(' '))
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 eval(iterable).map do |x|
58
- eval(map_condition.join(' ')) if eval(if_condition.join(' ')).compact!
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
- return lc(arr)
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
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
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Michael