code-ruby 3.1.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/bin/code +97 -20
  4. data/lib/code/concerns/shared.rb +331 -15
  5. data/lib/code/format.rb +22 -2
  6. data/lib/code/network.rb +87 -0
  7. data/lib/code/node/call.rb +79 -2
  8. data/lib/code/node/call_argument.rb +14 -0
  9. data/lib/code/node/code.rb +5 -4
  10. data/lib/code/node/function_parameter.rb +7 -4
  11. data/lib/code/node/list.rb +29 -1
  12. data/lib/code/node/while.rb +21 -0
  13. data/lib/code/object/base_64.rb +132 -6
  14. data/lib/code/object/boolean.rb +60 -0
  15. data/lib/code/object/class.rb +138 -2
  16. data/lib/code/object/code.rb +111 -3
  17. data/lib/code/object/context.rb +57 -1
  18. data/lib/code/object/cryptography.rb +63 -0
  19. data/lib/code/object/date.rb +13339 -462
  20. data/lib/code/object/decimal.rb +1725 -0
  21. data/lib/code/object/dictionary.rb +1835 -12
  22. data/lib/code/object/duration.rb +28 -0
  23. data/lib/code/object/function.rb +261 -23
  24. data/lib/code/object/global.rb +534 -1
  25. data/lib/code/object/html.rb +227 -16
  26. data/lib/code/object/http.rb +244 -14
  27. data/lib/code/object/ics.rb +75 -13
  28. data/lib/code/object/identifier_list.rb +17 -2
  29. data/lib/code/object/integer.rb +1941 -2
  30. data/lib/code/object/json.rb +75 -1
  31. data/lib/code/object/list.rb +3417 -10
  32. data/lib/code/object/nothing.rb +53 -0
  33. data/lib/code/object/number.rb +110 -0
  34. data/lib/code/object/parameter.rb +140 -0
  35. data/lib/code/object/range.rb +596 -14
  36. data/lib/code/object/smtp.rb +95 -12
  37. data/lib/code/object/string.rb +944 -3
  38. data/lib/code/object/super.rb +10 -1
  39. data/lib/code/object/time.rb +13358 -498
  40. data/lib/code/object/url.rb +65 -0
  41. data/lib/code/object.rb +543 -0
  42. data/lib/code/parser.rb +177 -26
  43. data/lib/code-ruby.rb +3 -0
  44. data/lib/code.rb +30 -3
  45. metadata +135 -84
  46. data/.github/dependabot.yml +0 -15
  47. data/.github/workflows/ci.yml +0 -38
  48. data/.gitignore +0 -30
  49. data/.node-version +0 -1
  50. data/.npm-version +0 -1
  51. data/.prettierignore +0 -2
  52. data/.rspec +0 -1
  53. data/.rubocop.yml +0 -140
  54. data/.ruby-version +0 -1
  55. data/.tool-versions +0 -3
  56. data/AGENTS.md +0 -43
  57. data/Gemfile +0 -22
  58. data/Gemfile.lock +0 -292
  59. data/Rakefile +0 -5
  60. data/bin/bundle +0 -123
  61. data/bin/bundle-audit +0 -31
  62. data/bin/bundler-audit +0 -31
  63. data/bin/dorian +0 -31
  64. data/bin/rspec +0 -31
  65. data/bin/rubocop +0 -31
  66. data/bin/test +0 -5
  67. data/code-ruby.gemspec +0 -34
  68. data/docs/precedence.txt +0 -36
  69. data/package-lock.json +0 -14
  70. data/package.json +0 -7
  71. data/spec/bin/code_spec.rb +0 -48
  72. data/spec/code/format_spec.rb +0 -153
  73. data/spec/code/node/call_spec.rb +0 -11
  74. data/spec/code/object/boolean_spec.rb +0 -18
  75. data/spec/code/object/cryptography_spec.rb +0 -25
  76. data/spec/code/object/decimal_spec.rb +0 -50
  77. data/spec/code/object/dictionary_spec.rb +0 -98
  78. data/spec/code/object/function_spec.rb +0 -268
  79. data/spec/code/object/http_spec.rb +0 -33
  80. data/spec/code/object/ics_spec.rb +0 -50
  81. data/spec/code/object/integer_spec.rb +0 -42
  82. data/spec/code/object/list_spec.rb +0 -22
  83. data/spec/code/object/nothing_spec.rb +0 -14
  84. data/spec/code/object/range_spec.rb +0 -23
  85. data/spec/code/object/string_spec.rb +0 -26
  86. data/spec/code/parser/boolean_spec.rb +0 -11
  87. data/spec/code/parser/chained_call_spec.rb +0 -16
  88. data/spec/code/parser/dictionary_spec.rb +0 -18
  89. data/spec/code/parser/function_spec.rb +0 -16
  90. data/spec/code/parser/group_spec.rb +0 -11
  91. data/spec/code/parser/if_modifier_spec.rb +0 -18
  92. data/spec/code/parser/list_spec.rb +0 -17
  93. data/spec/code/parser/number_spec.rb +0 -11
  94. data/spec/code/parser/string_spec.rb +0 -20
  95. data/spec/code/parser_spec.rb +0 -52
  96. data/spec/code/type_spec.rb +0 -21
  97. data/spec/code_spec.rb +0 -642
  98. data/spec/spec_helper.rb +0 -21
  99. data/spec/zeitwerk/loader_spec.rb +0 -7
