Class: Code::Object::List

Inherits:
Code::Object show all
Defined in:
lib/code/object/list.rb

Direct Known Subclasses

IdentifierList

Constant Summary

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary

Attributes included from Concerns::Shared

#methods, #raw

Instance Method Summary collapse

Methods inherited from Code::Object

code_new, #code_new, maybe, #name, repeat, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #blank?, #code_and, #code_as_json, #code_blank?, #code_compare, #code_different, #code_duplicate, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_falsy?, code_fetch, code_get, #code_greater, #code_greater_or_equal, #code_inclusive_range, #code_inspect, #code_less, #code_less_or_equal, #code_methods, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_self, code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?

Constructor Details

#initialize(*args, **_kargs, &_block) ⇒ List

Returns a new instance of List.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/code/object/list.rb', line 114

def initialize(*args, **_kargs, &_block)
  self.raw =
    if args.first.is_a?(List)
      args.first.raw.map(&:to_code)
    elsif args.first.is_a?(Dictionary)
      args.first.raw.to_a.map(&:to_code)
    elsif args.first.is_an?(::Array)
      args.first.map(&:to_code)
    elsif args.first.is_a?(::Hash)
      args.first.to_a.map(&:to_code)
    else
      []
    end
end

Instance Method Details

#any?Boolean

Returns:



1075
1076
1077
# File 'lib/code/object/list.rb', line 1075

def any?
  code_any?.truthy?
end

