code / tomo

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