-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathenumerable.rb
504 lines (459 loc) · 10.4 KB
/
enumerable.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
module Enumerable
def first(*args)
if args.empty?
self.each_entry { |e| return e }
nil
else
take(*args)
end
end
def map(&block)
return self.enum_for(:map) unless block
result = []
self.each do |*x|
result << yield(*x)
end
result
end
alias collect map
def inject(*args, &block)
op = nil
dropped = 0
memo = nil
case args.size
when 0
dropped = 1
when 1
if args[0].is_a?(Symbol)
dropped = 1
op = args[0]
else
memo = args[0]
end
when 2
memo = args[0]
op = args[1]
end
self.each_with_index do |e, i|
if i < dropped
memo = e
else
memo = op ? memo.send(op, e) : yield(memo, e)
end
end
memo
end
alias reduce inject
def each_with_index(*args, &block)
return self.enum_for(:each_with_index, *args) if !block
i = 0
self.each_entry(*args) do |obj|
yield obj, i
i += 1
end
end
def each_with_object(memo, &block)
return self.enum_for(:each_with_object, memo) unless block
self.each_entry do |elm|
yield elm, memo
end
memo
end
def each_entry(*args, &block)
return self.enum_for(:each_entry, *args) unless block
each(*args) do |*e|
v = (e.size == 1) ? e[0] : e
yield v
end
self
end
def reverse_each(&block)
return self.enum_for(:reverse_each) unless block
self.to_a.reverse_each(&block)
self
end
def all?(&block)
if block
self.each { |*e| return false unless yield(*e) }
else
self.each_entry { |e| return false unless e }
end
true
end
def any?(&block)
if block
self.each { |*e| return true if yield(*e) }
else
self.each_entry { |e| return true if e }
end
false
end
def select(&block)
return self.enum_for(:select) unless block
result = []
self.each_entry do |o|
if block.call(o)
result << o
end
end
result
end
alias :find_all :select
def include?(obj)
self.each_entry do |o|
return true if o == obj
end
false
end
alias member? include?
def drop(n)
n = Topaz.convert_type(n, Fixnum, :to_int)
raise ArgumentError.new("attempt to drop negative size") if n < 0
result = self.to_a
return [] if n > result.size
result[n...result.size]
end
def drop_while(&block)
return self.enum_for(:drop_while) if !block
result = []
dropping = true
self.each_entry do |o|
unless dropping && yield(o)
result << o
dropping = false
end
end
result
end
def to_a(*args)
result = []
self.each_entry(*args) do |i|
result << i
end
result
end
alias entries to_a
def detect(ifnone = nil, &block)
return self.enum_for(:detect, ifnone) unless block
self.each_entry do |o|
return o if block.call(o)
end
ifnone.is_a?(Proc) ? ifnone.call : ifnone
end
alias find detect
def take(n)
n = Topaz.convert_type(n, Fixnum, :to_int)
raise ArgumentError.new("attempt to take negative size") if n < 0
result = []
unless n == 0
self.each_entry do |o|
result << o
break if result.size == n
end
end
result
end
def take_while(&block)
return self.enum_for(:take_while) unless block
result = []
self.each_entry do |o|
break unless yield(o)
result << o
end
result
end
def reject(&block)
return self.enum_for(:reject) unless block
result = []
self.each_entry do |o|
result << o unless yield(o)
end
result
end
def max(&block)
max = nil
self.each_with_index do |e, i|
max = e if i == 0 || Topaz.compare(e, max, &block) > 0
end
max
end
def min(&block)
min = nil
self.each_with_index do |e, i|
min = e if i == 0 || Topaz.compare(e, min, &block) < 0
end
min
end
def max_by(&block)
return self.enum_for(:max_by) unless block
max = maxv = nil
self.each_with_index do |e, i|
ev = yield(e)
max, maxv = e, ev if i == 0 || Topaz.compare(ev, maxv) > 0
end
max
end
def min_by(&block)
return self.enum_for(:min_by) unless block
min = minv = nil
self.each_with_index do |e, i|
ev = yield(e)
min, minv = e, ev if i == 0 || Topaz.compare(ev, minv) < 0
end
min
end
def minmax(&block)
min = max = nil
self.each_with_index do |e, i|
min = e if i == 0 || Topaz.compare(e, min, &block) < 0
max = e if i == 0 || Topaz.compare(e, max, &block) > 0
end
[min, max]
end
def minmax_by(&block)
return self.enum_for(:minmax_by) unless block
min = max = minv = maxv = nil
self.each_with_index do |e, i|
ev = yield(e)
max, maxv = e, ev if i == 0 || Topaz.compare(ev, maxv) > 0
min, minv = e, ev if i == 0 || Topaz.compare(ev, minv) < 0
end
[min, max]
end
def partition(&block)
return self.enum_for(:partition) unless block
a, b = [], []
self.each_entry do |e|
block.call(e) ? a.push(e) : b.push(e)
end
[a, b]
end
def count(*args, &block)
c = 0
if args.empty?
if block
self.each { |e| c += 1 if block.call(e) }
else
self.each { c += 1 }
end
else
arg = args[0]
self.each { |e| c += 1 if e == arg }
end
c
end
def one?(&block)
c = 0
if block
self.each do |*e|
c += 1 if yield(*e)
return false if c > 1
end
else
self.each_entry do |e|
c += 1 if e
return false if c > 1
end
end
c == 1
end
def none?(&block)
if block
self.each { |*e| return false if yield(*e) }
else
self.each_entry { |e| return false if e }
end
true
end
def group_by(&block)
return self.enum_for(:group_by) unless block
h = {}
self.each_entry do |e|
v = yield e
a = h.fetch(v) { |v| h[v] = [] }
a << e
end
h
end
def find_index(obj = nil, &block)
return self.enum_for(:find_index) if !obj && !block
i = 0
each do |e|
return i if obj ? (e == obj) : block.call(e)
i += 1
end
nil
end
def each_cons(num, &block)
return self.enum_for(:each_cons, num) if !block
num = Topaz.convert_type(num, Fixnum, :to_int)
raise ArgumentError.new("invalid size") if num <= 0
buf = []
self.each_with_index do |e, i|
buf << e
if i == num - 1
yield buf.dup
elsif i >= num
buf.shift
yield buf.dup
end
end
nil
end
def each_slice(num, &block)
return self.enum_for(:each_slice, num) if !block
num = Topaz.convert_type(num, Fixnum, :to_int)
raise ArgumentError.new("invalid slice size") if num <= 0
buf = []
self.each_entry do |e|
buf << e
if buf.size == num
yield buf
buf = []
end
end
yield buf unless buf.empty?
nil
end
def sort(&block)
to_a.sort!(&block)
end
def sort_by(&block)
to_a.sort_by!(&block)
end
def collect_concat(&block)
return self.enum_for(:collect_concat) unless block
out = []
self.each do |e|
v = yield(e)
if ary = Array.try_convert(v)
out.concat(ary)
else
out << v
end
end
out
end
alias flat_map collect_concat
def zip(*lists, &block)
lists = lists.map do |l|
l.respond_to?(:to_ary) ? l.to_ary : l.to_enum(:each)
end
index = -1
tail = proc do
index += 1
lists.map do |l|
l.kind_of?(Array) ? l[index] : l.next
end
end
if block
self.each do |elm|
yield [elm, *tail.call]
end
nil
else
res = []
self.each_entry { |elm| res << [elm, *tail.call] }
res
end
end
def cycle(n = nil, &block)
return self.enum_for(:cycle, n) unless block
unless n.nil?
n = Topaz.convert_type(n, Fixnum, :to_int)
return nil if n <= 0
end
buf = []
self.each_entry do |e|
buf << e
yield e
end
return nil if buf.empty?
if n
(n - 1).times { buf.each(&block) }
else
while true
buf.each(&block)
end
end
end
def grep(pattern, &block)
ret = []
if block
self.each do |elm|
if pattern === elm
ret << yield(elm)
end
end
else
self.each do |elm|
if pattern === elm
ret << elm
end
end
end
ret
end
def chunk(initial_state = nil, &original_block)
raise ArgumentError.new("no block given") unless original_block
::Enumerator.new do |yielder|
previous = nil
accumulate = []
block = initial_state.nil? ? original_block : Proc.new{ |val| original_block.yield(val, initial_state.clone)}
each do |val|
key = block.yield(val)
if key.nil? || (key.is_a?(Symbol) && key.to_s[0, 1] == "_")
yielder.yield [previous, accumulate] unless accumulate.empty?
accumulate = []
previous = nil
case key
when nil, :_separator
when :_alone
yielder.yield [key, [val]]
else
raise RuntimeError.new("symbols beginning with an underscore are reserved")
end
else
if previous.nil? || previous == key
accumulate << val
else
yielder.yield [previous, accumulate] unless accumulate.empty?
accumulate = [val]
end
previous = key
end
end
yielder.yield [previous, accumulate] unless accumulate.empty?
end
end
def slice_before(*args, &block)
arg = nil
if block
raise ArgumentError.new("wrong number of arguments (#{args.size} for 0..1)") if args.size > 1
if args.size == 1
has_init = true
arg = args[0]
end
else
raise ArgumentError.new("wrong number of arguments (#{args.size} for 1)") if args.size > 1
raise ArgumentError.new("wrong number of arguments (0 for 1)") if args.empty?
arg = args[0]
block = Proc.new{ |elem| arg === elem }
end
::Enumerator.new do |yielder|
init = arg.dup if has_init
accumulator = nil
each do |elem|
start_new = has_init ? block.yield(elem, init) : block.yield(elem)
if start_new
yielder.yield accumulator if accumulator
accumulator = [elem]
else
accumulator ||= []
accumulator << elem
end
end
yielder.yield accumulator if accumulator
end
end
end