forked from newhorizon-tech/custom_enumerables
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.rb
More file actions
286 lines (260 loc) · 6.96 KB
/
Copy pathenum.rb
File metadata and controls
286 lines (260 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
module Enumerable
def my_each
if block_given?
my_each_arr = to_a
my_each_arr.length.times { |i| yield(my_each_arr[i]) }
self
else
enum_for
end
end
def my_each_with_index
if block_given?
my_each_arr = to_a
my_each_arr.length.times { |i| yield(my_each_arr[i], i) }
self
else
enum_for
end
end
def my_select
if block_given?
my_arr = to_a
result = []
my_arr.length.times do |i|
result << my_arr[i] if yield(my_arr[i])
end
result
else
enum_for
end
end
def my_all?(arg = nil)
my_arr = to_a
result = true
if block_given?
my_arr.length.times { |i| result = false if !yield(my_arr[i]) or my_arr[i].nil? }
elsif !arg.nil?
my_arr.length.times do |i|
if arg.is_a? Regexp
result = false unless my_arr[i].match(arg) or my_arr[i].nil?
elsif arg.is_a? Class
result = false unless my_arr[i].is_a? arg or my_arr[i].nil?
else
result = false unless my_arr[i] == arg or my_arr[i].nil?
end
end
else
my_arr.length.times { |i| result = false if !my_arr[i] or my_arr[i].nil? }
end
result
end
def my_any?(arg = nil)
my_arr = to_a
result = false
if block_given?
my_arr.length.times { |i| result = true if yield(my_arr[i]) }
elsif !arg.nil?
my_arr.length.times do |i|
if arg.is_a? Regexp and my_arr[i].match(arg)
result = true
elsif arg.is_a? Class and my_arr[i].is_a? arg
result = true
elsif my_arr[i] == arg
result = true
end
end
else
my_arr.length.times { |i| result = true if my_arr[i] }
end
result
end
def my_none?(arg = nil)
my_arr = to_a
result = true
if block_given?
my_arr.length.times do |i|
result = false if yield(my_arr[i])
end
elsif !arg.nil?
my_arr.length.times do |i|
if arg.is_a? Regexp
result = false if my_arr[i].match(arg)
elsif arg.is_a? Class and my_arr[i].is_a? arg
result = false
elsif my_arr[i] == arg
result = false
end
end
else
my_arr.length.times { |i| result = false if my_arr[i] }
end
result
end
def my_count(arg = nil)
count = 0
my_arr = to_a
if block_given?
my_arr.length.times do |i|
count += 1 if yield(my_arr[i])
end
count
elsif !arg.nil?
my_each do |item|
count += 1 if item == arg
end
count
else
my_arr.length
end
end
def my_map(arg = nil)
map_array = to_a
result = []
if !arg.nil?
map_array.length.times do |i|
result << arg.call(map_array[i])
end
elsif block_given?
map_array.length.times do |i|
result << yield(map_array[i])
end
else
return to_enum
end
result
end
def my_inject(one = nil, two = nil)
init = nil
sym = nil
unless block_given?
if one.is_a? Symbol
sym = one
elsif two.is_a? Symbol
init = one
sym = two
end
end
init = one if block_given?
inject_arr = []
inject_arr.push(init) unless init.nil?
inject_arr += to_a
answer = 0
if sym.nil?
if inject_arr.length == 1
answer = inject_arr[0]
else
inject_arr.length.times do |a|
if a.zero?
answer = yield(inject_arr[0], inject_arr[1])
elsif a > 1
answer = yield(inject_arr[a], answer)
end
end
end
elsif inject_arr.length == 1
inject_arr[0]
else
case sym
when :+
return inject_arr.sum
when :*
answer = 1
inject_arr.length.times do |a|
answer *= inject_arr[a]
end
return answer
end
end
answer
end
end
def multiply_els(arr)
arr.my_inject { |item1, item2| item1 * item2 }
end
# arr = %w[test value test enum]
# puts '-' * 40
# puts "\n Enum method 1. #my_each \n"
# arr.my_each do |p|
# puts p
# end
# puts '-' * 40
# puts "\nEnum method 2. #my_each_with_index \n"
# arr.my_each_with_index do |p, q|
# print [p, q]
# print "\n"
# end
# puts '-' * 40
# test = [1, 2, 3, 5, 6, 8]
# puts '-' * 40
# puts "\n Enum method 3. #my_select \n"
# my_num = test.my_select(&:even?)
# puts my_num.inspect
# puts '-' * 40
# puts "\n Enum method 4. #my_all \n"
# puts { %w[ant bear cat].my_all? { |word| word.length >= 3 } } #=> true
# puts { %w[ant bear cat].my_all? { |word| word.length >= 4 } } #=> false
# puts [nil, true, 99].my_all? #=> false
# puts [].my_all? #=> true
# puts '-' * 40
# puts "\n Enum method 5. #my_any \n"
# puts { [1, 2, 3, 5, 6, 8].my_any? { |n| n > 9 } } #=> false
# puts { [1, 2, 3, 5, 6, 8].my_any? { |n| n > 7 } } #=> true
# puts '-' * 40
# puts "\n Enum method 6. #my_none \n"
# puts [1, 2, 3, 5, 6, 8].my_none? { |n| n > 9 } #=> true
# puts [1, 2, 3, 5, 6, 8].my_none? { |n| n > 7 } #=> false
# puts '-' * 40
# puts "\n Enum method 7. #my_count \n"
# puts { [1, 2, 3, 5, 6, 8].my_count { |n| n > 4 } } #=> 3
# puts { [1, 2, 3, 5, 6, 8].my_count { |n| n > 2 } } #=> 4
# ary = [1, 2, 3, 4, 3, 3, 3]
# puts ary.my_count #=> 7
# puts ary.my_count(3) #=> 4
# puts ary.my_count(&:even?) #=> 2
# puts '-' * 40
# puts "\n Enum method 8. #my_map \n"
# p { (1..4).my_map { |i| i * i } } #=> [1, 4, 9, 16]
# p { (1..4).my_map { 'cat' } } #=> ["cat", "cat", "cat", "cat"]
# p { (1..4).my_map {} }
# puts '-' * 40
# puts "\n Enum method 9. #my_inject \n"
# p 'Original Inject'
# p { [5].inject { |result, element| result * element } } # => 20
# # Same using a block and inject
# p { (5..10).inject { |sum, n| sum + n } } #=> 45
# # Same using a block}
# p { (5..10).inject(1) { |product, n| product * n } } #=> 151200
# # find the longest word
# longest = %w[cat sheep bear].inject do |memo, word|
# memo.length > word.length ? memo : word
# end
# p longest #=> "sheep"
# puts '-' * 40
# p 'My Inject'
# p { [5].my_inject { |result, element| result * element } } # => 20
# # Same using a block and inject
# p { (5..10).my_inject { |sum, n| sum + n } } #=> 45
# # Same using a block
# p { (5..10).my_inject(1) { |product, n| product * n } } #=> 151200
# # find the longest word
# longest = %w[cat sheep bear].my_inject do |memo, word|
# memo.length > word.length ? memo : word
# end
# p longest #=> "sheep"
# new_inject = [[:key, 'CONVERT'], [:value, 'LOWER CASE']].each_with_object({}) do |element, result|
# result[element.first.to_s] = element.last.downcase
# end
# p new_inject # => {"key"=>"convert", "value"=>"lower case"}
# p { [1.4, 2.1, 3.5].my_inject { |sum, n| sum + n } } #=> 7.0
# puts '-' * 40
# puts "\n Enum method 10. #multiply_els \n"
# p multiply_els([5, 2, 10, 8]) #=> 40
# puts '-' * 40
# puts "\n Enum method 11. #my_map proc \n"
# sq = proc { |x| x * x }
# cat_proc = proc { 'cat' }
# p { (1..4).my_map(&sq) }
# p { (1..4).my_map(&cat_proc) }
# puts '-' * 40
#--------------------------------------------------------------------------------