ruby_list_comprehension 0.0.2
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 +7 -0
- data/lib/ruby_list_comprehension.rb +85 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dbcc8eb51951a8ad12232e72f07f9899e448d080421710a5cf4fadcff9b10205
|
4
|
+
data.tar.gz: 262202f315da4d52de27783d4550a629c3dd5c420e0c1b63669cf329352deec6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6bbd40ed89e918b0a2561938a45e591a99e3c0606765de7c8e68a6168a390a9d2bbec2fcb8094c27d1429c11062db0fe9f0ec36514c078cb63b00e654fedb1ac
|
7
|
+
data.tar.gz: c106965977c1b7f7188e3f05cce7b2758701442bf189c0447cd3bfb2a501741adfc61ee874f0bf42c56341e4acacbffa4e25195317176d69bea4ca4a95304d2a
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class ListComprehension
|
4
|
+
attr_reader :c, :version
|
5
|
+
|
6
|
+
def [](*args)
|
7
|
+
c[*args]
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@version = RUBY_VERSION
|
12
|
+
@c = ->x {
|
13
|
+
arr = x.split
|
14
|
+
begin
|
15
|
+
if arr[3][-1] == ';' || arr[4][0] == ';'
|
16
|
+
arr = x.to_s.sub(';', ' do').split
|
17
|
+
end
|
18
|
+
res = eval(x)
|
19
|
+
return [] if res.nil?
|
20
|
+
raise 'iterable can only be range, set, or array, not hash objects' if res.is_a? Hash
|
21
|
+
rescue SyntaxError => se
|
22
|
+
raise 'incorrect syntax for list comprehension' + se.to_s
|
23
|
+
end
|
24
|
+
# raise 'iterable can only be range, set, or array, not hash objects' if eval(x).is_a? Hash
|
25
|
+
|
26
|
+
|
27
|
+
iterable = arr[3]
|
28
|
+
if_condition = arr.include?('if') ? arr[arr.index('if') + 1...-1] : ['true']
|
29
|
+
map_condition = arr[arr.index('do') + 1...(arr.index('if') || arr.index('end'))]
|
30
|
+
if (map_condition == [arr[1]]) && (if_condition == ['true'] || if_condition == true)
|
31
|
+
# p "no method needed"
|
32
|
+
return [eval(arr[3])]
|
33
|
+
end
|
34
|
+
self.class.send(:define_method,'lc') do |arr|
|
35
|
+
if map_condition == [arr[1]]
|
36
|
+
# p 'filter'
|
37
|
+
return eval(iterable).filter do |x|
|
38
|
+
eval(if_condition.join(' '))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
if if_condition == ['true'] || if_condition == true
|
43
|
+
# p 'map'
|
44
|
+
return eval(iterable).map do |x|
|
45
|
+
eval(map_condition.join(' '))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
# check Ruby Version stored in @version
|
49
|
+
if @version >= '2.7.0'
|
50
|
+
return eval(iterable).filter_map do |x|
|
51
|
+
eval(map_condition.join(' ')) if eval(if_condition.join(' '))
|
52
|
+
end
|
53
|
+
else
|
54
|
+
return eval(iterable).map do |x|
|
55
|
+
eval(map_condition.join(' ')) if eval(if_condition.join(' ')).compact!
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
return lc(arr)
|
60
|
+
}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
require 'set'
|
64
|
+
l = ListComprehension.new
|
65
|
+
# p l['for x in 1..[1,2,3].reduce{@1+@2} do x if x end']
|
66
|
+
# # p l["for x in {1=>1, 2=>2} do x if x end"]
|
67
|
+
# p l['for x in 1..10 do x if x end']
|
68
|
+
# p l['for x in Set.new(1..10) do x**2 if x > 2 end']
|
69
|
+
# p l['for x in 1..10 do x**2 if x > 5 end'] == (1..10).filter_map{@1 ** 2 if @1 > 5}
|
70
|
+
# p arr = l['for x in 1..10 do x end'] == [for x in 1..10 do x end]
|
71
|
+
# p arr2 = l['for x in 1..10 do x ** 2 if x % 2 == 0 end'] == (1..10).filter_map{@1**2 if @1 % 2 == 0}
|
72
|
+
# p arr3 = l['for x in [1,2,3,4,5] do x if x % 2 == 0 end'] == [1,2,3,4,5].filter{@1 % 2 == 0}
|
73
|
+
# p arr7 = l['for x in 1..10 do x if x % 2 == 0 end']
|
74
|
+
# p arr9 = l['for x in 1..10 ; x if x % 2 == 0 end'] == (1..10).filter{@1 % 2 == 0}
|
75
|
+
# p arr9 = l['for x in 1..10; x if x % 2 == 0 end'] == (1..10).filter{@1 % 2 == 0}
|
76
|
+
#
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
# a = for x in Set.new([1,2,3]) do x**2 end
|
83
|
+
# Set.new([1,2,3]).filter_map(&->x{x+1 if x > 2})
|
84
|
+
# p a.each(&1.:p)
|
85
|
+
# p for x in Set.new([1,2,3]) do x**2 if x > 2 end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_list_comprehension
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Michael
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2010-04-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: List Comprehensions for Ruby
|
14
|
+
email: smichael@appacademy.io
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/ruby_list_comprehension.rb
|
20
|
+
homepage: https://rubygems.org/gems/ruby_list_comprehension
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubygems_version: 3.0.6
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: Ruby List Comprehension
|
43
|
+
test_files: []
|