-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_enumerables.rb
190 lines (174 loc) · 4.11 KB
/
my_enumerables.rb
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
# rubocop:disable Metrics/ModuleLength, Style/CaseEquality, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
module Enumerable
def my_each
arr = to_a
i = 0
if block_given?
while i < arr.length
yield arr[i]
i += 1
end
self
else
to_enum
end
end
def my_each_with_index
arr = to_a
i = 0
if block_given?
while i < arr.length
yield arr[i], i
i += 1
end
self
else
to_enum
end
end
def my_select
arr = to_a
modified_object = []
i = 0
if block_given?
while i < arr.length
modified_object << arr[i] if (yield arr[i]) == true
i += 1
end
modified_object
else
to_enum
end
end
def my_all?(pattern = nil)
arr = to_a
true if arr.length.zero?
selected_array = []
i = 0
if block_given?
selected_array = my_select { |element| yield element }
elsif !block_given? && pattern
while i < arr.length
selected_array << arr[i] if pattern === arr[i]
i += 1
end
elsif !block_given? && !pattern
while i < arr.length
selected_array << arr[i] if arr[i]
i += 1
end
end
selected_array.length == arr.length
end
def my_any?(pattern = nil)
arr = to_a
false if arr.length.zero?
selected_array = []
i = 0
if block_given?
selected_array = my_select { |element| yield element }
elsif !block_given? && pattern
while i < arr.length
selected_array << arr[i] if pattern === arr[i]
i += 1
end
elsif !block_given? && !pattern
while i < arr.length
selected_array << arr[i] if arr[i]
i += 1
end
end
selected_array.length.positive?
end
def my_none?(pattern = nil)
arr = to_a
true if arr.length.zero?
selected_array = []
i = 0
if block_given?
selected_array = my_select { |element| yield element }
elsif !block_given? && pattern
while i < arr.length
selected_array << arr[i] if pattern === arr[i]
i += 1
end
elsif !block_given? && !pattern
while i < arr.length
selected_array << arr[i] if arr[i]
i += 1
end
end
selected_array.length.zero?
end
def my_count(*args)
arr = is_a?(Array) ? self : to_a
count = 0
i = 0
if block_given?
while i < arr.length
count += 1 if yield arr[i]
i += 1
end
elsif !block_given? && !args.empty?
while i < arr.length
count += 1 if arr[i] === args.first
i += 1
end
else
while i < arr.length
count += 1 if arr[i]
i += 1
end
end
count
end
def my_map(a_proc = nil)
object = to_a
modified_object = []
i = 0
if a_proc
while i < object.length
modified_object << a_proc.call(object[i])
i += 1
end
elsif a_proc.nil? && block_given?
while i < object.length
modified_object << (yield object[i])
i += 1
end
else
modified_object = to_enum
end
modified_object
end
def my_inject(initial = nil, method = nil)
object = to_a
modified_object = []
if block_given?
if initial
memo = initial
modified_object = my_map { |element| memo = yield memo, element }
else
memo = first
modified_object = object[1..-1].my_map { |element| memo = yield memo, element }
end
elsif !block_given?
if initial && method
memo = initial
modified_object = my_map { |element| memo = memo.send(method, element) }
elsif initial
method = initial
memo = first
modified_object = to_a[1..-1].my_map { |element| memo = memo.send(method, element) }
else
memo = initial
modified_object = my_map { |element| memo = yield memo, element }
end
end
modified_object[-1]
end
end
def multiply_els(array)
array.my_inject(:*)
end
# rubocop:enable Metrics/ModuleLength, Style/CaseEquality, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity