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
|
#
# Enumerable
#
module Enumerable
# 15.3.2.2.1
def all?(&block)
st = true
if block
self.each{|val|
unless block.call(val)
st = false
break
end
}
else
self.each{|val|
unless val
st = false
break
end
}
end
st
end
# 15.3.2.2.2
def any?(&block)
st = false
if block
self.each{|val|
if block.call(val)
st = true
break
end
}
else
self.each{|val|
if val
st = true
break
end
}
end
st
end
# 15.3.2.2.3
def collect(&block)
ary = []
self.each{|val|
ary.push(block.call(val))
}
ary
end
# 15.3.2.2.4
def detect(ifnone=nil, &block)
ret = ifnone
self.each{|val|
if block.call(val)
ret = val
break
end
}
ret
end
# 15.3.2.2.5
def each_with_index(&block)
i = 0
self.each{|val|
block.call(val, i)
i += 1
}
self
end
# 15.3.2.2.6
def entries
ary = []
self.each{|val|
ary.push val
}
ary
end
# 15.3.2.2.7
# find(ifnone=nil, &block)
alias find detect
# 15.3.2.2.8
def find_all(&block)
ary = []
self.each{|val|
ary.push(val) if block.call(val)
}
ary
end
# 15.3.2.2.9
def grep(pattern, &block)
ary = []
self.each{|val|
if pattern === val
ary.push((block)? block.call(val): val)
end
}
ary
end
# 15.3.2.2.10
def include?(obj)
st = false
self.each{|val|
if val == obj
st = true
break
end
}
st
end
# 15.3.2.2.11
def inject(*args, &block)
raise ArgumentError, "too many arguments" if args.size > 2
flag = true # 1st element?
result = nil
self.each{|val|
if flag
# 1st element
result = (args.empty?)? val: block.call(args[0], val)
flag = false
else
result = block.call(result, val)
end
}
result
end
# 15.3.2.2.12
# map(&block)
alias map collect
# 15.3.2.2.13
def max(&block)
flag = true # 1st element?
result = nil
self.each{|val|
if flag
# 1st element
result = val
flag = false
else
if block
result = val if block.call(val, result) > 0
else
result = val if (val <=> result) > 0
end
end
}
result
end
# 15.3.2.2.14
def min(&block)
flag = true # 1st element?
result = nil
self.each{|val|
if flag
# 1st element
result = val
flag = false
else
if block
result = val if block.call(val, result) < 0
else
result = val if (val <=> result) < 0
end
end
}
result
end
# 15.3.2.2.15
# member?(obj)
alias member? include?
# 15.3.2.2.16
def partition(&block)
ary_T = []
ary_F = []
self.each{|val|
if block.call(val)
ary_T.push(val)
else
ary_F.push(val)
end
}
[ary_T, ary_F]
end
# 15.3.2.2.17
def reject(&block)
ary = []
self.each{|val|
ary.push(val) unless block.call(val)
}
ary
end
# 15.3.2.2.18
# select(&block)
alias select find_all
# Does this OK? Please test it.
def __sort_sub__(sorted, work, src_ary, head, tail, &block)
if head == tail
sorted[head] = work[head] if src_ary == 1
return
end
# on current step, which is a src ary?
if src_ary == 0
src, dst = sorted, work
else
src, dst = work, sorted
end
key = src[head] # key value for dividing values
i, j = head, tail # position to store on the dst ary
(head + 1).upto(tail){|idx|
if ((block)? block.call(src[idx], key): (src[idx] <=> key)) > 0
# larger than key
dst[j] = src[idx]
j -= 1
else
dst[i] = src[idx]
i += 1
end
}
sorted[i] = key
# sort each sub-array
src_ary = (src_ary + 1) % 2 # exchange a src ary
__sort_sub__(sorted, work, src_ary, head, i - 1, &block) if i > head
__sort_sub__(sorted, work, src_ary, i + 1, tail, &block) if i < tail
end
# private :__sort_sub__
# 15.3.2.2.19
def sort(&block)
ary = []
self.each{|val| ary.push(val)}
unless ary.empty?
__sort_sub__(ary, ::Array.new(ary.size), 0, 0, ary.size - 1, &block)
end
ary
end
# 15.3.2.2.20
# to_a
alias to_a entries
end
|