@@ -3,6 +3,294 @@
3
3
  class Code
4
4
  class Object
5
5
  class Range < Object
6
+ CLASS_DOCUMENTATION = {
7
+ name: "Range",
8
+ description: "represents inclusive or exclusive sequences between comparable bounds.",
9
+ examples: [
10
+ "1..3",
11
+ "1...3",
12
+ "(1..3).to_list"
13
+ ]
14
+ }.freeze
15
+ INSTANCE_FUNCTIONS = {
16
+ "all?" => {
17
+ name: "all?",
18
+ description: "returns whether every item in the range matches a function.",
19
+ examples: [
20
+ "(1..3).all?((x) => { x > 0 })",
21
+ "(1..3).all?((x) => { x < 4 })",
22
+ "(1..3).all?((x) => { x < 3 })"
23
+ ]
24
+ },
25
+ "any?" => {
26
+ name: "any?",
27
+ description: "returns whether any item in the range matches a function.",
28
+ examples: [
29
+ "(1..3).any?((x) => { x == 2 })",
30
+ "(1..3).any?((x) => { x == 1 })",
31
+ "(1..3).any?((x) => { x > 3 })"
32
+ ]
33
+ },
34
+ "none?" => {
35
+ name: "none?",
36
+ description: "returns whether no items in the range match a function.",
37
+ examples: [
38
+ "(1..3).none?((x) => { x > 3 })",
39
+ "(1..3).none?((x) => { x == 2 })",
40
+ "(1..3).none?((x) => { x < 0 })"
41
+ ]
42
+ },
43
+ "each" => {
44
+ name: "each",
45
+ description: "calls a function for each item in the range and returns the range.",
46
+ examples: [
47
+ "(1..3).each((x) => { x })",
48
+ "(:a..:c).each((x) => { x })",
49
+ "(1...3).each((x) => { x })"
50
+ ]
51
+ },
52
+ "reverse_each" => {
53
+ name: "reverse_each",
54
+ description: "calls a function for each item in the range in reverse order.",
55
+ examples: [
56
+ "(1..3).reverse_each((x) => { x })",
57
+ "(:a..:c).reverse_each((x) => { x })",
58
+ "(1...3).reverse_each((x) => { x })"
59
+ ]
60
+ },
61
+ "include?" => {
62
+ name: "include?",
63
+ description: "returns whether the range includes a value.",
64
+ examples: [
65
+ "(1..3).include?(2)",
66
+ "(1..3).include?(4)",
67
+ "(:a..:c).include?(:b)"
68
+ ]
69
+ },
70
+ "member?" => {
71
+ name: "member?",
72
+ description: "returns whether the range includes a value.",
73
+ examples: [
74
+ "(1..3).member?(2)",
75
+ "(1..3).member?(4)",
76
+ "(:a..:c).member?(:b)"
77
+ ]
78
+ },
79
+ "cover?" => {
80
+ name: "cover?",
81
+ description: "returns whether a value is between the range bounds.",
82
+ examples: [
83
+ "(1..3).cover?(2)",
84
+ "(1..3).cover?(4)",
85
+ "(:a..:c).cover?(:b)"
86
+ ]
87
+ },
88
+ "overlap?" => {
89
+ name: "overlap?",
90
+ description: "returns whether the range overlaps another range.",
91
+ examples: [
92
+ "(1..3).overlap?(2..4)",
93
+ "(1..3).overlap?(4..6)",
94
+ "(:a..:c).overlap?(:b..:d)"
95
+ ]
96
+ },
97
+ "empty?" => {
98
+ name: "empty?",
99
+ description: "returns whether the range is empty.",
100
+ examples: [
101
+ "(1..3).empty?",
102
+ "(1...1).empty?",
103
+ "(:a..:c).empty?"
104
+ ]
105
+ },
106
+ "begin" => {
107
+ name: "begin",
108
+ description: "returns the starting bound of the range.",
109
+ examples: [
110
+ "(1..3).begin",
111
+ "(:a..:c).begin",
112
+ "(1...3).begin"
113
+ ]
114
+ },
115
+ "end" => {
116
+ name: "end",
117
+ description: "returns the ending bound of the range.",
118
+ examples: [
119
+ "(1..3).end",
120
+ "(:a..:c).end",
121
+ "(1...3).end"
122
+ ]
123
+ },
124
+ "exclude_end?" => {
125
+ name: "exclude_end?",
126
+ description: "returns whether the range excludes its ending bound.",
127
+ examples: [
128
+ "(1..3).exclude_end?",
129
+ "(1...3).exclude_end?",
130
+ "(:a...:c).exclude_end?"
131
+ ]
132
+ },
133
+ "first" => {
134
+ name: "first",
135
+ description: "returns the first item in the range.",
136
+ examples: [
137
+ "(1..3).first",
138
+ "(2..4).first",
139
+ "(:a..:c).first"
140
+ ]
141
+ },
142
+ "last" => {
143
+ name: "last",
144
+ description: "returns the last item in the range.",
145
+ examples: [
146
+ "(1..3).last",
147
+ "(2..4).last",
148
+ "(:a..:c).last"
149
+ ]
150
+ },
151
+ "minimum" => {
152
+ name: "minimum",
153
+ description: "returns the minimum item in the range.",
154
+ examples: [
155
+ "(1..3).minimum",
156
+ "(:a..:c).minimum",
157
+ "(3..1).minimum"
158
+ ]
159
+ },
160
+ "maximum" => {
161
+ name: "maximum",
162
+ description: "returns the maximum item in the range.",
163
+ examples: [
164
+ "(1..3).maximum",
165
+ "(:a..:c).maximum",
166
+ "(3..1).maximum"
167
+ ]
168
+ },
169
+ "minimum_maximum" => {
170
+ name: "minimum_maximum",
171
+ description: "returns the minimum and maximum items as a list.",
172
+ examples: [
173
+ "(1..3).minimum_maximum",
174
+ "(:a..:c).minimum_maximum",
175
+ "(3..1).minimum_maximum"
176
+ ]
177
+ },
178
+ "map" => {
179
+ name: "map",
180
+ description: "returns a list with each item transformed by a function.",
181
+ examples: [
182
+ "(1..3).map((x) => { x + 1 })",
183
+ "(1..3).map((x) => { x.to_string })",
184
+ "(:a..:c).map((x) => { x.to_string })"
185
+ ]
186
+ },
187
+ "select" => {
188
+ name: "select",
189
+ description: "returns a list of items matched by a function.",
190
+ examples: [
191
+ "(1..3).select((x) => { x > 1 })",
192
+ "(1..3).select((x) => { x < 3 })",
193
+ "(:a..:c).select((x) => { x > :a })"
194
+ ]
195
+ },
196
+ "reject" => {
197
+ name: "reject",
198
+ description: "returns a list of items not matched by a function.",
199
+ examples: [
200
+ "(1..3).reject((x) => { x > 1 })",
201
+ "(1..3).reject((x) => { x < 3 })",
202
+ "(:a..:c).reject((x) => { x > :a })"
203
+ ]
204
+ },
205
+ "reduce" => {
206
+ name: "reduce",
207
+ description: "combines items in the range with a function.",
208
+ examples: [
209
+ "(1..3).reduce((sum, x) => { sum + x })",
210
+ "(1..3).reduce((product, x) => { product * x })",
211
+ "(2..4).reduce((sum, x) => { sum + x })"
212
+ ]
213
+ },
214
+ "step" => {
215
+ name: "step",
216
+ description: "returns a list of items separated by a step size.",
217
+ examples: [
218
+ "(1..5).step(2)",
219
+ "(1..5).step(1)",
220
+ "(1...5).step(2)"
221
+ ]
222
+ },
223
+ "binary_search" => {
224
+ name: "binary_search",
225
+ description: "returns the first item matched by binary search.",
226
+ examples: [
227
+ "(1..10).binary_search((x) => { x >= 5 })",
228
+ "(1..10).binary_search((x) => { x > 10 })",
229
+ "(1..3).binary_search((x) => { x >= 2 })"
230
+ ]
231
+ },
232
+ "sample" => {
233
+ name: "sample",
234
+ description: "returns a random item from the range.",
235
+ examples: [
236
+ "(1..3).sample",
237
+ "(2..4).sample",
238
+ "(:a..:c).sample"
239
+ ]
240
+ },
241
+ "size" => {
242
+ name: "size",
243
+ description: "returns the number of items in the range.",
244
+ examples: [
245
+ "(1..3).size",
246
+ "(1...3).size",
247
+ "(:a..:c).size"
248
+ ]
249
+ },
250
+ "count" => {
251
+ name: "count",
252
+ description: "returns the number of items, optionally matched by a function.",
253
+ examples: [
254
+ "(1..3).count",
255
+ "(1...3).count",
256
+ "(1..5).count((x) => { x.even? })"
257
+ ]
258
+ },
259
+ "to_list" => {
260
+ name: "to_list",
261
+ description: "returns the range items as a list.",
262
+ examples: [
263
+ "(1..3).to_list",
264
+ "(1...3).to_list",
265
+ "(:a..:c).to_list"
266
+ ]
267
+ },
268
+ "entries" => {
269
+ name: "entries",
270
+ description: "returns the range items as a list.",
271
+ examples: [
272
+ "(1..3).entries",
273
+ "(1...3).entries",
274
+ "(:a..:c).entries"
275
+ ]
276
+ },
277
+ "to_dictionary" => {
278
+ name: "to_dictionary",
279
+ description: "returns a dictionary built from indexed range items.",
280
+ examples: [
281
+ "(1..3).to_dictionary",
282
+ "(1...3).to_dictionary",
283
+ "(:a..:c).to_dictionary"
284
+ ]
285
+ }
286
+ }.freeze
287
+
288
+ def self.function_documentation(scope)
289
+ return INSTANCE_FUNCTIONS if scope == :instance
290
+
291
+ {}
292
+ end
293
+
6
294
  attr_reader :code_left, :code_right, :code_options, :code_exclude_end