#call(**args) ⇒ Object



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
# File 'lib/code/object/list.rb', line 129

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  globals = multi_fetch(args, *GLOBALS)
  code_value = code_arguments.code_first

  case code_operator.to_s
  when "join"
    sig(args) { String.maybe }
    code_join(code_value)
  when "sort"
    sig(args) { Function.maybe }
    code_sort(code_value, **globals)
  when "<<", "append"
    sig(args) { Object }
    code_append(code_value)
  when "+", "plus"
    sig(args) { List.maybe }
    code_arguments.any? ? code_plus(code_value) : code_self
  when "-", "minus"
    sig(args) { List }
    code_minus(code_value)
  when "any?"
    sig(args) { (Function | Class).maybe }
    code_any?(code_value, **globals)
  when "detect"
    sig(args) { (Function | Class).maybe }
    code_detect(code_value, **globals)
  when "each"
    sig(args) { (Function | Class).maybe }
    code_each(code_value, **globals)
  when "first"
    sig(args) { Integer.maybe }
    code_first(code_value)
  when "sample"
    sig(args) { Integer.maybe }
    code_sample(code_value)
  when "shuffle"
    sig(args)
    code_shuffle
  when "flatten"
    sig(args) { Integer.maybe }
    code_flatten(code_value)
  when "pop"
    sig(args) { Integer.maybe }
    code_pop(code_value)
  when "pop!"
    sig(args) { Integer.maybe }
    code_pop!(code_value)
  when "shift"
    sig(args) { Integer.maybe }
    code_shift(code_value)
  when "include?"
    sig(args) { Object }
    code_include?(code_value)
  when "last"
    sig(args)
    code_last
  when "map"
    sig(args) { (Function | Class).maybe }
    code_map(code_value, **globals)
  when "map!"
    sig(args) { (Function | Class).maybe }
    code_map!(code_value, **globals)
  when "max"
    sig(args) { (Function | Class).maybe }
    code_max(code_value, **globals)
  when "none?"
    sig(args) { (Function | Class).maybe }
    code_none?(code_value, **globals)
  when "all?"
    sig(args) { (Function | Class).maybe }
    code_all?(code_value, **globals)
  when "reduce"
    sig(args) { (Function | Class).maybe }
    code_reduce(code_value, **globals)
  when "reverse"
    sig(args)
    code_reverse
  when "select"
    sig(args) { (Function | Class).maybe }
    code_select(code_value, **globals)
  when "select!"
    sig(args) { (Function | Class).maybe }
    code_select!(code_value, **globals)
  when "compact"
    sig(args) { (Function | Class).maybe }
    code_compact(code_value, **globals)
  when "compact!"
    sig(args) { (Function | Class).maybe }
    code_compact!(code_value, **globals)
  when "reject"
    sig(args) { (Function | Class).maybe }
    code_reject(code_value, **globals)
  when "reject!"
    sig(args) { (Function | Class).maybe }
    code_reject!(code_value, **globals)
  when "size"
    sig(args)
    code_size
  when "sum"
    sig(args)
    code_sum
  when "uniq"
    sig(args) { (Function | Class).maybe }
    code_uniq(code_value, **globals)
  when "many?"
    sig(args)
    code_many?
  when "positive?"
    sig(args)
    code_positive?
  when "negative?"
    sig(args)
    code_negative?
  when "zero?"
    sig(args)
    code_zero?
  when "one?"
    sig(args)
    code_one?
  when "two?"
    sig(args)
    code_two?
  when "three?"
    sig(args)
    code_three?
  when "four?"
    sig(args)
    code_four?
  when "five?"
    sig(args)
    code_five?
  when "six?"
    sig(args)
    code_six?
  when "seven?"
    sig(args)
    code_seven?
  when "eight?"
    sig(args)
    code_eight?
  when "nine?"
    sig(args)
    code_nine?
  when "ten?"
    sig(args)
    code_ten?
  when "eleven?"
    sig(args)
    code_eleven?
  when "twelve?"
    sig(args)
    code_twelve?
  when "thirteen?"
    sig(args)
    code_thirteen?
  when "fourteen?"
    sig(args)
    code_fourteen?
  when "fifteen?"
    sig(args)
    code_fifteen?
  when "sixteen?"
    sig(args)
    code_sixteen?
  when "seventeen?"
    sig(args)
    code_seventeen?
  when "eighteen?"
    sig(args)
    code_eighteen?
  when "nineteen?"
    sig(args)
    code_nineteen?
  when "twenty?"
    sig(args)
    code_twenty?
  when "twenty_one?"
    sig(args)
    code_twenty_one?
  when "twenty_two?"
    sig(args)
    code_twenty_two?
  when "twenty_three?"
    sig(args)
    code_twenty_three?
  when "twenty_four?"
    sig(args)
    code_twenty_four?
  when "twenty_five?"
    sig(args)
    code_twenty_five?
  when "twenty_six?"
    sig(args)
    code_twenty_six?
  when "twenty_seven?"
    sig(args)
    code_twenty_seven?
  when "twenty_eight?"
    sig(args)
    code_twenty_eight?
  when "twenty_nine?"
    sig(args)
    code_twenty_nine?
  when "thirty?"
    sig(args)
    code_thirty?
  when "thirty_one?"
    sig(args)
    code_thirty_one?
  when "thirty_two?"
    sig(args)
    code_thirty_two?
  when "thirty_three?"
    sig(args)
    code_thirty_three?
  when "thirty_four?"
    sig(args)
    code_thirty_four?
  when "thirty_five?"
    sig(args)
    code_thirty_five?
  when "thirty_six?"
    sig(args)
    code_thirty_six?
  when "thirty_seven?"
    sig(args)
    code_thirty_seven?
  when "thirty_eight?"
    sig(args)
    code_thirty_eight?
  when "thirty_nine?"
    sig(args)
    code_thirty_nine?
  when "forty?"
    sig(args)
    code_forty?
  when "forty_one?"
    sig(args)
    code_forty_one?
  when "forty_two?"
    sig(args)
    code_forty_two?
  when "forty_three?"
    sig(args)
    code_forty_three?
  when "forty_four?"
    sig(args)
    code_forty_four?
  when "forty_five?"
    sig(args)
    code_forty_five?
  when "forty_six?"
    sig(args)
    code_forty_six?
  when "forty_seven?"
    sig(args)
    code_forty_seven?
  when "forty_eight?"
    sig(args)
    code_forty_eight?
  when "forty_nine?"
    sig(args)
    code_forty_nine?
  when "fifty?"
    sig(args)
    code_fifty?
  when "fifty_one?"
    sig(args)
    code_fifty_one?
  when "fifty_two?"
    sig(args)
    code_fifty_two?
  when "fifty_three?"
    sig(args)
    code_fifty_three?
  when "fifty_four?"
    sig(args)
    code_fifty_four?
  when "fifty_five?"
    sig(args)
    code_fifty_five?
  when "fifty_six?"
    sig(args)
    code_fifty_six?
  when "fifty_seven?"
    sig(args)
    code_fifty_seven?
  when "fifty_eight?"
    sig(args)
    code_fifty_eight?
  when "fifty_nine?"
    sig(args)
    code_fifty_nine?
  when "sixty?"
    sig(args)
    code_sixty?
  when "sixty_one?"
    sig(args)
    code_sixty_one?
  when "sixty_two?"
    sig(args)
    code_sixty_two?
  when "sixty_three?"
    sig(args)
    code_sixty_three?
  when "sixty_four?"
    sig(args)
    code_sixty_four?
  when "sixty_five?"
    sig(args)
    code_sixty_five?
  when "sixty_six?"
    sig(args)
    code_sixty_six?
  when "sixty_seven?"
    sig(args)
    code_sixty_seven?
  when "sixty_eight?"
    sig(args)
    code_sixty_eight?
  when "sixty_nine?"
    sig(args)
    code_sixty_nine?
  when "seventy?"
    sig(args)
    code_seventy?
  when "seventy_one?"
    sig(args)
    code_seventy_one?
  when "seventy_two?"
    sig(args)
    code_seventy_two?
  when "seventy_three?"
    sig(args)
    code_seventy_three?
  when "seventy_four?"
    sig(args)
    code_seventy_four?
  when "seventy_five?"
    sig(args)
    code_seventy_five?
  when "seventy_six?"
    sig(args)
    code_seventy_six?
  when "seventy_seven?"
    sig(args)
    code_seventy_seven?
  when "seventy_eight?"
    sig(args)
    code_seventy_eight?
  when "seventy_nine?"
    sig(args)
    code_seventy_nine?
  when "eighty?"
    sig(args)
    code_eighty?
  when "eighty_one?"
    sig(args)
    code_eighty_one?
  when "eighty_two?"
    sig(args)
    code_eighty_two?
  when "eighty_three?"
    sig(args)
    code_eighty_three?
  when "eighty_four?"
    sig(args)
    code_eighty_four?
  when "eighty_five?"
    sig(args)
    code_eighty_five?
  when "eighty_six?"
    sig(args)
    code_eighty_six?
  when "eighty_seven?"
    sig(args)
    code_eighty_seven?
  when "eighty_eight?"
    sig(args)
    code_eighty_eight?
  when "eighty_nine?"
    sig(args)
    code_eighty_nine?
  when "ninety?"
    sig(args)
    code_ninety?
  when "ninety_one?"
    sig(args)
    code_ninety_one?
  when "ninety_two?"
    sig(args)
    code_ninety_two?
  when "ninety_three?"
    sig(args)
    code_ninety_three?
  when "ninety_four?"
    sig(args)
    code_ninety_four?
  when "ninety_five?"
    sig(args)
    code_ninety_five?
  when "ninety_six?"
    sig(args)
    code_ninety_six?
  when "ninety_seven?"
    sig(args)
    code_ninety_seven?
  when "ninety_eight?"
    sig(args)
    code_ninety_eight?
  when "ninety_nine?"
    sig(args)
    code_ninety_nine?
  when "one_hundred?"
    sig(args)
    code_one_hundred?
  else
    super
  end
