-
Notifications
You must be signed in to change notification settings - Fork 4
/
sashimi_friday.rb
658 lines (610 loc) · 13.8 KB
/
sashimi_friday.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
module SashimiFriday
def all?(&block)
each do |*item|
item = item.first if item.size == 1
item = block.call(*item) if block
return false unless item
end
return true
end
def any?(&block)
each do |*item|
item = item.first if item.size == 1
item = block.call(*item) if block
return true if item
end
return false
end
def none?(&block)
each do |*item|
item = item.first if item.size == 1
item = block.call(*item) if block
return false if item
end
return true
end
def one?(&block)
result = nil
each do |*item|
item = item.first if item.size == 1
item = block.call(*item) if block
if item
case result
when nil
result = true
when true
return false
end
end
end
return false unless result
return result
end
def chunk(initial_state = nil, &block)
raise ArgumentError unless block
initial_state = initial_state.dup unless initial_state.nil?
results = []
each do |*item|
item = item.first if item.size == 1
args = [item]
args << initial_state unless initial_state.nil?
case state = block.call(*args)
when :_alone
results << [state, [item]]
when :_separator, nil
next
else
raise if Symbol === state and state.to_s.start_with?(?_)
previous_state, items = results.last
if previous_state == state and items
items << item
else
results << [state, [item]]
end
end
end
return results.to_enum(:each)
end
def collect(&block)
return enum_for(__callee__) unless block
results = []
each do |*item|
item = item.first if item.size == 1
results << block.call(*item)
end
return results
end
alias map collect
def collect_concat(&block)
return enum_for(__callee__) unless block
results = []
each do |*item|
item = item.first if item.size == 1
results << block.call(item)
end
return results.flatten(1)
end
alias flat_map collect_concat
def count(*args, &block)
found = 0
case args.size
when 0
if block_given?
each do |item|
found += 1 if block.call(item)
end
else
each do |item|
found += 1
end
end
when 1
arg = args.first
each do |item|
found += 1 if item == arg
end
else
raise ArgumentError
end
return found
end
def cycle(*args, &block)
return enum_for(__callee__, *args) unless block
raise ArgumentError if args.size > 1
n = args.first
unless n.nil?
begin
n = Integer(n)
return if n <= 0
rescue
raise TypeError
end
end
ary = []
each do |*item|
item = item.first if item.size == 1
ary << item
block.call(item)
end
return if ary.empty?
(1...(n || Float::INFINITY)).each do
ary.each do |item|
block.call(item)
end
end
return nil
end
def drop(n)
results = []
begin
n = Integer(n)
rescue
raise TypeError
end
raise ArgumentError if n < 0
each do |*item|
next if (n = n.pred) >= 0
item = item.first if item.size == 1
results << item
end
return results
end
def take(n)
results = []
begin
n = Integer(n)
rescue
raise TypeError
end
raise ArgumentError if n < 0
return results if n.zero?
each do |*item|
item = item.first if item.size == 1
results << item
break if (n = n.pred) <= 0
end
return results
end
def drop_while(&block)
return enum_for(__callee__) unless block
results = []
state = true
each do |*item|
item = item.first if item.size == 1
if state and block.call(item)
next
else
results << item
state = false
end
end
return results
end
def each_cons(n, &block)
return enum_for(__callee__, n) unless block
n = n.to_int unless Integer === n
raise ArgumentError unless n > 0
list = []
each do |*item|
item = item.first if item.size == 1
list.shift if list.size == n
list.push(item)
block.call(list.dup) if list.size == n
end
return nil
end
def each_entry(*args, &block)
raise ArgumentError if args.size == 1
return enum_for(__callee__, *args) unless block
each(*args) do |*item|
item = item.first if item.size == 1
block.call(item)
end
return self
end
def each_slice(n, &block)
return enum_for(__callee__, n) unless block
n = n.to_int unless Integer === n
raise ArgumentError unless n > 0
list = []
each do |*item|
item = item.first if item.size == 1
list << item
if list.size == n
block.call(list.dup)
list.clear
end
end
block.call(list.dup) unless list.empty?
return nil
end
def each_with_index(*args, &block)
return enum_for(__callee__, *args) unless block
index = -1
each(*args) do |*item|
item = item.first if item.size == 1
block.call(item, index = index.next)
end
return self
end
def each_with_object(obj, &block)
return enum_for(__callee__, obj) unless block
each do |*item|
item = item.first if item.size == 1
block.call(item, obj)
end
return obj
end
def entries(*args)
results = []
each(*args) do |*item|
item = item.first if item.size == 1
results << item
end
return results
end
alias to_a entries
def find(ifnone = nil, &block)
return enum_for(__callee__, ifnone) unless block
each do |*item|
item = item.first if item.size == 1
return item if block.call(item)
end
ifnone.call if ifnone
end
alias detect find
def find_all(&block)
return enum_for(__callee__) unless block
results = []
each do |*item|
item = item.first if item.size == 1
results << item if block.call(item)
end
return results
end
alias select find_all
def find_index(*args, &block)
if args.empty?
return enum_for(__callee__, *args) unless block
else
obj = args.first
end
index = 0
each do |*item|
item = item.first if item.size == 1
return index if obj ? obj == item : block.call(*item)
index += 1
end
return nil
end
def first(*args)
unless args.empty?
n = args.first
n = n.to_int if n.respond_to?(:to_int)
raise TypeError unless Numeric === n
end
if n
return [] if n == 0
raise ArgumentError if n < 0
end
results = []
each do |*item|
item = item.first if item.size == 1
if n
results << item
else
return item
end
n -= 1
break if n <= 0
end
return results if n
end
def grep(pattern)
result = []
each do |item|
if pattern === item
result << (block_given? ? yield(item) : item)
end
end
return result
end
def group_by(&block)
return enum_for(__callee__) unless block
results = Hash.new{|h, k| h[k] = []}
each do |*item|
item = item.first if item.size == 1
results[block.call(item)] << item
end
return results
end
def include?(val)
each do |*item|
item = item.first if item.size == 1
return true if item == val
end
return false
end
alias member? include?
def inject(*args, &block)
block = args.pop.to_proc if Symbol === args.last
unless args.empty?
result = args.shift
init = true
end
each do |*item|
item = item.first if item.size == 1
unless init
init = true
result = item
next
end
result = block.call(result, item)
end
return result
end
alias reduce inject
def lazy
end
def max
_max = nil
init = false
each do |*item|
item = item.first if item.length == 1
unless init
_max = item
init = true
next
end
cmp = if block_given?
yield(item, _max)
else
item <=> _max
end
raise ArgumentError unless cmp
_max = item if cmp > 0
end
_max
end
def max_by(&block)
return enum_for(__callee__) unless block
max = nil
init = false
each do |*item|
item = item.first if item.size == 1
value = block.call(item)
unless init
max = [value, item]
init = true
next
end
cmp = value <=> max[0]
raise ArgumentError unless cmp
max = [value, item] if cmp > 0
end
return max && max[1]
end
def min(&block)
min = nil
init = false
each do |*item|
item = item.first if item.size == 1
unless init
min = item
init = true
next
end
cmp = if block
block.call(item, min)
else
item <=> min
end
raise ArgumentError unless cmp
min = item if cmp < 0
end
return min
end
def min_by(&block)
return enum_for(__callee__) unless block
min = nil
init = false
each do |*item|
item = item.first if item.size == 1
value = block.call(item)
unless init
min = [value, item]
init = true
next
end
cmp = value <=> min[0]
raise ArgumentError unless cmp
min = [value, item] if cmp < 0
end
return min && min[1]
end
def minmax(&block)
minmax = [nil, nil]
init = false
each do |*item|
item = item.first if item.size == 1
unless init
minmax = [item, item]
init = true
next
end
{0 => :<, 1 => :>}.each do |pos, op|
cmp = if block
block.call(item, minmax[pos])
else
item <=> minmax[pos]
end
raise ArgumentError unless cmp
minmax[pos] = item if cmp.__send__(op, 0)
end
end
return minmax
end
def minmax_by(&block)
return enum_for(__callee__) unless block
minmax = [[nil, nil], [nil, nil]]
init = false
each do |*item|
item = item.first if item.size == 1
value = block.call(item)
unless init
minmax = [[value, item], [value, item]]
init = true
next
end
{0 => :<, 1 => :>}.each do |pos, op|
cmp = value <=> minmax[pos][0]
raise ArgumentError unless cmp
minmax[pos] = [value, item] if cmp.__send__(op, 0)
end
end
return minmax.map(&:last)
end
def partition(&block)
return enum_for(__callee__) unless block
ts, fs = [], []
each do |*item|
item = item.first if item.size == 1
(block.call(item) ? ts : fs) << item
end
return ts, fs
end
def reject(&block)
return enum_for(__callee__) unless block
results = []
each do |*item|
item = item.first if item.size == 1
results << item unless block.call(item)
end
return results
end
def reverse_each(&block)
return enum_for(__callee__) unless block
items = []
each do |*item|
item = item.first if item.size == 1
items << item
end
until items.empty?
block.call(items.pop)
end
return self
end
def slice_before(*args, &block)
if block
raise ArgumentError unless args.size < 2
initial_state = args.first
initial_state = initial_state.dup unless initial_state.nil?
else
raise ArgumentError unless args.size == 1
pattern = args.first
end
results = []
each do |*item|
item = item.first if item.size == 1
state = if block
args = [item]
args << initial_state unless initial_state.nil?
state = block.call(*args)
else
pattern === item
end
results << [] if state
last = results.last
last ? last << item : results << [item]
end
return results.to_enum(:each)
end
def sort(&block)
results = []
init = false
each do |*item|
item = item.first if item.size == 1
unless init
results << item
init = true
next
end
index = nil
results.each do |obj|
index ||= 0
cmp = if block
block.call(item, obj)
else
item <=> obj
end
raise ArgumentError unless cmp
break if cmp <= 0
index += 1
end
if index
results[index, 0] = [item]
else
results << item
end
end
return results
end
def sort_by(&block)
return enum_for(__callee__) unless block
results = []
init = false
each do |*item|
item = item.first if item.size == 1
value = block.call(item)
unless init
results << [value, item]
init = true
next
end
index = nil
results.each do |obj|
index ||= 0
cmp = value <=> obj.first
raise ArgumentError unless cmp
break if cmp <= 0
index += 1
end
if index
results[index, 0] = [[value, item]]
else
results << [value, item]
end
end
return results.map(&:last)
end
def take_while(&block)
return enum_for(__callee__) unless block
results = []
each do |*item|
item = item.first if item.size == 1
break unless block.call(item)
results << item
end
return results
end
def zip(*lists, &block)
results = []
lists = lists.map do |list|
list = list.to_ary if list.respond_to?(:to_ary)
list.to_enum(:each)
end
each do |*item|
item = item.first if item.size == 1
items = [item]
lists.each do |list|
items << (list.next rescue nil)
end
if block
block.call(items)
else
results << items
end
end
return results unless block
end
end