7
295
 
8
296
  def initialize(*args, **kargs, &_block)
@@ -47,30 +335,90 @@ class Code
47
335
  when "any?"
48
336
  sig(args) { Function }
49
337
  code_any?(code_value, **globals)
338
+ when "none?"
339
+ sig(args) { Function }
340
+ code_none?(code_value, **globals)
50
341
  when "each"
51
342
  sig(args) { Function }
52
343
  code_each(code_value, **globals)
344
+ when "reverse_each"
345
+ sig(args) { Function }
346
+ code_reverse_each(code_value, **globals)
347
+ when "include?"
348
+ sig(args) { Object }
349
+ code_include?(code_value)
350
+ when "member?"
351
+ sig(args) { Object }
352
+ code_member?(code_value)
353
+ when "cover?"
354
+ sig(args) { Object }
355
+ code_cover?(code_value)
356
+ when "overlap?"
357
+ sig(args) { Range }
358
+ code_overlap?(code_value)
359
+ when "empty?"
360
+ sig(args)
361
+ code_empty?
362
+ when "begin"
363
+ sig(args)
364
+ code_begin
365
+ when "end"
366
+ sig(args)
367
+ code_end
368
+ when "exclude_end?"
369
+ sig(args)
370
+ code_exclude_end?
53
371
  when "first"