end

#code_all?(argument = nil, **globals) ⇒ Boolean

Returns:



796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'lib/code/object/list.rb', line 796

def code_all?(argument = nil, **globals)
  code_argument = argument.to_code

  index = 0

  Boolean.new(
    raw.all? do |code_element|
      if code_argument.is_a?(Function)
        code_argument
          .call(
            arguments: List.new([code_element, Integer.new(index), self]),
            **globals
          )
          .truthy?
          .tap { index += 1 }
      elsif code_argument.is_a?(Class)
        code_element.is_a?(code_argument.raw).tap { index += 1 }
      else
        true.tap { index += 1 }
      end
    rescue Error::Next => e
      e.code_value.truthy?.tap { index += 1 }
    end
  )
end

#code_any?(argument = nil, **globals) ⇒ Boolean

Returns:



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
# File 'lib/code/object/list.rb', line 552

def code_any?(argument = nil, **globals)
  code_argument = argument.to_code

  index = 0

  Boolean.new(
    raw.any? do |code_element|
      if code_argument.is_a?(Function)
        code_argument
          .call(
            arguments: List.new([code_element, Integer.new(index), self]),
            **globals
          )
          .truthy?
          .tap { index += 1 }
      elsif code_argument.is_a?(Class)
        code_element.is_a?(code_argument.raw).tap { index += 1 }
      else
        true.tap { index += 1 }
      end
    rescue Error::Next => e
      e.code_value.tap { index += 1 }
    end
  )
