1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
Int.abs:
short: absolute value
description: >
Calculates the absolute value of an integer.
return:
type: 'Int'
description: >
The absolute value of `x`.
args:
x:
type: 'Int'
description: >
The integer whose absolute value is to be calculated.
example: |
>> (-10).abs()
= 10
Int.choose:
short: binomial coefficient
description: >
Computes the binomial coefficient of the given numbers (the equivalent of `n`
choose `k` in combinatorics). This is equal to `n.factorial()/(k.factorial() *
(n-k).factorial())`.
return:
type: 'Int'
description: >
The binomial coefficient, equivalent to the number of ways to uniquely choose
`k` objects from among `n` objects, ignoring order.
args:
n:
type: 'Int'
description: >
The number of things to choose from.
k:
type: 'Int'
description: >
The number of things to be chosen.
example: |
>> (4).choose(2)
= 6
Int.clamped:
short: clamp an integer
description: >
Returns the given number clamped between two values so that it is within
that range.
return:
type: 'Int'
description: >
The first argument clamped between the other two arguments.
args:
x:
type: 'Int'
description: >
The integer to clamp.
low:
type: 'Int'
description: >
The lowest value the result can take.
high:
type: 'Int'
description: >
The highest value the result can take.
example: |
>> (2).clamped(5, 10)
= 5
Int.factorial:
short: factorial
description: >
Computes the factorial of an integer.
return:
type: 'Text'
description: >
The factorial of the given integer.
args:
n:
type: 'Int'
description: >
The integer to compute the factorial of.
example: |
>> (10).factorial()
= 3628800
Int.get_bit:
short: check whether a bit is set
description: >
In the binary representation of an integer, check whether a given bit index
is set to 1 or not.
note: >
For fixed-size integers, the bit index must be between 1 and the number of
bits in that integer (i.e. 1-64 for `Int64`). For `Int`, the bit index must
be between 1 and `Int64.max`. Values outside this range will produce a
runtime error.
return:
type: 'Bool'
description: >
Whether or not the given bit index is set to 1 in the binary
representation of the integer.
args:
i:
type: 'Int'
description: >
The integer whose bits are being inspected.
bit_index:
type: 'Int'
description: >
The index of the bit to check (1-indexed).
example: |
>> (6).get_bit(1)
= no
>> (6).get_bit(2)
= yes
>> (6).get_bit(3)
= yes
>> (6).get_bit(4)
= no
Int.hex:
short: convert to hexidecimal
description: >
Converts an integer to its hexadecimal representation.
return:
type: 'Text'
description: >
The hexadecimal string representation of the integer.
args:
i:
type: 'Int'
description: >
The integer to be converted.
digits:
type: 'Int'
default: '0'
description: >
The minimum number of digits in the output string.
uppercase:
type: 'Bool'
default: 'yes'
description: >
Whether to use uppercase letters for hexadecimal digits.
prefix:
type: 'Bool'
default: 'yes'
description: >
Whether to include a "0x" prefix.
example: |
>> (255).hex(digits=4, uppercase=yes, prefix=yes)
= "0x00FF"
Int.is_between:
short: test if an int is in a range
description: >
Determines if an integer is between two numbers (inclusive).
return:
type: 'Bool'
description: >
`yes` if `low <= x and x <= high`, otherwise `no`
args:
x:
type: 'Int'
description: >
The integer to be checked.
low:
type: 'Int'
description: >
The lower bound to check (inclusive).
high:
type: 'Int'
description: >
The upper bound to check (inclusive).
example: |
>> (7).is_between(1, 10)
= yes
>> (7).is_between(100, 200)
= no
>> (7).is_between(1, 7)
= yes
Int.is_prime:
short: check if an integer is prime
description: >
Determines if an integer is a prime number.
note: >
This function is _probabilistic_. With the default arguments, the chances of
getting an incorrect answer are astronomically small (on the order of 10^(-30)).
See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp)
for more details.
return:
type: 'Bool'
description: >
`yes` if `x` is a prime number, `no` otherwise.
args:
x:
type: 'Int'
description: >
The integer to be checked.
reps:
type: 'Int'
default: '50'
description: >
The number of repetitions for primality tests.
example: |
>> (7).is_prime()
= yes
>> (6).is_prime()
= no
Int.next_prime:
short: get the next prime
description: >
Finds the next prime number greater than the given integer.
note: >
This function is _probabilistic_, but the chances of getting an incorrect
answer are astronomically small (on the order of 10^(-30)).
See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp)
for more details.
return:
type: 'Int'
description: >
The next prime number greater than `x`.
args:
x:
type: 'Int'
description: >
The integer after which to find the next prime.
example: |
>> (11).next_prime()
= 13
Int.octal:
short: convert to octal
description: >
Converts an integer to its octal representation.
return:
type: 'Text'
description: >
The octal string representation of the integer.
args:
i:
type: 'Int'
description: >
The integer to be converted.
digits:
type: 'Int'
default: '0'
description: >
The minimum number of digits in the output string.
prefix:
type: 'Bool'
default: 'yes'
description: >
Whether to include a "0o" prefix.
example: |
>> (64).octal(digits=4, prefix=yes)
= "0o0100"
Int.onward:
short: iterate from a number onward
description: >
Return an iterator that counts infinitely from the starting integer (with an
optional step size).
return:
type: 'Text'
description: >
An iterator function that counts onward from the starting integer.
args:
first:
type: 'Int'
description: >
The starting integer.
step:
type: 'Int'
default: '1'
description: >
The increment step size.
example: |
nums : &[Int] = &[]
for i in (5).onward()
nums.insert(i)
stop if i == 10
>> nums[]
= [5, 6, 7, 8, 9, 10]
Int.parse:
short: convert text to integer
description: >
Converts a text representation of an integer into an integer.
return:
type: 'Int?'
description: >
The integer represented by the text. If the given text contains a value outside
of the representable range or if the entire text can't be parsed as an integer,
`none` will be returned.
args:
text:
type: 'Text'
description: >
The text containing the integer.
example: |
>> Int.parse("123")
= 123 : Int?
>> Int.parse("0xFF")
= 255 : Int?
# Can't parse:
>> Int.parse("asdf")
= none : Int?
# Outside valid range:
>> Int8.parse("9999999")
= none : Int8?
Int.prev_prime:
short: get the previous prime
description: >
Finds the previous prime number less than the given integer.
If there is no previous prime number (i.e. if a number less than `2` is
provided), then the function will create a runtime error.
note: >
This function is _probabilistic_, but the chances of getting an incorrect
answer are astronomically small (on the order of 10^(-30)).
See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp)
for more details.
return:
type: 'Int?'
description: >
The previous prime number less than `x`, or `none` if `x` is less than 2.
args:
x:
type: 'Int'
description: >
The integer before which to find the previous prime.
example: |
>> (11).prev_prime()
= 7
Int.sqrt:
short: square root
description: >
Calculates the nearest square root of an integer.
return:
type: 'Int'
description: >
The integer part of the square root of `x`.
args:
x:
type: 'Int'
description: >
The integer whose square root is to be calculated.
example: |
>> (16).sqrt()
= 4
>> (17).sqrt()
= 4
Int.to:
short: iterate a range of integers
description: >
Returns an iterator function that iterates over the range of numbers specified.
return:
type: 'func(->Int?)'
description: >
An iterator function that returns each integer in the given range (inclusive).
args:
first:
type: 'Int'
description: >
The starting value of the range.
last:
type: 'Int'
description: >
The ending value of the range.
step:
type: 'Int?'
default: 'none'
description: >
An optional step size to use. If unspecified or `none`, the step will be inferred to be `+1` if `last >= first`, otherwise `-1`.
example: |
>> (2).to(5)
= func(->Int?)
>> [x for x in (2).to(5)]
= [2, 3, 4, 5]
>> [x for x in (5).to(2)]
= [5, 4, 3, 2]
>> [x for x in (2).to(5, step=2)]
= [2, 4]
|