54
372
  sig(args)
55
373
  code_first
56
374
  when "last"
57
375
  sig(args)
58
376
  code_last
377
+ when "minimum"
378
+ sig(args)
379
+ code_minimum
380
+ when "maximum"
381
+ sig(args)
382
+ code_maximum
383
+ when "minimum_maximum"
384
+ sig(args)
385
+ code_minimum_maximum
59
386
  when "map"
60
387
  sig(args) { Function }
61
388
  code_map(code_value, **globals)
62
389
  when "select"
63
390
  sig(args) { Function }
64
391
  code_select(code_value, **globals)
392
+ when "reject"
393
+ sig(args) { Function }
394
+ code_reject(code_value, **globals)
395
+ when "reduce"
396
+ sig(args) { Function }
397
+ code_reduce(code_value, **globals)
65
398
  when "step"
66
- sig(args) { Integer | Decimal }
67
- code_step(code_value)
399
+ sig(args) { [(Integer | Decimal).maybe, Function.maybe] }
400
+ code_step(*code_arguments.raw, **globals)
401
+ when "binary_search"
402
+ sig(args) { Function }
403
+ code_binary_search(code_value, **globals)
68
404
  when "sample"
69
405
  sig(args)
70
406
  code_sample
407
+ when "size"
408
+ sig(args)
409
+ code_size
410
+ when "count"
411
+ sig(args) { Function.maybe }
412
+ code_count(code_value, **globals)
71
413
  when "to_list"