end

#code_append(other) ⇒ Object



578
579
580
581
582
583
584
# File 'lib/code/object/list.rb', line 578

def code_append(other)
  code_other = other.to_code

  raw << code_other

  self
end

#code_compact(argument = nil, **globals) ⇒ Object



848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/code/object/list.rb', line 848

def code_compact(argument = nil, **globals)
  code_argument = argument.to_code

  index = 0

  List.new(
    raw.select do |code_element|
      if code_argument.is_a?(Function)
        code_argument
          .call(
            arguments: List.new([code_element, Integer.new(index), self]),
            **globals
          )
          .truthy?
          .tap { index += 1 }
      elsif code_argument.is_a?(Class)
        code_element.is_a?(code_argument.raw).tap { index += 1 }
      else
        code_element.truthy?.tap { index += 1 }
      end
    rescue Error::Next => e
      e.code_value.truhty?.tap { index += 1 }
    end
  )
end

#code_compact!(argument = nil, **globals) ⇒ Object



874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
# File 'lib/code/object/list.rb', line 874

def code_compact!(argument = nil, **globals)
  code_argument = argument.to_code

  index = 0

  raw.select! do |code_element|
    if code_argument.is_a?(Function)
      code_argument
        .call(
          arguments: List.new([code_element, Integer.new(index), self]),
          **globals
        )
        .truthy?
        .tap { index += 1 }
    elsif code_argument.is_a?(Class)
      code_element.is_a?(code_argument.raw).tap { index += 1 }
    else
      code_element.truthy?.tap { index += 1 }
    end
  rescue Error::Next => e
    e.code_value.truhty?.tap { index += 1 }
  end

  self
end

#code_deep_duplicateObject



1052
1053
1054
# File 'lib/code/object/list.rb', line 1052

def code_deep_duplicate
  List.new(raw.dup.map(&:code_deep_duplicate))
end

#code_detect(argument = nil, **globals) ⇒ Object



598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/code/object/list.rb', line 598

def code_detect(argument = nil, **globals)
  code_argument = argument.to_code

  raw.detect.with_index do |code_element, index|
    if code_argument.is_a?(Function)
      code_argument.call(
        arguments: List.new([code_element, Integer.new(index), self]),
        **globals
      ).truthy?
    elsif code_argument.is_a?(Class)
      code_element.is_a?(code_argument.raw)
    else
      false
    end
  rescue Error::Next => e
    e.code_value
  end || Nothing.new
end

#code_each(argument = nil, **globals) ⇒ Object



617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/code/object/list.rb', line 617

