code-ruby 3.1.2 → 4.0.1
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.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/bin/code +100 -20
- data/lib/code/concerns/shared.rb +335 -15
- data/lib/code/format.rb +33 -15
- data/lib/code/network.rb +82 -0
- data/lib/code/node/call.rb +80 -2
- data/lib/code/node/call_argument.rb +14 -0
- data/lib/code/node/code.rb +4 -3
- data/lib/code/node/function_parameter.rb +7 -4
- data/lib/code/node/list.rb +32 -2
- data/lib/code/node/square_bracket.rb +4 -2
- data/lib/code/object/base_64.rb +132 -6
- data/lib/code/object/boolean.rb +56 -0
- data/lib/code/object/class.rb +143 -2
- data/lib/code/object/code.rb +108 -7
- data/lib/code/object/context.rb +59 -1
- data/lib/code/object/cryptography.rb +69 -0
- data/lib/code/object/date.rb +13800 -462
- data/lib/code/object/decimal.rb +1098 -0
- data/lib/code/object/dictionary.rb +1861 -11
- data/lib/code/object/duration.rb +24 -0
- data/lib/code/object/function.rb +289 -27
- data/lib/code/object/global.rb +447 -1
- data/lib/code/object/html.rb +181 -7
- data/lib/code/object/http.rb +253 -17
- data/lib/code/object/ics.rb +76 -13
- data/lib/code/object/identifier_list.rb +30 -10
- data/lib/code/object/integer.rb +1265 -2
- data/lib/code/object/json.rb +80 -1
- data/lib/code/object/list.rb +3371 -10
- data/lib/code/object/nothing.rb +53 -0
- data/lib/code/object/number.rb +120 -0
- data/lib/code/object/parameter.rb +149 -0
- data/lib/code/object/range.rb +530 -14
- data/lib/code/object/smtp.rb +103 -12
- data/lib/code/object/string.rb +968 -3
- data/lib/code/object/super.rb +11 -1
- data/lib/code/object/time.rb +13932 -498
- data/lib/code/object/url.rb +67 -0
- data/lib/code/object.rb +582 -0
- data/lib/code/parser.rb +194 -55
- data/lib/code-ruby.rb +3 -0
- data/lib/code.rb +30 -3
- metadata +135 -84
- data/.github/dependabot.yml +0 -15
- data/.github/workflows/ci.yml +0 -38
- data/.gitignore +0 -30
- data/.node-version +0 -1
- data/.npm-version +0 -1
- data/.prettierignore +0 -2
- data/.rspec +0 -1
- data/.rubocop.yml +0 -140
- data/.ruby-version +0 -1
- data/.tool-versions +0 -3
- data/AGENTS.md +0 -43
- data/Gemfile +0 -22
- data/Gemfile.lock +0 -292
- data/Rakefile +0 -5
- data/bin/bundle +0 -123
- data/bin/bundle-audit +0 -31
- data/bin/bundler-audit +0 -31
- data/bin/dorian +0 -31
- data/bin/rspec +0 -31
- data/bin/rubocop +0 -31
- data/bin/test +0 -5
- data/code-ruby.gemspec +0 -34
- data/docs/precedence.txt +0 -36
- data/package-lock.json +0 -14
- data/package.json +0 -7
- data/spec/bin/code_spec.rb +0 -48
- data/spec/code/format_spec.rb +0 -153
- data/spec/code/node/call_spec.rb +0 -11
- data/spec/code/object/boolean_spec.rb +0 -18
- data/spec/code/object/cryptography_spec.rb +0 -25
- data/spec/code/object/decimal_spec.rb +0 -50
- data/spec/code/object/dictionary_spec.rb +0 -98
- data/spec/code/object/function_spec.rb +0 -268
- data/spec/code/object/http_spec.rb +0 -33
- data/spec/code/object/ics_spec.rb +0 -50
- data/spec/code/object/integer_spec.rb +0 -42
- data/spec/code/object/list_spec.rb +0 -22
- data/spec/code/object/nothing_spec.rb +0 -14
- data/spec/code/object/range_spec.rb +0 -23
- data/spec/code/object/string_spec.rb +0 -26
- data/spec/code/parser/boolean_spec.rb +0 -11
- data/spec/code/parser/chained_call_spec.rb +0 -16
- data/spec/code/parser/dictionary_spec.rb +0 -18
- data/spec/code/parser/function_spec.rb +0 -16
- data/spec/code/parser/group_spec.rb +0 -11
- data/spec/code/parser/if_modifier_spec.rb +0 -18
- data/spec/code/parser/list_spec.rb +0 -17
- data/spec/code/parser/number_spec.rb +0 -11
- data/spec/code/parser/string_spec.rb +0 -20
- data/spec/code/parser_spec.rb +0 -52
- data/spec/code/type_spec.rb +0 -21
- data/spec/code_spec.rb +0 -717
- data/spec/spec_helper.rb +0 -21
- data/spec/zeitwerk/loader_spec.rb +0 -7
data/lib/code/object/range.rb
CHANGED
|
@@ -3,6 +3,242 @@
|
|
|
3
3
|
class Code
|
|
4
4
|
class Object
|
|
5
5
|
class Range < Object
|
|
6
|
+
CLASS_DOCUMENTATION = {
|
|
7
|
+
name: "Range",
|
|
8
|
+
description:
|
|
9
|
+
"represents inclusive or exclusive sequences between comparable bounds.",
|
|
10
|
+
examples: %w[1..3 1...3 (1..3).to_list]
|
|
11
|
+
}.freeze
|
|
12
|
+
INSTANCE_FUNCTIONS = {
|
|
13
|
+
"all?" => {
|
|
14
|
+
name: "all?",
|
|
15
|
+
description:
|
|
16
|
+
"returns whether every item in the range matches a function.",
|
|
17
|
+
examples: [
|
|
18
|
+
"(1..3).all?((x) => { x > 0 })",
|
|
19
|
+
"(1..3).all?((x) => { x < 4 })",
|
|
20
|
+
"(1..3).all?((x) => { x < 3 })"
|
|
21
|
+
]
|
|
22
|
+
},
|
|
23
|
+
"any?" => {
|
|
24
|
+
name: "any?",
|
|
25
|
+
description:
|
|
26
|
+
"returns whether any item in the range matches a function.",
|
|
27
|
+
examples: [
|
|
28
|
+
"(1..3).any?((x) => { x == 2 })",
|
|
29
|
+
"(1..3).any?((x) => { x == 1 })",
|
|
30
|
+
"(1..3).any?((x) => { x > 3 })"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"none?" => {
|
|
34
|
+
name: "none?",
|
|
35
|
+
description:
|
|
36
|
+
"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:
|
|
46
|
+
"calls a function for each item in the range and returns the range.",
|
|
47
|
+
examples: [
|
|
48
|
+
"(1..3).each((x) => { x })",
|
|
49
|
+
"(:a..:c).each((x) => { x })",
|
|
50
|
+
"(1...3).each((x) => { x })"
|
|
51
|
+
]
|
|
52
|
+
},
|
|
53
|
+
"reverse_each" => {
|
|
54
|
+
name: "reverse_each",
|
|
55
|
+
description:
|
|
56
|
+
"calls a function for each item in the range in reverse order.",
|
|
57
|
+
examples: [
|
|
58
|
+
"(1..3).reverse_each((x) => { x })",
|
|
59
|
+
"(:a..:c).reverse_each((x) => { x })",
|
|
60
|
+
"(1...3).reverse_each((x) => { x })"
|
|
61
|
+
]
|
|
62
|
+
},
|
|
63
|
+
"include?" => {
|
|
64
|
+
name: "include?",
|
|
65
|
+
description: "returns whether the range includes a value.",
|
|
66
|
+
examples: %w[
|
|
67
|
+
(1..3).include?(2)
|
|
68
|
+
(1..3).include?(4)
|
|
69
|
+
(:a..:c).include?(:b)
|
|
70
|
+
]
|
|
71
|
+
},
|
|
72
|
+
"member?" => {
|
|
73
|
+
name: "member?",
|
|
74
|
+
description: "returns whether the range includes a value.",
|
|
75
|
+
examples: %w[(1..3).member?(2) (1..3).member?(4) (:a..:c).member?(:b)]
|
|
76
|
+
},
|
|
77
|
+
"cover?" => {
|
|
78
|
+
name: "cover?",
|
|
79
|
+
description: "returns whether a value is between the range bounds.",
|
|
80
|
+
examples: %w[(1..3).cover?(2) (1..3).cover?(4) (:a..:c).cover?(:b)]
|
|
81
|
+
},
|
|
82
|
+
"overlap?" => {
|
|
83
|
+
name: "overlap?",
|
|
84
|
+
description: "returns whether the range overlaps another range.",
|
|
85
|
+
examples: %w[
|
|
86
|
+
(1..3).overlap?(2..4)
|
|
87
|
+
(1..3).overlap?(4..6)
|
|
88
|
+
(:a..:c).overlap?(:b..:d)
|
|
89
|
+
]
|
|
90
|
+
},
|
|
91
|
+
"empty?" => {
|
|
92
|
+
name: "empty?",
|
|
93
|
+
description: "returns whether the range is empty.",
|
|
94
|
+
examples: %w[(1..3).empty? (1...1).empty? (:a..:c).empty?]
|
|
95
|
+
},
|
|
96
|
+
"begin" => {
|
|
97
|
+
name: "begin",
|
|
98
|
+
description: "returns the starting bound of the range.",
|
|
99
|
+
examples: %w[(1..3).begin (:a..:c).begin (1...3).begin]
|
|
100
|
+
},
|
|
101
|
+
"end" => {
|
|
102
|
+
name: "end",
|
|
103
|
+
description: "returns the ending bound of the range.",
|
|
104
|
+
examples: %w[(1..3).end (:a..:c).end (1...3).end]
|
|
105
|
+
},
|
|
106
|
+
"exclude_end?" => {
|
|
107
|
+
name: "exclude_end?",
|
|
108
|
+
description: "returns whether the range excludes its ending bound.",
|
|
109
|
+
examples: %w[
|
|
110
|
+
(1..3).exclude_end?
|
|
111
|
+
(1...3).exclude_end?
|
|
112
|
+
(:a...:c).exclude_end?
|
|
113
|
+
]
|
|
114
|
+
},
|
|
115
|
+
"first" => {
|
|
116
|
+
name: "first",
|
|
117
|
+
description: "returns the first item in the range.",
|
|
118
|
+
examples: %w[(1..3).first (2..4).first (:a..:c).first]
|
|
119
|
+
},
|
|
120
|
+
"last" => {
|
|
121
|
+
name: "last",
|
|
122
|
+
description: "returns the last item in the range.",
|
|
123
|
+
examples: %w[(1..3).last (2..4).last (:a..:c).last]
|
|
124
|
+
},
|
|
125
|
+
"minimum" => {
|
|
126
|
+
name: "minimum",
|
|
127
|
+
description: "returns the minimum item in the range.",
|
|
128
|
+
examples: %w[(1..3).minimum (:a..:c).minimum (3..1).minimum]
|
|
129
|
+
},
|
|
130
|
+
"maximum" => {
|
|
131
|
+
name: "maximum",
|
|
132
|
+
description: "returns the maximum item in the range.",
|
|
133
|
+
examples: %w[(1..3).maximum (:a..:c).maximum (3..1).maximum]
|
|
134
|
+
},
|
|
135
|
+
"minimum_maximum" => {
|
|
136
|
+
name: "minimum_maximum",
|
|
137
|
+
description: "returns the minimum and maximum items as a list.",
|
|
138
|
+
examples: %w[
|
|
139
|
+
(1..3).minimum_maximum
|
|
140
|
+
(:a..:c).minimum_maximum
|
|
141
|
+
(3..1).minimum_maximum
|
|
142
|
+
]
|
|
143
|
+
},
|
|
144
|
+
"map" => {
|
|
145
|
+
name: "map",
|
|
146
|
+
description:
|
|
147
|
+
"returns a list with each item transformed by a function.",
|
|
148
|
+
examples: [
|
|
149
|
+
"(1..3).map((x) => { x + 1 })",
|
|
150
|
+
"(1..3).map((x) => { x.to_string })",
|
|
151
|
+
"(:a..:c).map((x) => { x.to_string })"
|
|
152
|
+
]
|
|
153
|
+
},
|
|
154
|
+
"select" => {
|
|
155
|
+
name: "select",
|
|
156
|
+
description: "returns a list of items matched by a function.",
|
|
157
|
+
examples: [
|
|
158
|
+
"(1..3).select((x) => { x > 1 })",
|
|
159
|
+
"(1..3).select((x) => { x < 3 })",
|
|
160
|
+
"(:a..:c).select((x) => { x > :a })"
|
|
161
|
+
]
|
|
162
|
+
},
|
|
163
|
+
"reject" => {
|
|
164
|
+
name: "reject",
|
|
165
|
+
description: "returns a list of items not matched by a function.",
|
|
166
|
+
examples: [
|
|
167
|
+
"(1..3).reject((x) => { x > 1 })",
|
|
168
|
+
"(1..3).reject((x) => { x < 3 })",
|
|
169
|
+
"(:a..:c).reject((x) => { x > :a })"
|
|
170
|
+
]
|
|
171
|
+
},
|
|
172
|
+
"reduce" => {
|
|
173
|
+
name: "reduce",
|
|
174
|
+
description: "combines items in the range with a function.",
|
|
175
|
+
examples: [
|
|
176
|
+
"(1..3).reduce((sum, x) => { sum + x })",
|
|
177
|
+
"(1..3).reduce((product, x) => { product * x })",
|
|
178
|
+
"(2..4).reduce((sum, x) => { sum + x })"
|
|
179
|
+
]
|
|
180
|
+
},
|
|
181
|
+
"step" => {
|
|
182
|
+
name: "step",
|
|
183
|
+
description: "returns a list of items separated by a step size.",
|
|
184
|
+
examples: %w[(1..5).step(2) (1..5).step(1) (1...5).step(2)]
|
|
185
|
+
},
|
|
186
|
+
"binary_search" => {
|
|
187
|
+
name: "binary_search",
|
|
188
|
+
description: "returns the first item matched by binary search.",
|
|
189
|
+
examples: [
|
|
190
|
+
"(1..10).binary_search((x) => { x >= 5 })",
|
|
191
|
+
"(1..10).binary_search((x) => { x > 10 })",
|
|
192
|
+
"(1..3).binary_search((x) => { x >= 2 })"
|
|
193
|
+
]
|
|
194
|
+
},
|
|
195
|
+
"sample" => {
|
|
196
|
+
name: "sample",
|
|
197
|
+
description: "returns a random item from the range.",
|
|
198
|
+
examples: %w[(1..3).sample (2..4).sample (:a..:c).sample]
|
|
199
|
+
},
|
|
200
|
+
"size" => {
|
|
201
|
+
name: "size",
|
|
202
|
+
description: "returns the number of items in the range.",
|
|
203
|
+
examples: %w[(1..3).size (1...3).size (:a..:c).size]
|
|
204
|
+
},
|
|
205
|
+
"count" => {
|
|
206
|
+
name: "count",
|
|
207
|
+
description:
|
|
208
|
+
"returns the number of items, optionally matched by a function.",
|
|
209
|
+
examples: [
|
|
210
|
+
"(1..3).count",
|
|
211
|
+
"(1...3).count",
|
|
212
|
+
"(1..5).count((x) => { x.even? })"
|
|
213
|
+
]
|
|
214
|
+
},
|
|
215
|
+
"to_list" => {
|
|
216
|
+
name: "to_list",
|
|
217
|
+
description: "returns the range items as a list.",
|
|
218
|
+
examples: %w[(1..3).to_list (1...3).to_list (:a..:c).to_list]
|
|
219
|
+
},
|
|
220
|
+
"entries" => {
|
|
221
|
+
name: "entries",
|
|
222
|
+
description: "returns the range items as a list.",
|
|
223
|
+
examples: %w[(1..3).entries (1...3).entries (:a..:c).entries]
|
|
224
|
+
},
|
|
225
|
+
"to_dictionary" => {
|
|
226
|
+
name: "to_dictionary",
|
|
227
|
+
description: "returns a dictionary built from indexed range items.",
|
|
228
|
+
examples: %w[
|
|
229
|
+
(1..3).to_dictionary
|
|
230
|
+
(1...3).to_dictionary
|
|
231
|
+
(:a..:c).to_dictionary
|
|
232
|
+
]
|
|
233
|
+
}
|
|
234
|
+
}.freeze
|
|
235
|
+
|
|
236
|
+
def self.function_documentation(scope)
|
|
237
|
+
return INSTANCE_FUNCTIONS if scope == :instance
|
|
238
|
+
|
|
239
|
+
{}
|
|
240
|
+
end
|
|
241
|
+
|
|
6
242
|
attr_reader :code_left, :code_right, :code_options, :code_exclude_end
|
|
7
243
|
|
|
8
244
|
def initialize(*args, **kargs, &_block)
|
|
@@ -47,30 +283,90 @@ class Code
|
|
|
47
283
|
when "any?"
|
|
48
284
|
sig(args) { Function }
|
|
49
285
|
code_any?(code_value, **globals)
|
|
286
|
+
when "none?"
|
|
287
|
+
sig(args) { Function }
|
|
288
|
+
code_none?(code_value, **globals)
|
|
50
289
|
when "each"
|
|
51
290
|
sig(args) { Function }
|
|
52
291
|
code_each(code_value, **globals)
|
|
292
|
+
when "reverse_each"
|
|
293
|
+
sig(args) { Function }
|
|
294
|
+
code_reverse_each(code_value, **globals)
|
|
295
|
+
when "include?"
|
|
296
|
+
sig(args) { Object }
|
|
297
|
+
code_include?(code_value)
|
|
298
|
+
when "member?"
|
|
299
|
+
sig(args) { Object }
|
|
300
|
+
code_member?(code_value)
|
|
301
|
+
when "cover?"
|
|
302
|
+
sig(args) { Object }
|
|
303
|
+
code_cover?(code_value)
|
|
304
|
+
when "overlap?"
|
|
305
|
+
sig(args) { Range }
|
|
306
|
+
code_overlap?(code_value)
|
|
307
|
+
when "empty?"
|
|
308
|
+
sig(args)
|
|
309
|
+
code_empty?
|
|
310
|
+
when "begin"
|
|
311
|
+
sig(args)
|
|
312
|
+
code_begin
|
|
313
|
+
when "end"
|
|
314
|
+
sig(args)
|
|
315
|
+
code_end
|
|
316
|
+
when "exclude_end?"
|
|
317
|
+
sig(args)
|
|
318
|
+
code_exclude_end?
|
|
53
319
|
when "first"
|
|
54
320
|
sig(args)
|
|
55
321
|
code_first
|
|
56
322
|
when "last"
|
|
57
323
|
sig(args)
|
|
58
324
|
code_last
|
|
325
|
+
when "minimum"
|
|
326
|
+
sig(args)
|
|
327
|
+
code_minimum
|
|
328
|
+
when "maximum"
|
|
329
|
+
sig(args)
|
|
330
|
+
code_maximum
|
|
331
|
+
when "minimum_maximum"
|
|
332
|
+
sig(args)
|
|
333
|
+
code_minimum_maximum
|
|
59
334
|
when "map"
|
|
60
335
|
sig(args) { Function }
|
|
61
336
|
code_map(code_value, **globals)
|
|
62
337
|
when "select"
|
|
63
338
|
sig(args) { Function }
|
|
64
339
|
code_select(code_value, **globals)
|
|
340
|
+
when "reject"
|
|
341
|
+
sig(args) { Function }
|
|
342
|
+
code_reject(code_value, **globals)
|
|
343
|
+
when "reduce"
|
|
344
|
+
sig(args) { Function }
|
|
345
|
+
code_reduce(code_value, **globals)
|
|
65
346
|
when "step"
|
|
66
|
-
sig(args) { Integer | Decimal }
|
|
67
|
-
code_step(
|
|
347
|
+
sig(args) { [(Integer | Decimal).maybe, Function.maybe] }
|
|
348
|
+
code_step(*code_arguments.raw, **globals)
|
|
349
|
+
when "binary_search"
|
|
350
|
+
sig(args) { Function }
|
|
351
|
+
code_binary_search(code_value, **globals)
|
|
68
352
|
when "sample"
|
|
69
353
|
sig(args)
|
|
70
354
|
code_sample
|
|
355
|
+
when "size"
|
|
356
|
+
sig(args)
|
|
357
|
+
code_size
|
|
358
|
+
when "count"
|
|
359
|
+
sig(args) { Function.maybe }
|
|
360
|
+
code_count(code_value, **globals)
|
|
71
361
|
when "to_list"
|
|
72
362
|
sig(args)
|
|
73
363
|
code_to_list
|
|
364
|
+
when "entries"
|
|
365
|
+
sig(args)
|
|
366
|
+
code_entries
|
|
367
|
+
when "to_dictionary"
|
|
368
|
+
sig(args)
|
|
369
|
+
code_to_dictionary
|
|
74
370
|
else
|
|
75
371
|
super
|
|
76
372
|
end
|
|
@@ -120,6 +416,28 @@ class Code
|
|
|
120
416
|
e.code_value
|
|
121
417
|
end
|
|
122
418
|
|
|
419
|
+
def code_none?(argument, **globals)
|
|
420
|
+
code_argument = argument.to_code
|
|
421
|
+
|
|
422
|
+
index = 0
|
|
423
|
+
|
|
424
|
+
Boolean.new(
|
|
425
|
+
raw.none? do |code_element|
|
|
426
|
+
code_argument
|
|
427
|
+
.call(
|
|
428
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
|
429
|
+
**globals
|
|
430
|
+
)
|
|
431
|
+
.truthy?
|
|
432
|
+
.tap { index += 1 }
|
|
433
|
+
rescue Error::Next => e
|
|
434
|
+
e.code_value.truthy?.tap { index += 1 }
|
|
435
|
+
end
|
|
436
|
+
)
|
|
437
|
+
rescue Error::Break => e
|
|
438
|
+
e.code_value
|
|
439
|
+
end
|
|
440
|
+
|
|
123
441
|
def code_each(argument, **globals)
|
|
124
442
|
code_argument = argument.to_code
|
|
125
443
|
|
|
@@ -137,6 +455,59 @@ class Code
|
|
|
137
455
|
e.code_value
|
|
138
456
|
end
|
|
139
457
|
|
|
458
|
+
def code_reverse_each(argument, **globals)
|
|
459
|
+
code_argument = argument.to_code
|
|
460
|
+
|
|
461
|
+
raw.reverse_each.with_index do |code_element, index|
|
|
462
|
+
code_argument.call(
|
|
463
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
|
464
|
+
**globals
|
|
465
|
+
)
|
|
466
|
+
rescue Error::Next => e
|
|
467
|
+
e.code_value
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
self
|
|
471
|
+
rescue Error::Break => e
|
|
472
|
+
e.code_value
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
def code_include?(value)
|
|
476
|
+
code_value = value.to_code
|
|
477
|
+
Boolean.new(raw.include?(code_value))
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
def code_member?(value)
|
|
481
|
+
code_include?(value)
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
def code_cover?(value)
|
|
485
|
+
code_value = value.to_code
|
|
486
|
+
Boolean.new(raw.cover?(code_value))
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
def code_overlap?(range)
|
|
490
|
+
code_range = range.to_code
|
|
491
|
+
|
|
492
|
+
Boolean.new(raw.overlap?(code_range.raw))
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
def code_empty?
|
|
496
|
+
Boolean.new(raw.to_a.empty?)
|
|
497
|
+
end
|
|
498
|
+
|
|
499
|
+
def code_begin
|
|
500
|
+
code_left
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
def code_end
|
|
504
|
+
code_right
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
def code_exclude_end?
|
|
508
|
+
Boolean.new(exclude_end?)
|
|
509
|
+
end
|
|
510
|
+
|
|
140
511
|
def code_first
|
|
141
512
|
raw.first || Nothing.new
|
|
142
513
|
end
|
|
@@ -145,6 +516,18 @@ class Code
|
|
|
145
516
|
raw.last || Nothing.new
|
|
146
517
|
end
|
|
147
518
|
|
|
519
|
+
def code_minimum
|
|
520
|
+
raw.min || Nothing.new
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
def code_maximum
|
|
524
|
+
raw.max || Nothing.new
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
def code_minimum_maximum
|
|
528
|
+
List.new([code_minimum, code_maximum])
|
|
529
|
+
end
|
|
530
|
+
|
|
148
531
|
def code_map(argument, **globals)
|
|
149
532
|
code_argument = argument.to_code
|
|
150
533
|
|
|
@@ -179,41 +562,174 @@ class Code
|
|
|
179
562
|
e.code_value
|
|
180
563
|
end
|
|
181
564
|
|
|
565
|
+
def code_reject(argument, **globals)
|
|
566
|
+
code_argument = argument.to_code
|
|
567
|
+
|
|
568
|
+
List.new(
|
|
569
|
+
raw.reject.with_index do |code_element, index|
|
|
570
|
+
code_argument.call(
|
|
571
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
|
572
|
+
**globals
|
|
573
|
+
).truthy?
|
|
574
|
+
rescue Error::Next => e
|
|
575
|
+
e.code_value.truthy?
|
|
576
|
+
end
|
|
577
|
+
)
|
|
578
|
+
rescue Error::Break => e
|
|
579
|
+
e.code_value
|
|
580
|
+
end
|
|
581
|
+
|
|
582
|
+
def code_reduce(argument, **globals)
|
|
583
|
+
code_argument = argument.to_code
|
|
584
|
+
|
|
585
|
+
index = 0
|
|
586
|
+
|
|
587
|
+
raw.reduce do |code_acc, code_element|
|
|
588
|
+
code_argument
|
|
589
|
+
.call(
|
|
590
|
+
arguments:
|
|
591
|
+
List.new([code_acc, code_element, Integer.new(index), self]),
|
|
592
|
+
**globals
|
|
593
|
+
)
|
|
594
|
+
.tap { index += 1 }
|
|
595
|
+
rescue Error::Next => e
|
|
596
|
+
e.code_value.tap { index += 1 }
|
|
597
|
+
end || Nothing.new
|
|
598
|
+
rescue Error::Break => e
|
|
599
|
+
e.code_value
|
|
600
|
+
end
|
|
601
|
+
|
|
182
602
|
def exclude_end?
|
|
183
603
|
code_exclude_end.truthy?
|
|
184
604
|
end
|
|
185
605
|
|
|
186
|
-
def
|
|
606
|
+
def code_binary_search(argument, **globals)
|
|
187
607
|
code_argument = argument.to_code
|
|
608
|
+
values = raw.to_a
|
|
609
|
+
lower = 0
|
|
610
|
+
upper = values.length - 1
|
|
611
|
+
result = nil
|
|
612
|
+
|
|
613
|
+
while lower <= upper
|
|
614
|
+
index = (lower + upper) / 2
|
|
615
|
+
code_element = values[index]
|
|
616
|
+
matched =
|
|
617
|
+
begin
|
|
618
|
+
code_argument.call(
|
|
619
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
|
620
|
+
**globals
|
|
621
|
+
).truthy?
|
|
622
|
+
rescue Error::Next => e
|
|
623
|
+
e.code_value.truthy?
|
|
624
|
+
end
|
|
625
|
+
|
|
626
|
+
if matched
|
|
627
|
+
result = code_element
|
|
628
|
+
upper = index - 1
|
|
629
|
+
else
|
|
630
|
+
lower = index + 1
|
|
631
|
+
end
|
|
632
|
+
end
|
|
633
|
+
|
|
634
|
+
result || Nothing.new
|
|
635
|
+
rescue Error::Break => e
|
|
636
|
+
e.code_value
|
|
637
|
+
end
|
|
638
|
+
|
|
639
|
+
def code_step(argument = nil, function = nil, **globals)
|
|
640
|
+
code_argument = argument.to_code
|
|
641
|
+
code_function = function.to_code
|
|
642
|
+
|
|
643
|
+
if code_argument.is_a?(Function)
|
|
644
|
+
code_function = code_argument
|
|
645
|
+
code_argument = Integer.new(1)
|
|
646
|
+
elsif code_argument.nothing?
|
|
647
|
+
code_argument = Integer.new(1)
|
|
648
|
+
end
|
|
188
649
|
|
|
189
650
|
code_list = List.new
|
|
190
651
|
code_element = code_left
|
|
191
|
-
|
|
652
|
+
index = 0
|
|
653
|
+
step_is_positive = code_argument.code_greater(Integer.new(0)).truthy?
|
|
192
654
|
|
|
193
|
-
|
|
655
|
+
loop do
|
|
656
|
+
comparison =
|
|
657
|
+
if step_is_positive
|
|
658
|
+
if exclude_end?
|
|
659
|
+
code_element.code_less(code_right)
|
|
660
|
+
else
|
|
661
|
+
code_element.code_less_or_equal(code_right)
|
|
662
|
+
end
|
|
663
|
+
else
|
|
664
|
+
if exclude_end?
|
|
665
|
+
code_element.code_greater(code_right)
|
|
666
|
+
else
|
|
667
|
+
code_element.code_greater_or_equal(code_right)
|
|
668
|
+
end
|
|
669
|
+
end
|
|
194
670
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
671
|
+
break unless comparison.truthy?
|
|
672
|
+
|
|
673
|
+
if code_function.is_a?(Function)
|
|
674
|
+
code_function.call(
|
|
675
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
|
676
|
+
**globals
|
|
677
|
+
)
|
|
678
|
+
else
|
|
202
679
|
code_list.code_append(code_element)
|
|
203
|
-
code_element = code_element.code_plus(code_argument)
|
|
204
680
|
end
|
|
681
|
+
|
|
682
|
+
index += 1
|
|
683
|
+
code_element = code_element.code_plus(code_argument)
|
|
205
684
|
end
|
|
206
685
|
|
|
207
|
-
code_list
|
|
686
|
+
code_function.is_a?(Function) ? self : code_list
|
|
687
|
+
rescue Error::Break => e
|
|
688
|
+
e.code_value
|
|
208
689
|
end
|
|
209
690
|
|
|
210
691
|
def code_to_list
|
|
211
692
|
List.new(raw.to_a)
|
|
212
693
|
end
|
|
213
694
|
|
|
695
|
+
def code_entries
|
|
696
|
+
code_to_list
|
|
697
|
+
end
|
|
698
|
+
|
|
699
|
+
def code_to_dictionary
|
|
700
|
+
code_to_list.code_to_dictionary
|
|
701
|
+
end
|
|
702
|
+
|
|
214
703
|
def code_sample
|
|
215
704
|
code_to_list.code_sample
|
|
216
705
|
end
|
|
706
|
+
|
|
707
|
+
def code_size
|
|
708
|
+
Integer.new(raw.to_a.size)
|
|
709
|
+
end
|
|
710
|
+
|
|
711
|
+
def code_count(argument = nil, **globals)
|
|
712
|
+
code_argument = argument.to_code
|
|
713
|
+
|
|
714
|
+
return Integer.new(raw.to_a.size) if code_argument.nothing?
|
|
715
|
+
|
|
716
|
+
index = 0
|
|
717
|
+
Integer.new(
|
|
718
|
+
raw.count do |code_element|
|
|
719
|
+
code_argument
|
|
720
|
+
.call(
|
|
721
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
|
722
|
+
**globals
|
|
723
|
+
)
|
|
724
|
+
.truthy?
|
|
725
|
+
.tap { index += 1 }
|
|
726
|
+
rescue Error::Next => e
|
|
727
|
+
e.code_value.truthy?.tap { index += 1 }
|
|
728
|
+
end
|
|
729
|
+
)
|
|
730
|
+
rescue Error::Break => e
|
|
731
|
+
e.code_value
|
|
732
|
+
end
|
|
217
733
|
end
|
|
218
734
|
end
|
|
219
735
|
end
|