72
414
  sig(args)
73
415
  code_to_list
416
+ when "entries"
417
+ sig(args)
418
+ code_entries
419
+ when "to_dictionary"
420
+ sig(args)
421
+ code_to_dictionary
74
422
  else
75
423
  super
76
424
  end
@@ -90,8 +438,12 @@ class Code
90
438
  )
91
439
  .truthy?
92
440
  .tap { index += 1 }
441
+ rescue Error::Next => e
442
+ e.code_value.truthy?.tap { index += 1 }
93
443
  end
94
444
  )
445
+ rescue Error::Break => e
446
+ e.code_value
95
447
  end
96
448
 
97
449
  def code_any?(argument, **globals)
@@ -108,8 +460,34 @@ class Code
108
460
  )
109
461
  .truthy?
110
462
  .tap { index += 1 }
463
+ rescue Error::Next => e
464
+ e.code_value.truthy?.tap { index += 1 }
111
465
  end
112
466
  )
467
+ rescue Error::Break => e
468
+ e.code_value
469
+ end
470
+
471
+ def code_none?(argument, **globals)
472
+ code_argument = argument.to_code
473
+
474
+ index = 0
475
+
476
+ Boolean.new(
477
+ raw.none? do |code_element|
478
+ code_argument
479
+ .call(
480
+ arguments: List.new([code_element, Integer.new(index), self]),
481
+ **globals
482
+ )
483
+ .truthy?
484
+ .tap { index += 1 }
485
+ rescue Error::Next => e
486
+ e.code_value.truthy?.tap { index += 1 }
487
+ end
488
+ )
489
+ rescue Error::Break => e
490
+ e.code_value
113
491
  end
114
492
 
115
493
  def code_each(argument, **globals)
@@ -120,9 +498,66 @@ class Code
120
498
  arguments: List.new([code_element, Integer.new(index), self]),
121
499
  **globals
122
500
  )
501
+ rescue Error::Next => e
502
+ e.code_value
123
503
  end
124
504
 
125
505
  self