def code_each(argument = nil, **globals)
  code_argument = argument.to_code

  raw.each.with_index do |code_element, index|
    if code_argument.is_a?(Function)
      code_argument.call(
        arguments: List.new([code_element, Integer.new(index), self]),
        **globals
      )
    elsif code_argument.is_a?(Class)
      code_argument.raw.new(code_element)
    end
  rescue Error::Next => e
    e.code_value
  end

  self
end

#code_fetch(key) ⇒ Object



1069
1070
1071
1072
1073
# File 'lib/code/object/list.rb', line 1069

def code_fetch(key)
  code_key = key.to_code.code_to_integer

  raw.fetch(code_key.raw, Nothing.new)
end

#code_first(value = nil) ⇒ Object



636
637
638
639
640
641
642
643
644
# File 'lib/code/object/list.rb', line 636

def code_first(value = nil)
  code_value = value.to_code

  if code_value.nothing?
    raw.first || Nothing.new
  else
    List.new(raw.first(code_value.raw))
  end
end

#code_flatten(level = nil) ⇒ Object



660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/code/object/list.rb', line 660

def code_flatten(level = nil)
  code_level = level.to_code
  code_level = Integer.new(-1) if code_level.nothing?
  level = code_level.raw

  List.new(
    raw.reduce([]) do |acc, code_element|
      if code_element.is_a?(List) && level != 0
        if level.positive?
          acc + code_element.code_flatten(level - 1).raw
        else
          acc + code_element.code_flatten(level).raw
        end
      else
        acc + [code_element]
      end
    end
  )
end

#code_get(argument) ⇒ Object



1056
1057
1058
1059
1060
# File 'lib/code/object/list.rb', line 1056

def code_get(argument)
  code_argument = argument.to_code

  raw[code_argument.raw] || Nothing.new
end

#code_include?(other) ⇒ Boolean

Returns:



701
702
703
704
705
# File 'lib/code/object/list.rb', line 701

def code_include?(other)
  code_other = other.to_code

  Boolean.new(raw.include?(code_other))
end

#code_join(separator = nil) ⇒ Object



984
985
986
987
988
# File 'lib/code/object/list.rb', line 984

def code_join(separator = nil)
  code_separator = separator.to_s.to_code

  String.new(raw.join(code_separator.raw))
end

#code_lastObject



707
708
709
# File 'lib/code/object/list.rb', line 707

def code_last
  raw.last || Nothing.new
end

#code_map(argument = nil, **globals) ⇒ Object



711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# File 'lib/code/object/list.rb', line 711

def code_map(argument = nil, **globals)
  code_argument = argument.to_code

  List.new(
    raw.map.with_index do |code_element, index|
      if code_argument.is_a?(Function)
        code_argument.call(
          arguments: List.new([code_element, Integer.new(index), self]),
          **globals
        )
      elsif code_argument.is_a?(Class)
        code_argument.raw.new(code_element)
      else
        Nothing.new
      end
    rescue Error::Next => e
      e.code_value
    end
  )
end

#code_map!(argument = nil, **globals) ⇒ Object



732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/code/object/list.rb', line 732

def code_map!(argument = nil, **globals)
  code_argument = argument.to_code

  raw.map!.with_index do |code_element, index|
    if code_argument.is_a?(Function)
      code_argument.call(
        arguments: List.new([code_element, Integer.new(index), self]),
        **globals
      )
    elsif code_argument.is_a?(Class)
      code_argument.raw.new(code_element)
    else
      Nothing.new
    end
  rescue Error::Next => e
    e.code_value
  end

  self
end

#code_max(argument = nil, **globals) ⇒ Object



753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# File 'lib/code/object/list.rb', line 753

def code_max(argument = nil, **globals)
  code_argument = argument.to_code

  raw.max_by.with_index do |code_element, index|
    if code_argument.is_a?(Function)
      code_argument.call(
        arguments: List.new([code_element, Integer.new(index), self]),
        **globals
      ).truthy?
    else
      code_element
    end
  rescue Error::Next => e
    e.code_value
  end || Nothing.new
end

#code_minus(other) ⇒ Object



