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, #code_and, #code_as_json, #code_different, #code_duplicate, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_falsy?, code_fetch, code_get, #code_inclusive_range, #code_inspect, #code_methods, #code_name, #code_nothing?, #code_or, #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.



19
20
21
22
23
24
25
26
27
28
# File 'lib/code/object/list.rb', line 19

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

Instance Method Details

#any?Boolean

Returns:



547
548
549
# File 'lib/code/object/list.rb', line 547

def any?
  code_any?.truthy?
end

#call(**args) ⇒ Object



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

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)
    code_sort
  when "<<", "append"
    sig(args) { Object }
    code_append(code_value)
  when "-", "minus"
    sig(args) { List }
    code_minus(code_value)
  when "any?"
    sig(args) { Function.maybe }
    code_any?(code_value, **globals)
  when "detect"
    sig(args) { Function }
    code_detect(code_value, **globals)
  when "each"
    sig(args) { Function }
    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 "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 }
    code_map(code_value, **globals)
  when "map!"
    sig(args) { Function }
    code_map!(code_value, **globals)
  when "max"
    sig(args)
    code_max
  when "max_by"
    sig(args) { Function }
    code_max_by(code_value, **globals)
  when "none?"
    sig(args) { Function.maybe }
    code_none?(code_value, **globals)
  when "reduce"
    sig(args) { Function }
    code_reduce(code_value, **globals)
  when "reverse"
    sig(args)
    code_reverse
  when "select"
    sig(args) { Function }
    code_select(code_value, **globals)
  when "select!"
    sig(args) { Function }
    code_select!(code_value, **globals)
  when "compact"
    sig(args) { Function.maybe }
    code_compact(code_value, **globals)
  when "compact!"
    sig(args) { Function.maybe }
    code_compact!(code_value, **globals)
  when "reject"
    sig(args) { Function }
    code_reject(code_value, **globals)
  when "reject!"
    sig(args) { Function }
    code_reject!(code_value, **globals)
  when "size"
    sig(args)
    code_size
  when "sum"
    sig(args)
    code_sum
  when "uniq"
    sig(args)
    code_uniq
  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 "many?"
    sig(args)
    code_many?
  else
    super
  end
end

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

Returns:



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/code/object/list.rb', line 171

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

  index = 0

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

#code_append(other) ⇒ Object



195
196
197
198
199
200
201
# File 'lib/code/object/list.rb', line 195

def code_append(other)
  code_other = other.to_code

  raw << code_other

  self
end

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



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/code/object/list.rb', line 394

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

  index = 0

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

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



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/code/object/list.rb', line 418

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

  index = 0

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

  self
end

#code_deep_duplicateObject



524
525
526
# File 'lib/code/object/list.rb', line 524

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

#code_detect(argument, **globals) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/code/object/list.rb', line 209

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

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

#code_each(argument, **globals) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/code/object/list.rb', line 222

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

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

  self
end

#code_fetch(key) ⇒ Object



541
542
543
544
545
# File 'lib/code/object/list.rb', line 541

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



237
238
239
240
241
242
243
244
245
# File 'lib/code/object/list.rb', line 237

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



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/code/object/list.rb', line 261

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



528
529
530
531
532
# File 'lib/code/object/list.rb', line 528

def code_get(argument)
  code_argument = argument.to_code

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

#code_include?(other) ⇒ Boolean

Returns:



295
296
297
298
299
# File 'lib/code/object/list.rb', line 295

def code_include?(other)
  code_other = other.to_code

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

#code_join(separator = nil) ⇒ Object



502
503
504
505
506
# File 'lib/code/object/list.rb', line 502

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

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

#code_lastObject



301
302
303
# File 'lib/code/object/list.rb', line 301

def code_last
  raw.last || Nothing.new
end

#code_map(argument, **globals) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/code/object/list.rb', line 305

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

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

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



320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/code/object/list.rb', line 320

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

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

  self
end

#code_maxObject



335
336
337
# File 'lib/code/object/list.rb', line 335

def code_max
  raw.max || Nothing.new
end

#code_max_by(argument, **globals) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/code/object/list.rb', line 339

def code_max_by(argument, **globals)
  code_argument = argument.to_code

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

#code_minus(other) ⇒ Object



203
204
205
206
207
# File 'lib/code/object/list.rb', line 203

def code_minus(other)
  code_other = other.to_code

  List.new(raw - code_other.raw)
end

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

Returns:



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/code/object/list.rb', line 352

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

  index = 0

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

#code_pop(n = nil) ⇒ Object



281
282
283
284
285
286
# File 'lib/code/object/list.rb', line 281

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, **globals) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/code/object/list.rb', line 376

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

  raw.reduce.with_index do |code_acc, code_element, index|
    code_argument.call(
      arguments:
        List.new([code_acc, code_element, Integer.new(index), self]),
      **globals
    )
  rescue Error::Next => e
    e.code_value
  end || Nothing.new
end

#code_reject(argument, **globals) ⇒ Object



472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/code/object/list.rb', line 472

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

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

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



487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/code/object/list.rb', line 487

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

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

  self
end

#code_reverseObject



390
391
392
# File 'lib/code/object/list.rb', line 390

def code_reverse
  List.new(raw.reverse)
end

#code_sample(value = nil) ⇒ Object



247
248
249
250
251
252
253
254
255
# File 'lib/code/object/list.rb', line 247

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, **globals) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/code/object/list.rb', line 442

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

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

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



457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/code/object/list.rb', line 457

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

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

  self
end

#code_set(key, value) ⇒ Object



534
535
536
537
538
539
# File 'lib/code/object/list.rb', line 534

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



288
289
290
291
292
293
# File 'lib/code/object/list.rb', line 288

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



257
258
259
# File 'lib/code/object/list.rb', line 257

def code_shuffle
  List.new(raw.shuffle)
end

#code_sizeObject



512
513
514
# File 'lib/code/object/list.rb', line 512

def code_size
  Integer.new(raw.size)
end

#code_sortObject



508
509
510
# File 'lib/code/object/list.rb', line 508

def code_sort
  List.new(raw.sort)
end

#code_sumObject



520
521
522
# File 'lib/code/object/list.rb', line 520

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

#code_uniqObject



516
517
518
# File 'lib/code/object/list.rb', line 516

def code_uniq
  List.new(raw.uniq)
end