506
+ rescue Error::Break => e
507
+ e.code_value
508
+ end
509
+
510
+ def code_reverse_each(argument, **globals)
511
+ code_argument = argument.to_code
512
+
513
+ raw.reverse_each.with_index do |code_element, index|
514
+ code_argument.call(
515
+ arguments: List.new([code_element, Integer.new(index), self]),
516
+ **globals
517
+ )
518
+ rescue Error::Next => e
519
+ e.code_value
520
+ end
521
+
522
+ self
523
+ rescue Error::Break => e
524
+ e.code_value
525
+ end
526
+
527
+ def code_include?(value)
528
+ code_value = value.to_code
529
+ Boolean.new(raw.include?(code_value))
530
+ end
531
+
532
+ def code_member?(value)
533
+ code_include?(value)
534
+ end
535
+
536
+ def code_cover?(value)
537
+ code_value = value.to_code
538
+ Boolean.new(raw.cover?(code_value))
539
+ end
540
+
541
+ def code_overlap?(range)
542
+ code_range = range.to_code
543
+
544
+ Boolean.new(raw.overlap?(code_range.raw))
545
+ end
546
+
547
+ def code_empty?
548
+ Boolean.new(raw.to_a.empty?)
549
+ end
550
+
551
+ def code_begin
552
+ code_left
553
+ end
554
+
555
+ def code_end
556
+ code_right
557
+ end
558
+
559
+ def code_exclude_end?
560
+ Boolean.new(exclude_end?)
126
561
  end
127
562
 
128
563
  def code_first
@@ -133,6 +568,18 @@ class Code
133
568
  raw.last || Nothing.new
134
569
  end
135
570
 
571
+ def code_minimum
572
+ raw.min || Nothing.new
573
+ end
574
+
575
+ def code_maximum
576
+ raw.max || Nothing.new
577
+ end
578
+
579
+ def code_minimum_maximum
580
+ List.new([code_minimum, code_maximum])
581
+ end
582
+
136
583
  def code_map(argument, **globals)
137
584
  code_argument = argument.to_code
138
585
 
@@ -142,8 +589,12 @@ class Code
142
589
  arguments: List.new([code_element, Integer.new(index), self]),
143
590
  **globals
144
591
  )
592
+ rescue Error::Next => e
593
+ e.code_value
145
594
  end
146
595
  )
596
+ rescue Error::Break => e
597
+ e.code_value
147
598
  end
148
599
 
149
600
  def code_select(argument, **globals)
@@ -155,45 +606,176 @@ class Code
155
606
  arguments: List.new([code_element, Integer.new(index), self]),
156
607
  **globals
157
608
  ).truthy?
609
+ rescue Error::Next => e
610
+ e.code_value.truthy?
158
611
  end
159
612
  )
613
+ rescue Error::Break => e
614
+ e.code_value
615
+ end
616
+
617
+ def code_reject(argument, **globals)
618
+ code_argument = argument.to_code
619
+
620
+ List.new(
621
+ raw.reject.with_index do |code_element, index|
622
+ code_argument.call(
623
+ arguments: List.new([code_element, Integer.new(index), self]),
624
+ **globals
625
+ ).truthy?
626
+ rescue Error::Next => e
627
+ e.code_value.truthy?
628
+ end
629
+ )
630
+ rescue Error::Break => e
631
+ e.code_value
632
+ end
633
+
634
+ def code_reduce(argument, **globals)
635
+ code_argument = argument.to_code
636
+
637
+ index = 0
638
+
639
+ raw.reduce do |code_acc, code_element|
640
+ code_argument
641
+ .call(
642
+ arguments:
643
+ List.new([code_acc, code_element, Integer.new(index), self]),
644
+ **globals
645
+ )
646
+ .tap { index += 1 }
647
+ rescue Error::Next => e
648
+ e.code_value.tap { index += 1 }
649
+ end || Nothing.new
650
+ rescue Error::Break => e
651
+ e.code_value
160
652
  end
161
653
 
162
654
  def exclude_end?
163
655
  code_exclude_end.truthy?
164
656
  end
165
657
 