592
593
594
595
596
# File 'lib/code/object/list.rb', line 592

def code_minus(other)
  code_other = other.to_code

  List.new(raw - code_other.raw)
end

#code_none?(argument = nil, **globals) ⇒ Boolean

Returns:



770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
# File 'lib/code/object/list.rb', line 770

def code_none?(argument = nil, **globals)
  code_argument = argument.to_code

  index = 0

  Boolean.new(
    raw.none? do |code_element|
      if code_argument.is_a?(Function)
        code_argument
          .call(
            arguments: List.new([code_element, Integer.new(index), self]),
            **globals
          )
          .truthy?
          .tap { index += 1 }
      elsif code_argument.is_a?(Class)
        code_element.is_a?(code_argument.raw).tap { index += 1 }
      else
        true.tap { index += 1 }
      end
    rescue Error::Next => e
      e.code_value.truthy?.tap { index += 1 }
    end
  )
end

#code_plus(other) ⇒ Object



586
587
588
589
590
# File 'lib/code/object/list.rb', line 586

def code_plus(other)
  code_other = other.to_code

  List.new(raw + code_other.raw)
end

#code_pop(n = nil) ⇒ Object



680
681
682
683
684
685
# File 'lib/code/object/list.rb', line 680

def code_pop(n = nil)
  code_n = n.to_code
  n = code_n.raw

  code_n.nothing? ? raw.dup.pop || Nothing.new : List.new(raw.dup.pop(n))
end

#code_pop!(n = nil) ⇒ Object



687
688
689
690
691
692
# File 'lib/code/object/list.rb', line 687

def code_pop!(n = nil)
  code_n = n.to_code
  n = code_n.raw

  code_n.nothing? ? raw.pop || Nothing.new : List.new(raw.pop(n))
end

#code_reduce(argument = nil, **globals) ⇒ Object



822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
# File 'lib/code/object/list.rb', line 822

def code_reduce(argument = nil, **globals)
  code_argument = argument.to_code

  index = 0

  raw.reduce do |code_acc, code_element|
    if code_argument.is_a?(Function)
      code_argument
        .call(
          arguments:
            List.new([code_acc, code_element, Integer.new(index), self]),
          **globals
        )
        .tap { index += 1 }
    else
      code_acc.tap { index += 1 }
    end
  rescue Error::Next => e
    e.code_value.tap { index += 1 }
  end || Nothing.new
end

#code_reject(argument = nil, **globals) ⇒ Object



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
# File 'lib/code/object/list.rb', line 942

def code_reject(argument = nil, **globals)
  code_argument = argument.to_code

  List.new(
    raw.reject.with_index do |code_element, index|
      if code_argument.is_a?(Function)
        code_argument.call(
          arguments: List.new([code_element, Integer.new(index), self]),
          **globals
        ).truthy?
      elsif code_argument.is_a?(Class)
        code_element.is_a?(code_argument.raw)
      else
        false
      end
    rescue Error::Next => e
      e.code_value.truthy?
    end
  )
end

#code_reject!(argument = nil, **globals) ⇒ Object



963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
# File 'lib/code/object/list.rb', line 963

def code_reject!(argument = nil, **globals)
  code_argument = argument.to_code

  raw.reject!.with_index do |code_element, index|
    if code_argument.is_a?(Function)
      code_argument.call(
        arguments: List.new([code_element, Integer.new(index), self]),
        **globals
      ).truthy?
    elsif code_argument.is_a?(Class)
      code_element.is_a?(code_argument.raw)
    else
      false
    end
  rescue Error::Next => e
    e.code_value.truthy?
  end

  self
end

#code_reverseObject



844
845
846
# File 'lib/code/object/list.rb', line 844

def code_reverse
  List.new(raw.reverse)
end

#code_sample(value = nil) ⇒ Object



646
647
648
649
650
651
652
653
654
# File 'lib/code/object/list.rb', line 646

def code_sample(value = nil)
  code_value = value.to_code

  if code_value.nothing?
    raw.sample || Nothing.new
  else
    List.new(raw.sample(code_value.raw))
  end
end

#code_select(argument = nil, **globals) ⇒ Object



900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
# File 'lib/code/object/list.rb', line 900

def code_select(argument = nil, **globals)
  code_argument = argument.to_code

  List.new(
    raw.select.with_index do |code_element, index|
      if code_argument.is_a?(Function)
        code_argument.call(
          arguments: List.new([code_element, Integer.new(index), self]),
          **globals
        ).truthy?
      elsif code_argument.is_a?(Class)
        code_element.is_a?(code_argument.raw)
      else
        false
      end
    rescue Error::Next => e
      e.code_value.truthy?
    end
  )
end

#code_select!(argument = nil, **globals) ⇒ Object



921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# File 'lib/code/object/list.rb', line 921

def code_select!(argument = nil, **globals)
  code_argument = argument.to_code

  raw.select!.with_index do |code_element, index|
    if code_argument.is_a?(Function)
      code_argument.call(
        arguments: List.new([code_element, Integer.new(index), self]),
        **globals
      ).truthy?
    elsif code_argument.is_a?(Class)
      code_element.is_a?(code_argument.raw)
    else
      false
    end
  rescue Error::Next => e
    e.code_value.truthy?
  end

  self
end

#code_set(key, value) ⇒ Object



1062
1063
1064
1065
1066
1067
# File 'lib/code/object/list.rb', line 1062

def code_set(key, value)
  code_key = key.to_code.code_to_integer
  code_value = value.to_code
  raw[code_key.raw] = code_value
  code_value
end

#code_shift(n = nil) ⇒ Object



694
695
696
697
698
699
# File 'lib/code/object/list.rb', line 694

def code_shift(n = nil)
  code_n = n.to_code
  n = code_n.raw

  code_n.nothing? ? raw.shift || Nothing.new : List.new(raw.shift(n))
end

#code_shuffleObject



656
657
658
# File 'lib/code/object/list.rb', line 656

def code_shuffle
  List.new(raw.shuffle)
end

#code_sizeObject



1009
1010
1011
# File 'lib/code/object/list.rb', line 1009

def code_size
  Integer.new(raw.size)
end

#code_sort(argument = nil, **globals) ⇒ Object



990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
# File 'lib/code/object/list.rb', line 990

def code_sort(argument = nil, **globals)
  code_argument = argument.to_code

  List.new(
    raw.sort_by.with_index do |code_element, index|
      if code_argument.is_a?(Function)
        code_argument.call(
          arguments: List.new([code_element, Integer.new(index), self]),
          **globals
        )
      else
        code_element
      end
    rescue Error::Next => e
      e.code_value
    end
  )
end

#code_sumObject



1048
1049
1050
# File 'lib/code/object/list.rb', line 1048

def code_sum
  raw.inject(&:code_plus) || Nothing.new
end

#code_uniq(argument = nil, **globals) ⇒ Object



1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
# File 'lib/code/object/list.rb', line 1013

def code_uniq(argument = nil, **globals)
  code_argument = argument.to_code

  unless code_argument.is_a?(Function) || code_argument.is_a?(Class)
    return List.new(raw.uniq)
  end

  if code_argument.is_a?(Class)
    return List.new(
      raw
        .select { |code_element| code_element.is_a?(code_argument.raw) }
        .uniq
    )
  end

  index = 0

  List.new(
    raw.uniq do |code_element|
      if code_argument.is_a?(Function)
        code_argument
          .call(
            arguments: List.new([code_element, Integer.new(index), self]),
            **globals
          )
          .tap { index += 1 }
      else
        code_element.tap { index += 1 }
      end
    rescue Error::Next => e
      e.code_value.tap { index += 1 }
    end
  )
end

#present?Boolean

Returns:



1079
1080
1081
# File 'lib/code/object/list.rb', line 1079

def present?
  raw.present?
end