166
- def code_step(argument)
658
+ def code_binary_search(argument, **globals)
167
659
  code_argument = argument.to_code
660
+ values = raw.to_a
661
+ lower = 0
662
+ upper = values.length - 1
663
+ result = nil
664
+
665
+ while lower <= upper
666
+ index = (lower + upper) / 2
667
+ code_element = values[index]
668
+ matched =
669
+ begin
670
+ code_argument.call(
671
+ arguments: List.new([code_element, Integer.new(index), self]),
672
+ **globals
673
+ ).truthy?
674
+ rescue Error::Next => e
675
+ e.code_value.truthy?
676
+ end
677
+
678
+ if matched
679
+ result = code_element
680
+ upper = index - 1
681
+ else
682
+ lower = index + 1
683
+ end
684
+ end
685
+
686
+ result || Nothing.new
687
+ rescue Error::Break => e
688
+ e.code_value
689
+ end
690
+
691
+ def code_step(argument = nil, function = nil, **globals)
692
+ code_argument = argument.to_code
693
+ code_function = function.to_code
694
+
695
+ if code_argument.is_a?(Function)
696
+ code_function = code_argument
697
+ code_argument = Integer.new(1)
698
+ elsif code_argument.nothing?
699
+ code_argument = Integer.new(1)
700
+ end
168
701
 
169
702
  code_list = List.new
170
703
  code_element = code_left
171
- code_list.code_append(code_element)
704
+ index = 0
705
+ step_is_positive = code_argument.code_greater(Integer.new(0)).truthy?
172
706
 
173
- code_element = code_element.code_plus(code_argument)
707
+ loop do
708
+ comparison =
709
+ if step_is_positive
710
+ exclude_end? ? code_element.code_less(code_right) : code_element.code_less_or_equal(code_right)
711
+ else
712
+ exclude_end? ? code_element.code_greater(code_right) : code_element.code_greater_or_equal(code_right)
713
+ end
174
714
 
175
- if exclude_end?
176
- while code_element.code_less(code_right).truthy?
177
- code_list.code_append(code_element)
178
- code_element = code_element.code_plus(code_argument)
179
- end
180
- else
181
- while code_element.code_less_or_equal(code_right).truthy?
715
+ break unless comparison.truthy?
716
+
717
+ if code_function.is_a?(Function)
718
+ code_function.call(
719
+ arguments: List.new([code_element, Integer.new(index), self]),
720
+ **globals
721
+ )
722
+ else
182
723
  code_list.code_append(code_element)
183
- code_element = code_element.code_plus(code_argument)
184
724
  end
725
+
726
+ index += 1
727
+ code_element = code_element.code_plus(code_argument)
185
728
  end
186
729
 
187
- code_list
730
+ code_function.is_a?(Function) ? self : code_list
731
+ rescue Error::Break => e
732
+ e.code_value
188
733
  end
189
734
 
190
735
  def code_to_list
191
736
  List.new(raw.to_a)
192
737
  end
193
738
 
739
+ def code_entries
740
+ code_to_list
741
+ end
742
+
743
+ def code_to_dictionary
744
+ code_to_list.code_to_dictionary
745
+ end
746
+
194
747
  def code_sample
195
748
  code_to_list.code_sample
196
749
  end
750
+
751
+ def code_size
752
+ Integer.new(raw.to_a.size)
753
+ end
754
+
755
+ def code_count(argument = nil, **globals)
756
+ code_argument = argument.to_code
757
+
758
+ if code_argument.nothing?
759
+ return Integer.new(raw.to_a.size)
760
+ end
761
+
762
+ index = 0
763
+ Integer.new(
764
+ raw.count do |code_element|
765
+ code_argument
766
+ .call(
767
+ arguments: List.new([code_element, Integer.new(index), self]),
768
+ **globals
769
+ )
770
+ .truthy?
771
+ .tap { index += 1 }
772
+ rescue Error::Next => e
773
+ e.code_value.truthy?.tap { index += 1 }
774
+ end
775
+ )
776
+ rescue Error::Break => e
777
+ e.code_value
778
+ end
197
779
  end
198
780
  end
199
781
  end