uiua_parser/defs.rs
1//! All primitive definitions
2
3use enum_iterator::Sequence;
4use serde::*;
5
6use crate::{AsciiToken, PrimClass, PrimNames, Purity, SysOpClass};
7
8macro_rules! primitive {
9 ($(
10 #[doc = $doc_rust:literal]
11 $(#[doc = $doc:literal])*
12 (
13 $(
14 $($args:literal)?
15 $(($outputs:expr))?
16 $([$mod_args:expr])?
17 ,)?
18 $variant:ident, $class:ident, $names:expr $(,$purity:ident)* $(,{experimental: $experimental:literal})?
19 )
20 ),* $(,)?) => {
21 /// A built-in function
22 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Sequence, Serialize, Deserialize)]
23 #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
24 #[allow(rustdoc::broken_intra_doc_links)]
25 pub enum Primitive {
26 $(
27 #[doc = $doc_rust]
28 $variant,
29 )*
30 /// System function
31 #[serde(untagged)]
32 Sys(SysOp)
33 }
34
35 impl Primitive {
36 /// Get the primitive's names
37 #[allow(path_statements)]
38 pub fn names(&self) -> PrimNames {
39 match self {
40 $(Primitive::$variant => $names.into(),)*
41 Primitive::Sys(op) => op.name().into()
42 }
43 }
44 /// Get the primitive's class
45 pub fn class(&self) -> PrimClass {
46 match self {
47 $(Primitive::$variant => PrimClass::$class,)*
48 Primitive::Sys(op) => PrimClass::Sys(op.class()),
49 }
50 }
51 /// Get the number of function arguments the primitive takes
52 pub fn modifier_args(&self) -> Option<usize> {
53 match self {
54 $($($(Primitive::$variant => Some($mod_args),)?)?)*
55 Primitive::Sys(op) => op.modifier_args(),
56 _ => None
57 }
58 }
59 /// Get the number of arguments the primitive takes
60 pub fn args(&self) -> Option<usize> {
61 match self {
62 $($($(Primitive::$variant => Some($args),)?)?)*
63 Primitive::Sys(op) => Some(op.args()),
64 _ => None
65 }
66 }
67 /// Get the number of outputs the primitive produces
68 pub fn outputs(&self) -> Option<usize> {
69 match self {
70 $($($(Primitive::$variant => $outputs.into(),)?)?)*
71 Primitive::Sys(op) => Some(op.outputs()),
72 _ => Some(1)
73 }
74 }
75 /// Whether the primitive is pure
76 pub fn purity(&self) -> Purity {
77 match self {
78 $($(Primitive::$variant => Purity::$purity,)*)*
79 Primitive::Sys(op) => op.purity(),
80 _ => Purity::Pure
81 }
82 }
83 /// Check if this primitive is experimental
84 #[allow(unused_parens)]
85 pub fn is_experimental(&self) -> bool {
86 match self {
87 $($(Primitive::$variant => $experimental,)*)*
88 Primitive::Sys(op) => op.is_experimental(),
89 _ => false
90 }
91 }
92 /// Get the primitive's documentation string
93 pub fn doc(&self) -> &'static str {
94 match self {
95 $(Primitive::$variant => concat!($doc_rust, $($doc, "\n"),*),)*
96 Primitive::Sys(op) => op.doc(),
97 }
98 }
99 }
100 };
101}
102
103primitive!(
104 /// Duplicate the top value on the stack
105 ///
106 /// ex: [. 1 2 3 4]
107 ///
108 /// There is usually a better alternative to [duplicate]. Consider whether [by] or [fork] solves your stack manipulation needs instead.
109 ///
110 /// [duplicate] is often used in examples to show both the input and output of a function.
111 /// ex: √.144
112 /// ex: .[1 2 3 4]
113 /// : +1⇌
114 ///
115 /// [duplicate] works well with [table]:
116 /// ex: ⊞=.⇡4
117 /// Sometimes it is also good with [group] or [partition]
118 /// ex: ⊜⧻.[1 1 0 0 2 2 2 2 0 1 0 3 3]
119 (1(2), Dup, Stack, ("duplicate", '.')),
120 /// Swap the top two values on the stack
121 ///
122 /// ex: [: 1 2 3 4 5]
123 ///
124 /// [flip] is generally recommend against. It is largely a relic of when Uiua was a different language.
125 /// Many cases can be replaced with [backward]. Others can be replaced with [dip], [fork], [both], [on], [by], [with], or [off].
126 (2(2), Flip, Stack, ("flip", AsciiToken::Colon, ':')),
127 /// Discard the top stack value
128 ///
129 /// ex: [◌ 1 2 ◌ 3 4]
130 /// This is usually used to discard values that are no longer needed.
131 ///
132 /// [un][pop] can be used to retrieve the [fill] value.
133 /// ex: ⬚3(+°◌°◌)
134 (1(0), Pop, Stack, ("pop", '◌')),
135 /// Do nothing with one value
136 ///
137 /// ex: ∘ 5
138 ///
139 /// [identity] is mostly useless on its own. See the [More Stack Manipulation Tutorial](/tutorial/morestack) to understand what it is for.
140 (1, Identity, Planet, ("identity", '∘')),
141 // Pervasive monadic ops
142 /// Logical not
143 ///
144 /// ex: ¬0
145 /// ex: ¬1
146 /// ex: ¬[0 1 1 0]
147 /// ex: ¬[0 1 2 3]
148 ///
149 /// This is equivalent to `subtract``dip``1`
150 /// ex: ¬7
151 /// ex: ¬[1 2 3 4]
152 (1, Not, MonadicPervasive, ("not", '¬')),
153 /// Numerical sign (1, ¯1, or 0)
154 ///
155 /// ex: ± 1
156 /// ex: ± ¯5
157 /// ex: ± 0
158 /// ex: ± [¯2 ¯1 0 1 2]
159 /// [sign] on a [complex] number normalizes it to a magnitude of 1.
160 /// ex: ± ℂ3 4
161 ///
162 /// [sign] also works on characters to get their case.
163 /// - `¯1` for lowercase
164 /// - `1` for uppercase
165 /// - `0` for caseless
166 /// ex: ± "Hello, World!"
167 (1, Sign, MonadicPervasive, ("sign", '±')),
168 /// Negate a number
169 ///
170 /// Formats from `\``.
171 ///
172 /// ex: ¯ 1
173 /// ex: ¯ ¯3
174 /// ex: ¯ [1 2 ¯3]
175 /// ex: ¯ ℂ3 5
176 ///
177 /// [negate] also works on characters to toggle their case.
178 /// ex: ¯ "Hello, World!"
179 /// Use this with [absolute value] to lowercase a string.
180 /// ex: ¯⌵ "Hello, World!"
181 ///
182 /// Subscripted [negate] will rotate a number one nth of a turn in the complex plane.
183 /// ex: ¯₂ 5
184 /// ex: ¯₄ 5
185 /// ex: ⁅₃ ¯₃ 5
186 /// ex: [⍥₄⊸¯₄ ℂ1 2]
187 (
188 1,
189 Neg,
190 MonadicPervasive,
191 ("negate", AsciiToken::Backtick, '¯')
192 ),
193 /// Get the absolute value of a number
194 ///
195 /// ex: ⌵ ¯1
196 /// ex: ⌵ 1
197 /// [absolute value] converts complex numbers to their magnitude.
198 /// ex: ⌵ ℂ3 4
199 ///
200 /// [absolute value] works on characters to uppercase them.
201 /// ex: ⌵ "Hello, World!"
202 ///
203 /// The glyph looks like the graph of `|x|`.
204 (1, Abs, MonadicPervasive, ("absolute value", '⌵')),
205 /// Get the reciprocal of a number
206 ///
207 /// ex: # Experimental!
208 /// : ⨪3
209 /// ex: # Experimental!
210 /// : ⨪[1 2 3 6]
211 /// ex: # Experimental!
212 /// : ⨪ℂ2 1
213 (1, Reciprocal, MonadicPervasive, ("reciprocal", '⨪'), { experimental: true }),
214 /// Take the square root of a number
215 ///
216 /// ex: √4
217 /// ex: √[1 4 9 16]
218 /// ex: √¯1
219 /// You can only take the square root of a negative number if it is complex.
220 /// ex: √ ¯4
221 /// : √ℂ0¯4
222 ///
223 /// Subscripted [sqrt] gives the nth root.
224 /// ex: √₄81
225 ///
226 (1, Sqrt, MonadicPervasive, ("sqrt", '√')),
227 /// Get the exponential of a number
228 ///
229 /// ex: ₑ 1
230 /// You can get the natural logarithm with [un].
231 /// ex: °ₑ e
232 /// You can give a subscript to set the base.
233 /// ex: ₑ₂ 8
234 /// : ₑ₁₀ 6
235 (1, Exp, MonadicPervasive, ("exponential", 'ₑ')),
236 /// Get the sine of a number
237 ///
238 /// ex: ∿ 1
239 /// You can get a cosine function by [add]ing [eta].
240 /// ex: ∿+η 1
241 /// You can get an arcsine function with [un].
242 /// ex: °∿ 1
243 /// You can get an arccosine function by [subtract]ing the arcsine from [eta].
244 /// ex: ˜-η°∿ 0
245 (1, Sin, MonadicPervasive, ("sine", '∿')),
246 /// Round to the nearest integer towards `¯∞`
247 ///
248 /// ex: ⌊1.5
249 /// ex: ⌊¯1.5
250 /// ex: ⌊[1.5 ¯1.5 0.5 ¯0.5]
251 (1, Floor, MonadicPervasive, ("floor", '⌊')),
252 /// Round to the nearest integer towards `∞`
253 ///
254 /// ex: ⌈1.5
255 /// ex: ⌈¯1.5
256 /// ex: ⌈[1.5 ¯1.5 0.5 ¯0.5]
257 (1, Ceil, MonadicPervasive, ("ceiling", '⌈')),
258 /// Round to the nearest integer
259 ///
260 /// ex: ⁅1.2
261 /// ex: ⁅¯1.2
262 /// Numbers with fraction `0.5` always round away from zero.
263 /// ex: ⁅1.5
264 /// ex: ⁅[0.1 π 2 9.9 7.5]
265 /// ex: ⁅[4/3_¯2.5 9.81_¯3.6]
266 /// Subscripted [round] rounds to that many decimal places.
267 /// ex: ⁅₃ π
268 /// : ⁅₃ τ
269 /// If you need a dynamic number of decimal places, you can use [under][multiply].
270 /// ex: ⍜×⁅ 3 π
271 /// ex: ⍜×⁅ ˜ⁿ10⇡6 π
272 (1, Round, MonadicPervasive, ("round", '⁅')),
273 /// Compare for equality
274 ///
275 /// ex: =1 2
276 /// ex: =5 5
277 /// ex: =1 [1 2 3]
278 /// ex: = [1 2 2] [1 2 3]
279 /// Boxes compare lexicographically
280 /// ex: = {1 2_3 4_5_6} {1_2 3 4_5_6}
281 (2, Eq, DyadicPervasive, ("equals", AsciiToken::Equal, '=')),
282 /// Compare for inequality
283 ///
284 /// Formats from `!=`.
285 ///
286 /// ex: ≠1 2
287 /// ex: ≠5 5
288 /// ex: ≠1 [1 2 3]
289 /// ex: ≠ [1 2 2] [1 2 3]
290 /// Boxes compare lexicographically
291 /// ex: ≠ {1 2_3 4_5_6} {1_2 3 4_5_6}
292 (
293 2,
294 Ne,
295 DyadicPervasive,
296 ("not equals", AsciiToken::BangEqual, '≠')
297 ),
298 /// Compare for less than
299 ///
300 /// The second value is checked to be less than the first.
301 /// This is so you can think of `<``x` as a single unit.
302 /// ex: <1 2
303 /// ex: <5 5
304 /// ex: <7 3
305 /// ex: <2 [1 2 3]
306 /// ex: < [1 2 2] [1 2 3]
307 /// Boxes compare lexicographically
308 /// ex: < {1 2_3 4_5_6} {1_2 3 4_5_6}
309 (2, Lt, DyadicPervasive, ("less than", '<')),
310 /// Compare for less than or equal
311 ///
312 /// Formats from `<=`.
313 ///
314 /// The second value is checked to be less than or equal to the first.
315 /// This is so you can think of `≤``x` as a single unit.
316 /// ex: ≤1 2
317 /// ex: ≤5 5
318 /// ex: ≤7 3
319 /// ex: ≤2 [1 2 3]
320 /// ex: ≤ [1 2 2] [1 2 3]
321 /// Boxes compare lexicographically
322 /// ex: ≤ {1 2_3 4_5_6} {1_2 3 4_5_6}
323 (
324 2,
325 Le,
326 DyadicPervasive,
327 ("less or equal", AsciiToken::LessEqual, '≤')
328 ),
329 /// Compare for greater than
330 ///
331 /// The second value is checked to be greater than the first.
332 /// This is so you can think of `>``x` as a single unit.
333 /// ex: >1 2
334 /// ex: >5 5
335 /// ex: >7 3
336 /// ex: >2 [1 2 3]
337 /// ex: > [1 2 2] [1 2 3]
338 /// Boxes compare lexicographically
339 /// ex: > {1 2_3 4_5_6} {1_2 3 4_5_6}
340 (2, Gt, DyadicPervasive, ("greater than", '>')),
341 /// Compare for greater than or equal
342 ///
343 /// Formats from `>=`.
344 ///
345 /// The second value is checked to be greater than or equal to the first.
346 /// This is so you can think of `≥``x` as a single unit.
347 /// ex: ≥1 2
348 /// ex: ≥5 5
349 /// ex: ≥7 3
350 /// ex: ≥2 [1 2 3]
351 /// ex: ≥ [1 2 2] [1 2 3]
352 /// Boxes compare lexicographically
353 /// ex: ≥ {1 2_3 4_5_6} {1_2 3 4_5_6}
354 (
355 2,
356 Ge,
357 DyadicPervasive,
358 ("greater or equal", AsciiToken::GreaterEqual, '≥')
359 ),
360 /// Add values
361 ///
362 /// ex: +1 2
363 /// ex: +1 [2 3 4]
364 /// ex: + [1 2 3] [4 5 6]
365 ///
366 /// [un][add] splits a number into fractional and integer parts.
367 /// ex: °+ [3.5 ¯5.25]
368 (2, Add, DyadicPervasive, ("add", '+')),
369 /// Subtract values
370 ///
371 /// The first value is subtracted from the second.
372 /// This is so you can think of `-``x` as a single unit.
373 /// ex: -1 2
374 /// ex: -1 [2 3 4]
375 /// ex: - [1 2 3] [4 5 6]
376 (2, Sub, DyadicPervasive, ("subtract", '-')),
377 /// Multiply values
378 ///
379 /// Formats from `*`.
380 ///
381 /// ex: ×3 5
382 /// ex: ×2 [1 2 3]
383 /// ex: × [1 2 3] [4 5 6]
384 /// ex: × [¯1 0 1] "hey"
385 ///
386 /// Uiua does not have dedicated boolean logical operators.
387 /// [multiply] can be used as a logical AND.
388 /// ex: ◡×⊓⌟≥≤5 8 . [6 2 5 9 6 5 0 4]
389 ///
390 /// [un][multiply] splits a number into sign and magnitude.
391 /// ex: °× [1.5 0 ¯4]
392 /// ex: °× [i ℂ3 4 2]
393 (2, Mul, DyadicPervasive, ("multiply", AsciiToken::Star, '×')),
394 /// Divide values
395 ///
396 /// Formats from `%`.
397 ///
398 /// The second value is divided by the first.
399 /// This is so you can think of `÷``x` as a single unit.
400 /// ex: ÷3 12
401 /// ex: ÷2 [1 2 3]
402 /// ex: ÷ [1 2 3] [4 5 6]
403 /// ex: ÷ [¯1 0 1] "hey"
404 ///
405 /// [un][divide] splits a number into whole number denominator and numerator.
406 /// ex: °÷ [0.75 5/2 0.123 100 ¯0.5]
407 (
408 2,
409 Div,
410 DyadicPervasive,
411 ("divide", AsciiToken::Percent, '÷')
412 ),
413 /// Modulo values
414 ///
415 /// The second value is divided by the first, and the remainder is returned.
416 /// This is so you can think of `◿``x` as a single unit.
417 /// ex: ◿10 27
418 /// ex: ◿5 [3 7 14]
419 /// ex: ◿ [3 4 5] [10 10 10]
420 ///
421 /// The result is always non-negative:
422 /// ex: ◿ 4 ¯21
423 /// If you prefer the negative modulo instead of the remainder, you may use [under]:
424 /// ex: ⍜⊙⌵◿ 4 ¯21
425 (2, Modulo, DyadicPervasive, ("modulo", '◿')),
426 /// Logical OR and greatest common divisor
427 ///
428 /// ex: # Experimental!
429 /// : ∨ [0 1 0 1] [0 0 1 1]
430 /// ex: # Experimental!
431 /// : ⊞∨.[0 1]
432 /// Non-boolean values give the GCD.
433 /// ex: # Experimental!
434 /// : ∨ 16 24
435 /// ex: # Experimental!
436 /// : ∨ 51 85
437 /// The [reduce] identity of [or] is `0`. This makes it better than [maximum] as a logical OR.
438 /// ex: # Experimental!
439 /// : /∨ []
440 /// ex: # Experimental!
441 /// : [⊃/∨/↥] [0 0]
442 /// : [⊃/∨/↥] [0]
443 /// : [⊃/∨/↥] []
444 (2, Or, DyadicPervasive, ("or", '∨'), { experimental: true }),
445 /// Raise a value to a power
446 ///
447 /// The second value is raised to the power of the first.
448 /// This is so you can think of `ⁿ``x` as a single unit.
449 /// ex: ⁿ2 3
450 /// ex: ⁿ2 [1 2 3]
451 /// ex: ⁿ [1 2 3] [4 5 6]
452 (2, Pow, DyadicPervasive, ("power", 'ⁿ')),
453 /// Get the based logarithm of a number
454 ///
455 /// The first value is the base, and the second value is the power.
456 /// ex: ₙ2 8
457 /// ex: ₙ2 [8 16 32]
458 /// ex: ₙ [2 3 4] [16 27 1024]
459 (2, Log, DyadicPervasive, ("logarithm", 'ₙ')),
460 /// Take the minimum of two arrays
461 ///
462 /// ex: ↧ 3 5
463 /// ex: ↧ [1 4 2] [3 7 1]
464 /// Boxes compare lexicographically
465 /// ex: ↧ {1_2_3 "dog"} {1_4_5 "cat"}
466 ///
467 /// See also: [maximum]
468 (2, Min, DyadicPervasive, ("minimum", '↧')),
469 /// Take the maximum of two arrays
470 ///
471 /// ex: ↥ 3 5
472 /// ex: ↥ [1 4 2] [3 7 1]
473 /// Boxes compare lexicographically
474 /// ex: ↥ {1_2_3 "dog"} {1_4_5 "cat"}
475 ///
476 /// Uiua does not have dedicated boolean logical operators.
477 /// [maximum] can be used as a logical OR.
478 /// ex: ◡↥⊓⌟≤≥5 8 . [6 2 5 9 6 5 0 4]
479 ///
480 /// See also: [minimum]
481 (2, Max, DyadicPervasive, ("maximum", '↥')),
482 /// Take the arctangent of two numbers
483 ///
484 /// This takes a `y` and `x` argument and returns the angle in radians in the range `(-π, π]`.
485 /// ex: ∠ 1 0
486 /// ex: ∠ ¯1 0
487 /// ex: ∠ √2 √2
488 ///
489 /// [un][atangent] gives the [sine] and `cosine` of an angle.
490 /// ex: °∠ 0
491 /// ex: °∠ η
492 /// ex: °∠ π
493 /// ex: °∠ π/3
494 /// This means it can be combined with [divide] to get the tangent.
495 /// ex: ˜÷°∠ π/3
496 (2, Atan, DyadicPervasive, ("atangent", '∠')),
497 /// Make a complex number
498 ///
499 /// The first argument is the imaginary part, and the second argument is the real part.
500 /// ex: ℂ 3 5
501 /// ex: ℂ [0 1 2] [3 4 5]
502 /// [complex] is equivalent to `add``multiply``i`.
503 /// You can use [absolute value] to get the magnitude of the complex number.
504 /// ex: ⌵ ℂ3 4
505 /// You can use [sign] to normalize the complex number to a magnitude of 1.
506 /// ex: ± ℂ3 4
507 /// You can use [un][complex] to get the imaginary and real parts back out.
508 /// ex: [°ℂ] i
509 /// ex: [°ℂ] ×. ℂ3 4
510 /// A complex number [equals] a real one if the imaginary part is 0 and the real parts [match].
511 /// ex: = 5 ℂ0 5
512 (2, Complex, DyadicPervasive, ("complex", 'ℂ')),
513 /// Get the number of rows in an array
514 ///
515 /// ex: ⧻5
516 /// ex: ⧻[]
517 /// ex: ⧻1_2_3
518 /// ex: ⧻[1_2 3_4 5_6]
519 ///
520 /// [length] is equivalent to the [first] of the [shape].
521 /// ex: ⧻[1_2_3 4_5_6]
522 /// : ⊢△[1_2_3 4_5_6]
523 ///
524 /// Use [un][by][length] to set the [length] of an array. Over-taking will cycle rows.
525 /// ex: °⊸⧻ 5 "hello, world"
526 /// : °⊸⧻ 10 "abc"
527 ///
528 /// Subscripted [length] gets the length of a certain axis
529 /// ex: ⧻₁ °△2_3_4
530 (1, Len, MonadicArray, ("length", '⧻')),
531 /// Get the dimensions of an array
532 ///
533 /// ex: △5
534 /// ex: △[]
535 /// ex: △1_2_3
536 /// ex: △[1_2 3_4 5_6]
537 ///
538 /// [un][shape] creates an array of incrementing elements with the given shape.
539 /// ex: °△ 2_3_4
540 ///
541 /// Subscripted [shape] gets the shape from the first few axes.
542 /// ex: △₂ °△2_3_4_5
543 ///
544 /// It is a triangle`△` because a triangle is a shape.
545 (1, Shape, MonadicArray, ("shape", '△')),
546 /// Make an array of all natural numbers less than a number
547 ///
548 /// The rank of the input must be `0` or `1`.
549 /// ex: ⇡5
550 /// ex: ⇡2_3
551 /// ex: ⇡[3]
552 ///
553 /// When creating ranges with upper bounds that are rank `1`, [pick]ing the generated range array from an array with the [shape] of the input will yield that array.
554 /// ex: [1_2_3 4_5_6]
555 /// : △[1_2_3 4_5_6]
556 /// : ⇡△[1_2_3 4_5_6]
557 /// : ⊡⇡△.[1_2_3 4_5_6]
558 ///
559 /// Taking the [range] of a negative number will yield a decreasing sequence starting at `¯1`.
560 /// ex: ⇡¯5
561 /// [pick]ing from an array with the [range] of its [negate]d [shape] will reverse all elements.
562 /// ex: [1_2_3 4_5_6]
563 /// : ⊡⇡¯△. [1_2_3 4_5_6]
564 /// : ⍜♭⇌ [1_2_3 4_5_6]
565 ///
566 /// Subscripted [range] changes the offset of the range.
567 /// ex: ⇡₁ 5
568 /// : ⇡₂ 5
569 /// ex: ⇡₁ 2_3
570 (1, Range, MonadicArray, ("range", '⇡')),
571 /// Get the first row of an array
572 ///
573 /// ex: ⊢1_2_3
574 /// ex: ⊢[1_2 3_4 5_6]
575 /// ex: ⊢1
576 /// ex! ⊢[]
577 ///
578 /// [under][first] allows you to modify the first row of an array.
579 /// ex: ⍜⊢(×10) [2 3 4]
580 ///
581 /// Subscripted [first] puts the first N rows of an array onto the stack.
582 /// ex: ⊢₂ [1 2 3 4]
583 ///
584 /// See also: [last]
585 (1, First, MonadicArray, ("first", '⊢')),
586 /// Get the last row of an array
587 ///
588 /// ex: ⊣1_2_3
589 /// ex: ⊣[1_2 3_4 5_6]
590 /// ex: ⊣1
591 /// ex! ⊣[]
592 ///
593 /// [under][last] allows you to modify the last row of an array.
594 /// ex: ⍜⊣(×10) [2 3 4]
595 ///
596 /// Subscripted [last] puts the last N rows of an array onto the stack.
597 /// ex: ⊣₂ [1 2 3 4]
598 ///
599 /// See also: [first]
600 (1, Last, MonadicArray, ("last", '⊣')),
601 /// Reverse the rows of an array
602 ///
603 /// ex: ⇌1_2_3_9
604 /// ex: ⇌[1_2 3_4 5_6]
605 /// ex: ⇌"Hello!"
606 (1, Reverse, MonadicArray, ("reverse", '⇌')),
607 /// Make an array 1-dimensional
608 ///
609 /// ex: ♭5
610 /// ex: ♭[1 2 3]
611 /// ex: ♭.[1_2 3_4 5_6]
612 /// Subscripted [deshape] collapses the upper dimensions of the array until it is the given rank.
613 /// ex: △ ♭ °△2_3_4_5
614 /// : △ ♭₂ °△2_3_4_5
615 /// : △ ♭₃ °△2_3_4_5
616 /// Negative subscripts are relative to the rank of the array.
617 /// ex: △ ♭₋₁ °△2_3_4_5
618 /// : △ ♭₋₂ °△2_3_4_5
619 /// : △ ♭₋₃ °△2_3_4_5
620 /// If the subscript rank is greater than the rank of the array, length-1 axes are added to the front for the shape.
621 /// ex: ♭₂ [1 2 3]
622 /// : ♭₃ [1 2 3]
623 /// A subscript of `0` gives the first scalar in the array.
624 /// ex: ♭₀ [4_2_6 0_3_7]
625 ///
626 /// It looks like `♭` because it *flat*tens the array.
627 ///
628 /// See also: [reshape]
629 (1, Deshape, MonadicArray, ("deshape", '♭')),
630 /// Add a length-1 axis to an array
631 ///
632 /// ex: ¤5
633 /// ex: ¤¤5
634 /// ex: ¤[1 2 3]
635 /// ex: ¤¤[1 2 3]
636 /// This is useful when combine with [rows] or [table] to re-use an entire array for each row of others.
637 /// ex: ≡⊂ ¤ 1_2_3 4_5_6
638 /// [fix] can also be used with pervasive dyadic functions.
639 /// ex: - [1 2 3] [4 5 6]
640 /// : - ¤[1 2 3] [4 5 6]
641 /// : - [1 2 3] ¤[4 5 6]
642 /// ex! - 1_3 [3_4 5_6 7_8]
643 /// ex: - ¤1_3 [3_4 5_6 7_8]
644 /// [fix]'s name come from the way it "fixes" an array in this way.
645 /// See the [More Array Manipulation Tutorial](/tutorial/morearray) for more information on this use case.
646 (1, Fix, MonadicArray, ("fix", '¤')),
647 /// Encode an array as bits (LSB-first)
648 ///
649 /// **Warning**: Due to floating point imprecision, conversion (both [bits] and [un][bits]) performed on large numbers (over 53 bits long) may give incorrect results.
650 ///
651 /// The result will always be 1 rank higher than the input.
652 /// ex: ⋯27
653 /// ex: ⋯⇡8
654 /// ex: ⋯[1_2 3_4 5_6]
655 ///
656 /// [un][bits] can be used to decode the bits back into numbers.
657 /// ex: °⋯ [1 0 1]
658 /// ex: °⋯ [0 1 1 0 1]
659 /// ex: °⋯ [[0 1 1]
660 /// : [1 0 0]
661 /// : [1 1 0]]
662 ///
663 /// [under][bits] can be used to perform bit-wise operations.
664 /// ex: ⍜⋯(¬⬚0↙8) 5
665 ///
666 /// Subscripted [bits] forces the number of bits to be used. This extends or truncates the bits.
667 /// ex: ⋯₄ [1 2 3]
668 /// ex: ⋯ 1234
669 /// : ⋯₈ 1234
670 (1, Bits, MonadicArray, ("bits", '⋯')),
671 /// Rotate the shape of an array
672 ///
673 /// ex: ⍉.[1_2 3_4 5_6]
674 /// ex: ⍉.[[1_2 3_4] [5_6 7_8]]
675 /// [un][transpose] transposes in the opposite direction.
676 /// This is useful for arrays with rank `greater than``2`.
677 /// ex: °⍉ .⊟.[1_2_3 4_5_6]
678 ///
679 /// `shape``transpose` is always equivalent to `rotate``1``shape`.
680 /// ex: [1_2 3_4 5_6]
681 /// : ⊃(△⍉|↻1△)
682 ///
683 /// Multiple [transpose]s, as well as [rows][transpose], are optimized in the interpreter to only do a single operation.
684 (1, Transpose, MonadicArray, ("transpose", '⍉')),
685 /// Sort an array
686 ///
687 /// ex: ⍆ [3 9 1 8 2 7]
688 /// ex: ⍆ "uiua"
689 /// Multidimensional arrays have their rows sorted lexicographically.
690 /// ex: ⍆ . [1_5_3 4_3_2 1_5_2]
691 /// [sort] is equivalent to [select][by][rise]
692 /// ex! ⍆ "uiua"
693 /// : ⊏⊸⍏ "uiua"
694 /// If you want to sort by some key rather than the data itself, use [rise] or [fall].
695 /// [un][sort] shuffles an array.
696 /// ex: °⍆ [1 2 3 4]
697 /// : °⍆ [1 2 3 4]
698 /// [under][sort] sort reverses the sorting operation when undoing.
699 /// ex: ⍜⍆(↻1) . [3 1 5 2 4]
700 ///
701 /// The current [sort] implementation is a parallel [Introsort](https://en.wikipedia.org/wiki/Introsort). It has O(n log n) worst-case time complexity and O(log n) space complexity. It sorts the array in place and allocates no heap memory.
702 /// If an array is rank-`1` and known to be all bytes, it will be sorted with counting sort.
703 (1, Sort, MonadicArray, ("sort", '⍆')),
704 /// Get the indices into an array if it were sorted ascending
705 ///
706 /// The [rise] of an array is the list of indices that would sort the array ascending if used with [select].
707 /// ex: ⍏ 6_2_7_0_¯1_5
708 /// Using the [rise] as a selector in [select] yields the sorted array.
709 /// ex! ⊏⍏. 6_2_7_0_¯1_5
710 /// ex! ⊏⊸⍏ 6_2_7_0_¯1_5
711 /// This can also be done with [sort].
712 /// If we transform the array before [rise]ing, we can sort by a key.
713 /// Here, we sort the array ascending by the [absolute value] of its elements.
714 /// ex: ⊏⍏⌵. 6_2_7_0_¯1_5
715 ///
716 /// [first][rise] and [first][reverse][rise] are optimized in the interpreter to be O(n).
717 (1, Rise, MonadicArray, ("rise", '⍏')),
718 /// Get the indices into an array if it were sorted descending
719 ///
720 /// The [fall] of an array is the list of indices that would sort the array descending if used with [select].
721 /// ex: ⍖ 6_2_7_0_¯1_5
722 /// Using the [fall] as a selector in [select] yields the sorted array.
723 /// ex: ⊏⍖. 6_2_7_0_¯1_5
724 /// ex: ⊏⊸⍖ 6_2_7_0_¯1_5
725 /// This can also be done with [reverse][sort].
726 /// If we transform the array before [fall]ing, we can sort by a key.
727 /// Here, we sort the array descending by the [absolute value] of its elements.
728 /// ex: ⊏⍖⌵. 6_2_7_0_¯1_5
729 ///
730 /// [first][fall] and [first][reverse][fall] are optimized in the interpreter to be O(n).
731 (1, Fall, MonadicArray, ("fall", '⍖')),
732 /// Get indices where array values are not equal to zero
733 ///
734 /// The most basic use is to convert a mask into a list of indices.
735 /// ex: ⊚ [1 0 0 1 0 1 1 0]
736 /// ex: ⊚.=0◿3.[1 0 2 9 3 8 3 4 6]
737 /// It also works for counts `greater than` 1.
738 /// ex: ⊚ 1_2_3
739 /// ex: ⊚ 1_4_2
740 /// [where] on a list is equivalent to `backward``keep``un``select`
741 /// ex: ⊚ [0 1 0 0 2 0 1]
742 /// ex: ˜▽°⊏ [0 1 0 0 2 0 1]
743 ///
744 /// [un][where] will convert the indices back into a a list of counts
745 /// ex: °⊚ [0 0 0 1 1 2 2 2 2 2 3]
746 /// The indices need not be in order
747 /// ex: °⊚ [0 1 2 2 0 3 2 1 2 0 2]
748 ///
749 /// [where] can be used on multidimensional arrays, and the result will always be rank-2
750 /// ex: ⊚.[1_0_0 0_1_1 0_2_0]
751 /// The inverse works as well
752 /// ex: °⊚[3_4 2_1 0_3]
753 ///
754 /// [where] on a scalar is equivalent to [where] on a singleton array of that scalar, and so creates a list of `0`s.
755 /// ex: ⊚3
756 /// ex: ⊚8
757 (1, Where, MonadicArray, ("where", '⊚')),
758 /// Remove duplicate rows from an array
759 ///
760 /// ex: ◴ 7_7_8_0_1_2_0
761 /// ex: ◴ "Hello, World!"
762 /// ex: ◴ [3_2 1_4 3_2 5_6 1_4 7_8]
763 (1, Deduplicate, MonadicArray, ("deduplicate", '◴')),
764 /// Assign a unique index to each unique row in an array
765 ///
766 /// ex: ⊛7_7_8_0_1_2_0
767 /// ex: ⊛"Hello, World!"
768 ///
769 /// When combined with [group], you can do things like counting the number of occurrences of each character in a string.
770 /// ex: $ Count the characters in this string
771 /// : ⊕($"_ _"⊃⊢⧻) ⊛.⍆
772 (1, Classify, MonadicArray, ("classify", '⊛')),
773 /// Mark each row of an array with its occurrence count
774 ///
775 /// Each row in the array becomes a number corresponding to the number of times it has already appeared in the array.
776 /// ex: ⧆ "aabccdab"
777 /// ex: ⧆ "lego helmet"
778 /// ex: ⧆ [1_2 4_3 1_2 3_0]
779 ///
780 /// Subscripted [occurrences] marks `1` the first N occurrences of each row and `0` for the rest.
781 /// ex: ⧆₁ "lego helmet"
782 /// ex: ⧆₂ "aaaabcbcbcbc"
783 (1, Occurrences, MonadicArray, ("occurrences", '⧆')),
784 /// Get a mask of first occurrences of items in an array
785 ///
786 /// ex: ◰ 7_7_8_0_1_2_0
787 /// ex: ◰ "Hello, World!"
788 /// ex: ◰ [3_2 1_4 3_2 5_6 1_4 7_8]
789 /// [keep][by][unique] is equivalent to [deduplicate].
790 /// ex: ▽⊸◰ 7_7_8_0_1_2_0
791 /// [unique] is mainly useful for deduplicating by a certain property.
792 /// Here, we deduplicate by the [absolute value] of the elements.
793 /// ex: ▽◰⊸⌵ [1 ¯2 ¯5 2 3 1 5]
794 (1, Unique, MonadicArray, ("unique", '◰')),
795 /// Turn an array into a box
796 ///
797 /// This is Uiua's primary way to create nested or mixed-type arrays.
798 /// Normally, arrays can only be created if their rows have the same shape and type.
799 /// [fill] can help you with the shape part, but it is not always wanted, and it can't help with the type part.
800 /// ex! [@a 3 7_8_9]
801 /// [box] creates a box element that contains the array. All boxes, no matter the type of shape of their contents, are considered the same type and can be put into arrays together.
802 /// ex: [□@a □3 □7_8_9]
803 /// The more ergonomic way to make box arrays is to use `{}`s instead of `[]`s.
804 /// ex: {@a 3 7_8_9}
805 /// Use [un][box] to get the values back out.
806 /// ex: °□ □1_2_3
807 /// Use [un] with `{}`s, [dip]s, and [identity] to get the values back onto the stack
808 /// ex: °{⊙⊙∘} {@a 3 7_8_9}
809 ///
810 /// You would not normally construct arrays like the one above.
811 /// The more important use case of [box] is for jagged or nested data.
812 /// If you want to collect unevenly-sized groups from [partition] or [group], without [fill]ing, you must use [box].
813 /// ex: $ Words of different lengths
814 /// : ⊜□≠@ .
815 ///
816 /// Pervasive functions work through boxes and preserve the maximum [box] depth of their arguments.
817 /// ex: ¯ 1
818 /// : ¯ □1
819 /// : ¯ □□1
820 /// ex: +1 4
821 /// : +1 □4
822 /// : +1 □□4
823 /// : +□□1 4
824 /// There is an exception for comparison functions, which compare lexicographically if both arguments are boxes.
825 /// ex: = [1 2 3] [1 2 5]
826 /// : = □[1 2 3] □[1 2 5]
827 /// : > [1 2 3] [1 2 5]
828 /// : > □[1 2 3] □[1 2 5]
829 /// : > "banana" "orange"
830 /// : > □"banana" □"orange"
831 /// : > □"banana" "orange"
832 ///
833 /// For non-pervasive functions, boxed arrays need to be [un][box]ed before they can be operated on.
834 /// ex: ⊢ □[1 2 3]
835 /// ex: ⊢ °□[1 2 3]
836 /// [under][un][box] is useful when you want to re-[box] the result.
837 /// ex: $ Reverse these words
838 /// : ⊜□≠@ .
839 /// : ≡⍜°□⇌.
840 /// ex: {"Hey" "there" "world"}
841 /// : ≡⍜°□(⊂⊢.)
842 /// [under][un][box] works because `un``un``box` is just `box`. For each element, it [un][box]es the array out, does something to it, then [box]es the result.
843 /// ex: .{1_2_3 4_5 [7]}
844 /// : ≡⍜°□(⬚0↙3)
845 /// However, [rows][under][un][box] is such a common pattern, that this is what the [inventory] modifier does.
846 /// ex: PrepLen ← $"_ _"⧻.
847 /// : .⊜□≠@ . $ Prepend the word length
848 /// : ⍚PrepLen
849 /// If you do not need to re-[box] the result, you can use [content] instead.
850 /// [content] [un][box]es all box elements that are passed to a function before calling it.
851 /// ex: {1_2_3 9_2 5_5_5_5}
852 /// : ≡◇/+
853 /// This is the main way to [join] a list of [box]ed strings.
854 /// ex: /◇⊂ {"Join" "these" "strings"}
855 /// ex: /◇(⊂⊂⊙@ ) {"Join" "these" "strings"}
856 ///
857 /// Subscripted [box] combines that many values into a list of boxes.
858 /// ex: □₂ 5 "abc"
859 /// ex: □₃ 1 2_3 4_5_6
860 /// ex: □₀
861 (1, Box, MonadicArray, ("box", '□')),
862 /// Parse a string as a number
863 ///
864 /// ex: ⋕ "17"
865 /// ex: ⋕ "3.1415926535897932"
866 /// ex: ⋕ "1/2"
867 /// ex! ⋕ "dog"
868 ///
869 /// [parse] is semi-pervasive. It works on multidimensional arrays of characters or boxes.
870 /// ex: ⋕ {"5" "24" "106"}
871 /// ex: ⋕ .↯3_4 "012"
872 ///
873 /// [parse] can take a subscript to parse in a different base. Bases `1` through `36` are supported as well as [Base64](https://en.wikipedia.org/wiki/Base64).
874 /// ex: ⋕₁₆"beef"
875 /// ex: ⋕₂ "-101010"
876 ///
877 /// [un][parse] will convert a scalar number into a string.
878 /// ex: °⋕ 58
879 /// ex: °⋕ 6.283185307179586
880 /// ex: °⋕₁₆ 250.9375
881 /// [un][parse] on a non-scalar number array will [box] each string.
882 /// ex: °⋕ 1_2_3
883 /// ex: °⋕ ↯3_4⇡12
884 ///
885 /// [parse] accepts both `a+bi` and `arbi` formats, [un][parse] returns a string using the latter.
886 /// ex: ∩⋕ "8+3i" "8-3i"
887 /// ex: ∩⋕ "8r3i" "8r-3i"
888 /// ex: °⋕ ℂ3 8
889 ///
890 /// [fill][parse] sets a default value for failed parses.
891 /// ex: ⬚5⋕ {"13" "124" "not a number"}
892 /// [fill][un][parse] pads the strings to make a character array instead of a box array.
893 /// ex: ⬚@ °⋕ +9÷4⇡10
894 /// ex: ⬚@0°⋕ +9÷4⇡10
895 (1, Parse, Encoding, ("parse", '⋕')),
896 /// Check if two arrays are exactly the same
897 ///
898 /// ex: ≍ 1_2_3 [1 2 3]
899 /// ex: ≍ 1_2_3 [1 2]
900 ///
901 /// Although one number [equals] another, they may not [match] if they have different [type]s
902 /// ex: = 5 ℂ0 5
903 /// : ≍ 5 ℂ0 5
904 (2, Match, DyadicArray, ("match", '≍')),
905 /// Combine two arrays as rows of a new array
906 ///
907 /// [length] of the coupled array will *always* be `2`.
908 ///
909 /// For scalars, it is equivalent to [join].
910 /// ex: ⊟ 1 2
911 /// : ⊂ 1 2
912 /// For arrays, a new array is created with the first array as the first row and the second array as the second row.
913 /// ex: ⊟ [1 2 3] [4 5 6]
914 /// [un][couple] uncouples a [length] `2` array and pushes both rows onto the stack.
915 /// ex: °⊟ .[1_2_3 4_5_6]
916 /// ex: °⊟ [1_2 3_4]
917 ///
918 /// If one array's shape is a suffix of the other's, the smaller array will be repeated to match the shape of the larger array.
919 /// ex: ⊟ [1 2 3] 4
920 /// ex: ⊟ [1_2 3_4] 5
921 /// ex: ⊟ [1_2 3_4] 5_6
922 ///
923 /// Subscripted [couple] combines that many arrays.
924 /// ex: ⊟₃ 1_2 3_4 5_6
925 /// ex: ⊟₄ @a @b @c @d
926 /// ex: ⊟₁ 5
927 /// ex: ⊟₀
928 ///
929 /// By default, arrays with different shape suffixes cannot be [couple]d.
930 /// ex! ⊟ [1 2 3] [4 5]
931 /// Use [fill] to make their shapes match
932 /// ex: ⬚∞⊟ [1 2 3] [4 5]
933 (2, Couple, DyadicArray, ("couple", '⊟')),
934 /// Append two arrays end-to-end
935 ///
936 /// For scalars, it is equivalent to [couple].
937 /// ex: ⊂ 1 2
938 /// : ⊟ 1 2
939 ///
940 /// If the arrays have the same rank, it will append the second array to the first.
941 /// ex: ⊂ [1 2] [3 4]
942 /// ex: ⊂ [1_2 3_4] [5_6 7_8]
943 ///
944 /// If the arrays have a rank difference of 1, then the array with the smaller rank will be prepended or appended to the other as a row.
945 /// ex: ⊂ 1 [2 3]
946 /// ex: ⊂ [1 2] 3
947 /// ex: ⊂ 1_2 [3_4 5_6]
948 /// ex: ⊂ [1_2 3_4] 5_6
949 ///
950 /// If the arrays have a rank difference of 2 or more, then the array with the smaller rank will be repeated as rows to match the rank of the other.
951 /// This still requires the shape of the smaller array to be a suffix of the shape of the larger array.
952 /// ex: ⊂ 0 [1_2 3_4]
953 /// ex: ⊂ 1_2 [[3_4 5_6] [7_8 9_10]]
954 ///
955 /// By default, arrays that do not have equal [shape] suffixes cannot be [join]ed.
956 /// ex! ⊂ [1_2 3_4] [5_6_7 8_9_10]
957 /// Use [fill] to make their shapes compatible.
958 /// ex: ⬚0⊂ [1_2 3_4] [5_6_7 8_9_10]
959 ///
960 /// [un][join] splits the first row of the array from the rest.
961 /// ex: °⊂ [1 2 3 4]
962 /// ex: °⊂ [1_2 3_4 5_6]
963 ///
964 /// Numeric subscripted [join] joins that many arrays.
965 /// ex: ⊂₃ 1 2_3_4 5_6
966 /// ex: ⊂₄ "al" "ig" "at" "or"
967 ///
968 /// [join]ing to the front of an array is a bit slower than [join]ing to the back because it requires all the existing rows to be shifted.
969 ///
970 /// [join]'s glyph is `⊂` because it kind of looks like a magnet pulling its two arguments together.
971 (2, Join, DyadicArray, ("join", '⊂')),
972 /// Select multiple rows from an array
973 ///
974 /// For a scalar selector, [select] is equivalent to [pick].
975 /// ex: ⊏ 2 [8 3 9 2 0]
976 /// : ⊡ 2 [8 3 9 2 0]
977 /// For a rank `1` selector, [select] will pick multiple items from an array.
978 /// ex: ⊏ 4_2 [8 3 9 2 0]
979 /// ex: ⊏ 0_2_1_1 [1_2_3 4_5_6 7_8_9]
980 /// If the selector's rank is `greater than``1`, then each row of the selector will be selected separately.
981 /// ex: ⊏ [0_1 1_2 2_3] [2 3 5 7]
982 /// ex: ⊏ [0_1 1_2 2_0] [1_2_3 4_5_6 7_8_9]
983 ///
984 /// Negative indices select from the end.
985 /// ex: ⊏¯1 "hello"
986 /// ex: ⊏¯[1 3 5] "hello"
987 ///
988 /// [fill] allows you to set a default value for when an index is out of bounds.
989 /// ex: ⬚@-⊏[4 7 2 6 1] "hello!"
990 /// Negative indices will always use the fill value if there is one.
991 /// ex: ⬚@-⊏[¯2 ¯1 0 1 2 3 4 5 6] "hello!"
992 ///
993 /// [un][select] is equivalent to [range][length][duplicate]. This is a common way to enumerate the indices of the rows an array.
994 /// ex: °⊏ "hello!"
995 /// ex: °⊏ {1 2_3 4_5_6}
996 ///
997 /// [under][select] can be used to modify, replace, insert, or delete the rows of an array.
998 /// ex: ⍜⊏(×10) 1_5 ⇡10
999 /// ex: ⍜⊏⋅π 1_5 ⇡10
1000 /// ex: ⍜⊏⋅η_τ 1_5 ⇡10
1001 /// ex: ⍜⊏≡⋅[] 1_5 ⇡10
1002 /// ex: ⍜⊏≡⋅η_τ_π 1_5 ⇡10
1003 ///
1004 /// [anti][select] puts the rows of an array at their corresponding indices. This requires a [fill] value if not all indices are present.
1005 /// ex: ⌝⊏ 3_1_2_0 "abcd"
1006 /// ex: ⬚@-⌝⊏ 1_2_5 "abc"
1007 /// ex: ⬚@.⌝⊏ [1_5 7_2] ["ab" "cd"]
1008 (2, Select, DyadicArray, ("select", '⊏')),
1009 /// Index a row or elements from an array
1010 ///
1011 /// An index with rank `0` or `1` will pick a single row or element from an array.
1012 /// ex: ⊡ 2 [8 3 9 2 0]
1013 /// ex: ⊡ 1_1 .[1_2_3 4_5_6]
1014 ///
1015 /// If the index's rank is `2` or greater, then multiple rows or elements will be picked.
1016 /// ex: ⊡ [1_2 0_1] [1_2_3 4_5_6]
1017 ///
1018 /// [un][pick] is equivalent to [range][shape][duplicate]. This is a common way to enumerate the indices of the elements of an array.
1019 /// ex: °⊡ "hello!"
1020 /// ex: °⊡ ["ab" "cd"]
1021 ///
1022 /// [under][pick] can be used to modify or replace the value at an index.
1023 /// ex: ⍜⊡(×10) 2 [8 3 9 2 0]
1024 /// This works with multiple and/or deeper indices.
1025 /// ex: ⍜⊡(×10) [2_1 0_2] +1↯3_4⇡12
1026 /// To simply set a value, you can use [under][pick][pop].
1027 /// ex: ⍜⊡◌ 2 [8 3 9 2 0] 42
1028 /// Or [under][pick][gap] if the replacement is static.
1029 ///
1030 /// [anti][pick] puts the values of an array at their corresponding indices. This requires a [fill] value if not all indices are present.
1031 /// ex: ⬚0⌝⊡ 1_2 5
1032 /// ex: ⬚0⌝⊡ 1_1 1_2
1033 /// ex: ⬚@-⌝⊡ [1_2 3_4] "ab"
1034 /// ex: ⌝⊡ [1_0 0_0 1_1 0_1] "abcd"
1035 (2, Pick, DyadicArray, ("pick", '⊡')),
1036 /// Change the shape of an array
1037 ///
1038 /// ex: ↯ 2_3 [1 2 3 4 5 6]
1039 /// Shapes that have fewer elements than the original array will truncate it.
1040 /// ex: ↯ 2_2 [1_2_3 4_5_6]
1041 /// Shapes that have more elements than the original array will cycle elements.
1042 /// ex: ↯ [5] 2
1043 /// ex: ↯ 3_7 1_2_3_4
1044 ///
1045 /// Scalar shapes will copy the array as rows of a new array.
1046 /// ex: ↯ 4 [1 2 3 4 5]
1047 /// ex: ↯ 2 [1_2_3 4_5_6]
1048 /// This is in contrast to scalar [keep], which repeats each row but preserves rank.
1049 /// ex: ▽ 4 [1 2 3 4 5]
1050 /// ex: ▽ 2 [1_2_3 4_5_6]
1051 ///
1052 /// [fill][reshape] fills in the shape with the fill element instead of cycling the data.
1053 /// ex: ↯ 3_5 ⇡9
1054 /// : ⬚0↯ 3_5 ⇡9
1055 ///
1056 /// At most one of the dimensions of the new shape may be [infinity]. This indicates that this is a *derived* dimension, and it will be calculated to make the total number of elements in the new shape be `less or equal` the total number of elements in the original shape.
1057 /// ex: ↯5_∞ ⇡15
1058 /// ex: ↯∞_5 ⇡15
1059 /// ex: ↯2_2_∞ ⇡15
1060 /// ex: ↯∞_2_2 ⇡15
1061 /// ex: ↯3_∞_5 ⇡30
1062 /// If [fill] is used, the total number of elements in the new shape will always be `greater or equal` the total number of elements in the original shape.
1063 /// ex: ⬚0↯ ∞_5 ⇡12
1064 ///
1065 /// [under][shape] will [reshape] the array as an inverse.
1066 /// ex: ⍜△⇌. ↯2_3_4⇡24
1067 ///
1068 /// Negative axes in the shape will reverse the corresponding axes of the array.
1069 /// ex: ↯[¯3] 1_2_3
1070 /// ex: ↯2_3_4⇡24
1071 /// : ⍜△⍜(⊏0_2)¯
1072 /// ex: ↯¯3 [1 2 3 4]
1073 /// ex: ↯¯∞ [1 2 3 4 5]
1074 ///
1075 /// See also: [deshape]
1076 (2, Reshape, DyadicArray, ("reshape", '↯')),
1077 /// Change the rank of an array's rows
1078 ///
1079 /// The resulting array will always have the given rank plus `1`.
1080 /// ex: ☇ 0 ↯2_3_3⇡18
1081 /// : ☇ 1 ↯2_3_3⇡18
1082 /// : ☇ 2 ↯2_3_3⇡18
1083 /// Ranks greater than the rank of the original rows will prepend `1` to the array's [shape].
1084 /// ex: ☇ 2 [1 2 3 4]
1085 /// ex: ☇ 3 ↯2_3_3⇡18
1086 /// : ☇ 4 ↯2_3_3⇡18
1087 /// Negative ranks are relative to the rank of the array.
1088 /// ex: ☇ ¯1 ↯2_3_3⇡18
1089 /// : ☇ ¯2 ↯2_3_3⇡18
1090 /// : ☇ ¯3 ↯2_3_3⇡18
1091 ///
1092 /// [under][rerank] will set the rank back when it is done.
1093 /// ex: ⍜(☇1)≡□ ↯2_3_3⇡18
1094 /// ex: ⍜☇≡□ 2 ↯2_3_3⇡18
1095 (2, Rerank, DyadicArray, ("rerank", '☇')),
1096 /// Take the first n rows of an array
1097 ///
1098 /// This is the opposite of [drop].
1099 ///
1100 /// ex: ↙ 3 [8 3 9 2 0]
1101 /// ex: ↙ 2 ↯3_3⇡9
1102 /// Negative amounts take from the end.
1103 /// ex: ↙ ¯3 [8 3 9 2 0]
1104 /// ex: ↙ ¯2 ↯3_3⇡9
1105 /// The amount to take can also be a list to take along multiple axes.
1106 /// ex: .↯3_4⇡12
1107 /// : ⊃(↙¯2_¯2|↙2_3)
1108 ///
1109 /// By default, taking more than the length of the array will throw an error.
1110 /// ex! ↙7 [8 3 9 2 0]
1111 /// If you would like to fill the excess length with some fill value, use [fill].
1112 /// ex: ⬚π↙ 7 [8 3 9 2 0]
1113 /// This works with negative values as well.
1114 /// ex: ⬚π↙ ¯7 [8 3 9 2 0]
1115 ///
1116 /// [infinity] can be used to take every row along an axis.
1117 /// ex: ↯2_3_4⇡24
1118 /// : ↙¯1_∞_2.
1119 ///
1120 /// [un][fill]ed [take] will trim rows from the end of an array and return the pre-trimmed dimensions.
1121 /// ex: °⬚@-↙ "abc-----"
1122 /// ex: °⬚0↙ [10_20_0_0 30_40_0_0 0_0_0_0]
1123 (2, Take, DyadicArray, ("take", '↙')),
1124 /// Drop the first n rows of an array
1125 ///
1126 /// This is the opposite of [take].
1127 ///
1128 /// ex: ↘ 3 [8 3 9 2 0]
1129 /// ex: ↘ 2 ↯3_3⇡9
1130 /// Negative amounts drop from the end.
1131 /// ex: ↘ ¯3 [8 3 9 2 0]
1132 /// ex: ↘ ¯2 ↯3_3⇡9
1133 /// The amount to drop can also be a list to drop along multiple axes.
1134 /// ex: .↯3_4⇡12
1135 /// : ⊃(↘¯2_¯1|↘1_2)
1136 ///
1137 /// Dropping more than the length of the array will leave an empty array.
1138 /// ex: ↘ 7 [8 3 9 2 0]
1139 /// ex: ↘ ¯7 [8 3 9 2 0]
1140 /// ex: ↘ 5 ↯3_3⇡9
1141 /// ex: ↘ ¯5 ↯3_3⇡9
1142 ///
1143 /// [anti][drop] pads an array.
1144 /// By default, the pad value is a "zero element" of the array's type.
1145 /// - For number arrays, it is `0`.
1146 /// - For character arrays, it is `@ ` (space).
1147 /// - For complex arrays, it is `0ℂ`.
1148 /// - For box arrays, it is `⟦⟧`.
1149 /// A scalar first argument will pad the first axis of the array on both sides.
1150 /// ex: ⌝↘ 2 [1 2 3]
1151 /// ex: ⌝↘ ¯2 [1 2 3]
1152 /// ex: ⌝↘ 3 "Hello!"
1153 /// ex: ⌝↘ 1 [1_2 3_4]
1154 /// [fill] can be used to set the fill value. Non-scalar fills are allowed if they are compatible with the array's shape.
1155 /// ex: ⬚10⌝↘ 2 [1 2 3]
1156 /// ex: ⬚@-⌝↘ 2 "abc"
1157 /// ex: ⬚10⌝↘ 1 [1_2 3_4]
1158 /// ex: ⬚10_20⌝↘ 1 [1_2 3_4]
1159 /// If the first argument is a list, each axis will be padded on both sides with the corresponding amount.
1160 /// ex: ⌝↘ 1_2 [1_2 3_4]
1161 /// ex: ⌝↘ 1_¯2 [1_2 3_4]
1162 /// ex: ⌝↘ ¯1_2 +1°△2_2_4
1163 /// ex: ⌝↘ ¯1_1_2 +1°△2_2_4
1164 /// ex: ⌝↘ ¯1_0_2 +1°△2_2_4
1165 /// This can be good for padding images.
1166 /// ex: ⬚(⊂⊙1Purple|⌝↘¯⟜⌝↘) 20_20 Logo
1167 (2, Drop, DyadicArray, ("drop", '↘')),
1168 /// Rotate the elements of an array by n
1169 ///
1170 /// ex: ↻1 ⇡5
1171 /// ex: ↻2 ⇡5
1172 /// ex: ↻¯1 ⇡5
1173 /// ex: ↻2 .↯3_4⇡12
1174 ///
1175 /// Multi-dimensional rotations are supported.
1176 /// ex: ↻1_2 .↯4_5⇡20
1177 ///
1178 /// [fill][rotate] fills in array elements instead of wrapping them.
1179 /// ex: ⬚0↻ 2 [1 2 3 4 5]
1180 /// : ↻ 2 [1 2 3 4 5]
1181 /// ex: ⬚0↻ 1_2 .↯4_5⇡20
1182 ///
1183 /// If the rotation amount is rank `2` or greater, multiple copies of the rotated array will be made, each rotated by a different row of the rotation amount.
1184 /// ex: ↻ [[1] [2] [4]] [0 0 0 0 0 0 1]
1185 /// : ↻ ≡¤1_2_4 [0 0 0 0 0 0 1]
1186 /// ex: [.. 0_0_0_0 0_0_0_1]
1187 /// : ↻ [0_0 1_2 0_3]
1188 (2, Rotate, DyadicArray, ("rotate", '↻')),
1189 /// Change the order of the axes of an array
1190 ///
1191 /// The first argument is a list of unique axis indices.
1192 /// The corresponding axes of the array will be moved to the front of the array's shape.
1193 /// Positive indices start from the leading axis. Negative indices start from the trailing axis.
1194 /// ex: °△ 2_3_4
1195 /// : ⤸ 1 .
1196 /// ex: △ ⤸ 2_1 °△ 2_3_4_5
1197 /// [orient]`¯1` is equivalent to [un][transpose].
1198 /// ex: °△ 2_3_4
1199 /// : ∩△ ⊃°⍉(⤸¯1)
1200 ///
1201 /// [fill][orient] uses the fill value to fill in new axes. The elements of the array will be arranged along the diagonals specified by repeated axis indices. The rest of the array will be filled with the fill value.
1202 /// ex: ⬚0⤸ 0_0 [1 2 3 4]
1203 /// ex: ⬚@-⤸ 0_1_1 ["Hello" "World"]
1204 ///
1205 /// [anti][orient] moves the axes *to* the given indices.
1206 /// ex: △ ⤸ 3_1 °△ 2_3_4_5
1207 /// : △ ⌝⤸ 3_1 °△ 2_3_4_5
1208 /// Repeated axis indices will retrieve the diagonal along those axes.
1209 /// ex: ⌝⤸ 0_0 . °△ 3_3
1210 /// ex: ⌝⤸ 0_0_0 . °△ 3_3_3
1211 /// ex: ⌝⤸ 0_0 °△ 3_3_3
1212 /// ex: ⌝⤸ 0_1_1 . °△ 2_2_2
1213 /// ex: ⌝⤸ 1_1_0 °△ 2_2_2
1214 /// ex: ⌝⤸ 1_0_1 °△ 2_2_2
1215 ///
1216 /// [un][orient] is equivalent to [range][length][shape][duplicate]. This is an easy way to enumerate the indices of the axes of an array.
1217 /// ex: °⤸ "hello!"
1218 /// ex: °⤸ ["ab" "cd"]
1219 /// ex: °⤸ [[1_2 3_4] [5_6 7_8]]
1220 ///
1221 /// [under][anti][orient] will put diagonals back into the original array.
1222 /// ex: ⍜⌝⤸¯ 0_0 +1°△4_4
1223 (2, Orient, DyadicArray, ("orient", '⤸')),
1224 /// The n-wise windows of an array
1225 ///
1226 /// [windows] has been deprecated. Use [stencil] instead.
1227 ///
1228 /// ex: ◫2 .⇡4
1229 /// ex: ◫4 .⇡6
1230 ///
1231 /// Multi-dimensional window sizes are supported.
1232 /// ex: ◫2_2 .[1_2_3 4_5_6 7_8_9]
1233 ///
1234 /// Negative window sizes gives the absolute value number of windows.
1235 /// ex: ◫¯2 ↯4_4⇡16
1236 /// ex: ◫¯3 ↯4_4⇡16
1237 /// This can be useful when you want to get horizontal windows.
1238 /// ex: ◫¯1_2 ↯4_4⇡16
1239 ///
1240 /// Usually, [windows] "materializes" the windows. This means that the windows are copied into a new array. While this is very general, it can be slow and wasteful if you end up immediately reducing the windows.
1241 /// For this reason, the pattern `rows``reduce``F``windows` is optimized for scalar window sizes to [reduce] windows as they are generated.
1242 /// ex: ≡/+◫ 5 [1 8 2 9 3 0 2 4 4 5 1] # Fast!
1243 ///
1244 /// You can use [fill] to pad the array with a value.
1245 /// This can be useful for things like convolutions.
1246 /// ex: +1↯2_3⇡6
1247 /// : ⬚0◫2_3
1248 /// : ≡≡□
1249 ///
1250 /// [windows] with a scalar or list window size will always produce overlapping windows that shift by one row at a time.
1251 /// 2-dimensional windows sizes allow more control over the windows.
1252 /// A rank-2 array with only one row will "chunk" the array with non-overlapping windows.
1253 /// ex: ◫[[4]] ⇡12
1254 /// ex: ◫¤¤4 ⇡12
1255 /// ex: ≡≡□ ◫¤[2 2] . °△4_6
1256 /// Negative sizes still specify the number of windows desired.
1257 /// ex: ◫¤¤¯4 ⇡12
1258 /// ex: ≡≡□ ◫¤[¯2 ¯2] . °△4_6
1259 /// A rank-2 array with two rows allows the "stride" of the windows to be specified.
1260 /// The first row specifies the window size, and the second row specifies the stride.
1261 /// ex: ◫[[3] [4]] ⇡12
1262 /// ex: ◫[[4] [2]] ⇡12
1263 /// ex: ≡≡□ ◫[2_2 1_3] . °△4_6
1264 (2, Windows, DyadicArray, ("windows", '◫')),
1265 /// Discard or copy some rows of an array
1266 ///
1267 /// Takes two arrays. The first array is the number of copies to keep of each row of the second array.
1268 /// ex: ▽ [1 0 2 3 1] [8 3 9 2 0]
1269 ///
1270 /// By making the first array a mask derived from the second, [keep] becomes a filter.
1271 /// In this example, and a mask is created from it using `greater or equal``@a`, preserving the original string with [by]. Then, [keep] uses the mask to filter the string.
1272 /// ex: ▽⊸≥@a "lOWERCASe onLY"
1273 ///
1274 /// [keep] with a scalar for the first argument repeats each row of the second argument that many times.
1275 /// ex: ▽ 3 [1 2 3]
1276 /// ex: ▽ 2 [1_2_3 4_5_6]
1277 /// This is in contrast to scalar [reshape], which copies the array as rows of a new array.
1278 /// ex: ↯ 3 [1 2 3]
1279 /// ex: ↯ 2 [1_2_3 4_5_6]
1280 ///
1281 /// The counts list can be [fill]ed if it is shorter than the kept array.
1282 /// ex: ⬚3▽ [1 0 2] [8 3 9 2 0]
1283 /// The fill value may be a list, in which case it will be repeated.
1284 /// ex: ⬚[1 2 0]▽ [0] ⇡10
1285 ///
1286 /// [un][keep] splits an array into a counts list and an array with adjacent similar rows deduplicated.
1287 /// ex: °▽ "mississippi"
1288 ///
1289 /// A non-integer scalar count will either remove or duplicate rows at regular intervals.
1290 /// ex: ▽ 0.5 ⇡10
1291 /// ex: ▽ 1.5 ⇡10
1292 /// A numeric subscripts [keep]s along that many leading axes
1293 /// ex: ▽₂ 3 [1_2 3_4]
1294 ///
1295 /// [under][keep] allows you to modify part of an array according to a mask.
1296 /// ex: ⍜▽(+1) ⊸=@s "mississippi"
1297 /// If the kept array is modified to have a higher rank, each row will be "put back" into a different copy of the original array.
1298 /// ex: ⍜▽(⊞+⇡5) ⊸≠@ "a bcd ef"
1299 ///
1300 /// [anti][keep] puts the rows of an array at the corresponding `1`s and [fill]s the rest.
1301 /// ex: ⬚@-⌝▽ 0_1_1_0_0_1 "abc"
1302 /// ex: ⬚@-⌝▽ 1_0 "abcdefg"
1303 /// ex: ⬚@-⌝▽ 1_1_0 "abcdefg"
1304 /// ex: ⬚0⌝▽ 1_0_1 [1_2_3 4_5_6]
1305 ///
1306 /// [keep]'s glyph is `▽` because its main use is to filter, and `▽` kind of looks like a coffee filter.
1307 (2, Keep, DyadicArray, ("keep", '▽')),
1308 /// Find the occurrences of one array in another
1309 ///
1310 /// A `1` marker will be placed the the start of each occurrence of the first array in the second array.
1311 /// ex: ⌕ 5 [1 8 5 2 3 5 4 5 6 7]
1312 /// ex: ⌕ "ab" "abracadabra"
1313 /// If the searched-in array is multidimensional, the `1` marker will be placed in the minimum index "top left" corner.
1314 /// ex: ⌕ 1_2 . ↯4_4⇡3
1315 /// ex: ⌕ [1_2 2_0] . ↯4_4⇡3
1316 ///
1317 /// If you want to mark the entire occurrence, use [mask] instead.
1318 (2, Find, DyadicArray, ("find", '⌕')),
1319 /// Mask the occurrences of one array in another
1320 ///
1321 /// Occurrences of the first array in the second array will be marked with increasing numbers.
1322 /// While [find] only marks the start of each occurrence, [mask] marks the entire occurrence.
1323 /// ex: ⦷ "ab" "abracadabra"
1324 /// ex: ⦷ [1 2 3].[0 1 2 3 1 2 3 4 5 1 2 3 4 5 6]
1325 /// Increasing numbers are used so that adjacent occurrences can be distinguished.
1326 /// An occurrence that would overlap with a previous occurrence is not marked.
1327 /// ex: ⦷ [3 4 3 4].[0 3 4 3 4 3 4 0 0 3 4 3 4 0]
1328 ///
1329 /// Arbitrary rank arrays are supported.
1330 /// The first array's rank must be `less or equal` the rank of the second.
1331 /// ex: ◡⦷ 3_4 ↯2_3⇡6
1332 /// ex: ◡⦷ [1_2 5_6] [1_2_3_4 5_6_1_2 7_8_5_6 4_3_1_2]
1333 ///
1334 /// [mask] works well with [partition] in a way that [find] does not.
1335 /// Here, we [not] the [mask] of a non-scalar delimiter to split a string.
1336 /// ex: ⊜∘ ¬⊸⦷ " - " "foo - bar - baz"
1337 (2, Mask, DyadicArray, ("mask", '⦷')),
1338 /// Check if each row of one array exists in another
1339 ///
1340 /// The second argument is checked for membership in the first argument.
1341 /// ex: ∊ [1 2 3] 2
1342 /// ex: ∊ [1 2 3] 5
1343 /// ex: ∊ [0 3 4 5 1] [1 2 3]
1344 /// ex: ∊ [1_2_3 4_5_6] [4 5 6]
1345 /// ex: ∊ [3 4 5] [1_2_3 4_5_6]
1346 /// ex: ∊ [1_2_3 4_5_6] 2
1347 ///
1348 /// With the help of [keep], you can use [memberof] to get a set intersection.
1349 /// ex: ▽⊸∊ "abracadabra" "that's really cool"
1350 ///
1351 /// [memberof] is closely related to [indexof].
1352 (2, MemberOf, DyadicArray, ("memberof", '∊')),
1353 /// Find the first index in an array of each row of another array
1354 ///
1355 /// ex: ⨂ [1 2 3] 2
1356 /// ex: ⨂ [1_2_3 4_5_6] [4 5 6]
1357 /// ex: ⨂ [1_2_3 4_5_6] 2
1358 /// If the index cannot be found, the [length] of the searched-in array is returned.
1359 /// ex: ⨂ [0 3 4 5 1] [1 2 3]
1360 /// ex: ⨂ [3 4 5] [1_2_3 4_5_6]
1361 /// ex: ⨂ [1 2 3] 5
1362 ///
1363 /// [fill] can be used to set the value of missing items.
1364 /// ex: ⨂ [1 2 3 4] [4 8 2 9 1]
1365 /// : ⬚∞⨂ [1 2 3 4] [4 8 2 9 1]
1366 ///
1367 /// You can use the returned indices with [select] to get the rows that were found.
1368 /// If you expect any of the searched-for rows to be missing, you can use [fill] to set a default value.
1369 /// ex: [1 2 3 4 5]
1370 /// : [2 3 5 7 11 13]
1371 /// : ⬚∞⊏ ⤚⨂
1372 ///
1373 /// [indexin] is closely related to [memberof].
1374 (2, IndexIn, DyadicArray, ("indexin", '⨂')),
1375 /// Find the first index of each row of one array in another
1376 ///
1377 /// ex: ⊗ 2 [1 2 3]
1378 /// ex: ⊗ [4 5 6] [1_2_3 4_5_6]
1379 /// ex: ⊗ 2 [1_2_3 4_5_6]
1380 /// If the index cannot be found, the [length] of the searched-in array is returned.
1381 /// ex: ⊗ [1 2 3] [0 3 4 5 1]
1382 /// ex: ⊗ [1_2_3 4_5_6] [3 4 5]
1383 /// ex: ⊗ 5 [1 2 3]
1384 ///
1385 /// [fill] can be used to set the value of missing items.
1386 /// ex: ⊗ [4 8 2 9 1] [1 2 3 4]
1387 /// : ⬚∞⊗ [4 8 2 9 1] [1 2 3 4]
1388 ///
1389 /// You can use the returned indices with [select] to get the rows that were found.
1390 /// If you expect any of the searched-for rows to be missing, you can use [fill] to set a default value.
1391 /// ex: [2 3 5 7 11 13]
1392 /// : [1 2 3 4 5]
1393 /// : ◡⬚∞⊏ ⤚⊗
1394 ///
1395 /// [indexof] is closely related to [memberof].
1396 (2, IndexOf, DyadicArray, ("indexof", '⊗')),
1397 /// Get sequential indices of each row of an array in another
1398 ///
1399 /// Unlike [indexof], [progressive indexof] will never return the same index twice unless the item is not found.
1400 /// ex: # Experimental!
1401 /// : ⊗ "hello dog" "lego helmet"
1402 /// : ⊘ "hello dog" "lego helmet"
1403 /// When no more rows from the searched-in array match the next row of the searched-for array, the resulting index will be the [length] of the searched-in array.
1404 /// ex: # Experimental!
1405 /// : ⊘ [1 1 1 1 1 1 1 1] [1 1 1 1 0 0 0 0 0 0 0 0]
1406 /// ex: # Experimental!
1407 /// : ⊘ [1 2 3 1 2 3 1 2 3] [1 2 3]
1408 /// If the searched-for array has a greater rank than the searched-in array, the next index for each row of the searched-for array will be tracked separately.
1409 /// Notice here that the indices of the `1`s are `2` and `3` but index of the both `2`s is `4`. This is because the `1`s are in the same row while the `2`s are in different rows.
1410 /// ex: # Experimental!
1411 /// : ⊘ [1_1_2 2_3_3] [0 0 1 1 2 2 3]
1412 ///
1413 /// [fill] can be used to set the value of missing items. This includes rows in the searched-for array that have run out of corresponding items in the searched-in array.
1414 /// ex: # Experimental!
1415 /// : ⊘ [4 8 2 9 1] [1 2 3 4]
1416 /// : ⬚∞⊘ [4 8 2 9 1] [1 2 3 4]
1417 (2, ProgressiveIndexOf, DyadicArray, ("progressive indexof", '⊘'), { experimental: true }),
1418 /// Get the base digits of a number
1419 ///
1420 /// When passed a scalar number, [base] returns the base-N digits of the numbers in an array.
1421 /// Digits are always listed least-significant to most-significant.
1422 /// ex: ⊥ 10 123
1423 /// ex: ⊥ 2 10
1424 /// ex: ⊥ 16 256
1425 /// When passed an array of numbers, [base] treats each digit as having a different base.
1426 /// Any remainder will be truncated.
1427 /// ex: ⊥ [10 2] 35 # Truncated
1428 /// ex: ⊥ [60 60 24 365.25] now
1429 /// If you want to keep the remainder, use [infinity].
1430 /// ex: ⊥ [10 2 ∞] 35
1431 /// ex: ⊥ [60 60 24 365.25 ∞] now
1432 /// [fill] can be used to set a repeating base after the array.
1433 /// ex: ⬚10⊥[12 20] 999999
1434 /// Non-integer bases are supported.
1435 /// ex: ⊥ π [η π τ]
1436 /// ex: ⊥ 1.5 [1 2 3 4 5]
1437 ///
1438 /// [base] is compatible with [under].
1439 /// ex: ⍜(°⍉⊥4|⬚0↙3) [10 100 1000]
1440 /// It can also be used with [anti] to convert digits in a certain base back to numbers.
1441 /// ex: ⌝⊥ 2 [1 0 0 1 0]
1442 /// : ⌝⊥ 2 [1_0_0 0_1_1 1_1_1]
1443 /// : ⌝⊥ 10 [1 2 3]
1444 /// For a scalar base, this is equivalent to evaluating a polynomial.
1445 /// The polynomial x²-2x+1 could be represented like this:
1446 /// ex: ⌝⊥ 0 [1 ¯2 1]
1447 /// : ⌝⊥ 1 [1 ¯2 1]
1448 /// : ⌝⊥ 2 [1 ¯2 1]
1449 /// [anti][base] also works with array bases:
1450 /// ex: ⌝⊥[12 20] [1 12]
1451 /// : ⌝⊥[12 20 ∞] [11 1 3]
1452 /// : ⌝⬚10⊥[12 20] [3 13 6 6 1 4]
1453 (2, Base, DyadicArray, ("base", '⊥')),
1454 /// Apply a reducing function to an array
1455 ///
1456 /// For reducing with an initial value, see [fold].
1457 ///
1458 /// `reduce``add` sums the rows of an array.
1459 /// ex: /+ 1_2_3_4_5
1460 /// [reduce] goes from left to right. This is important for non-commutative functions like [subtract].
1461 /// ex: /- 1_2_3_4_5
1462 /// [reduce] works on arrays of arbitrary rank. The leading-axis rows will always be iterated over.
1463 /// ex: /+ . [1_2_3 4_5_6]
1464 /// ex: /+ . [[0_1 1_0] [2_0 0_0] [0_0 0_3]]
1465 ///
1466 /// If you want to see the intermediate values, you can use [scan].
1467 /// ex: /- 1_2_3_4_5
1468 /// : \- 1_2_3_4_5
1469 ///
1470 /// You can can reduce with arbitrary functions.
1471 /// ex: /(×+1) 1_2_3_4_5
1472 ///
1473 /// [reduce][join] is the simplest way to combine the first two dimensions of an array.
1474 /// It is optimized in the interpreter to be very fast.
1475 /// ex: /⊂ .↯2_2_4⇡16
1476 ///
1477 /// Some functions have default values if the array is empty.
1478 /// Functions without default values will throw an error if the array is empty.
1479 /// ex: /+ []
1480 /// ex: /× []
1481 /// ex: /↥ []
1482 /// ex: /↧ []
1483 /// ex! /∊ []
1484 ///
1485 /// An initial value can be set with [fill].
1486 /// ex: /↥ []
1487 /// ex: ⬚5/↥ []
1488 /// ex: /↥ [1 2 3]
1489 /// ex: ⬚5/↥ [1 2 3]
1490 ///
1491 /// If the function takes more than 2 arguments, additional arguments above the array on the stack will be passed to the function on every iteration. This is useful for things like interspersing one array between the rows of another.
1492 /// ex: /(⊂⊂) 0_1 [2 3 4 5]
1493 /// ex: /◇(⊂⊂) @, {"cat" "bird" "dog"}
1494 ([1], Reduce, AggregatingModifier, ("reduce", '/')),
1495 /// Apply a function to aggregate arrays
1496 ///
1497 /// Expects as many arguments as its function takes.
1498 /// In the simplest case, [fold] can be used to [reduce] an array with a default value.
1499 /// ex: ∧+ [1 2 3] 10
1500 /// : ∧+ [] 10
1501 ///
1502 /// If the function takes at least 1 more argument than it returns:
1503 /// Arguments that are lower on the stack that will be used as accumulators.
1504 /// There will be as many accumulators as the function's outputs.
1505 /// Arguments that are higher on the stack will be iterated over.
1506 /// The number of iterated arrays is the number of arguments minus the number of outputs.
1507 /// The function will be repeatedly called with the rows of the iterated arrays, followed by the accumulators.
1508 /// On each iteration, the returned values will be used as the new accumulators.
1509 ///
1510 /// Multiple accumulators can be used
1511 /// ex: ∧(⊃+(×⊙⋅∘)) +1⇡5 0 1
1512 /// If the iterated array is already on the stack, you can use [dip] to place the accumulators below it.
1513 /// ex: ∧(⊃+(×⊙⋅∘))⊙(0 1) +1⇡5
1514 ///
1515 /// Multiple iterated arrays are also fine.
1516 /// Here, we accumulate the first array with [add] and the second with [multiply].
1517 /// ex: ∧⊃(+⊙⋅∘|×⋅⊙⋅∘) 1_2_3 4_5_6 0 1
1518 ///
1519 /// Like [rows], [fold] will repeat the row of arrays that have exactly one row.
1520 /// ex: ∧(⊂⊂) 1_2_3 4 []
1521 ///
1522 /// If the function returns the same or more values than it takes as arguments:
1523 /// There will be exactly one iterated array. The rest of the arguments will be used as accumulators.
1524 /// Excess outputs will be collected into arrays. When the [fold] is done, these arrays will be placed *below* the accumulators on the stack.
1525 /// This behavior is currently `# Experimental!`.
1526 ///
1527 /// For example, [scan] can be manually reimplemented by [duplicate]ing the result of the function.
1528 /// ex: # Experimental!
1529 /// : ∧(.+) [1 2 3 4 5] 0
1530 /// ex: # Experimental!
1531 /// : ∧(◡⊙∘⊓⌞+×) [1 2 3 4 5] 0 1
1532 ([1], Fold, AggregatingModifier, ("fold", '∧')),
1533 /// Reduce, but keep intermediate values
1534 ///
1535 /// ex: \+ 1_2_3_4
1536 /// ex: \- 1_2_3_4
1537 /// ex: \˜- 1_2_3_4
1538 /// [scan] is often used to do something with masks.
1539 /// [scan]ning with [minimum] or [maximum] will propogate `0`s or `1`s.
1540 /// ex: ▽\↧≠@ . "Hello World!"
1541 /// [scan]ning with [add] and then using [group] can split by a delimiter while keeping the delimiter.
1542 /// ex: ⊕□\+=@ . "Everyday man's on the block"
1543 /// : ⊕□\+↻¯1=@ . "Everyday man's on the block"
1544 ///
1545 /// [fill] both sets the initial value and fills mismatched shapes if necessary.
1546 /// ex: \+ [1 2 3 4 5]
1547 /// : ⬚@a\+ [1 2 3 4 5]
1548 /// ex: +1⇡5
1549 /// : ⬚0\⊂ .
1550 /// : ↘1_1 .
1551 ///
1552 /// If the function takes more than 2 arguments, additional arguments above the array on the stack will be passed to the function on every iteration.
1553 /// ex: \(+×) 10 [1 2 3 4]
1554 /// ex: ⬚@ \(⊂⊂) @, "abcd"
1555 (1[1], Scan, AggregatingModifier, ("scan", '\\')),
1556 /// Apply a function to each element of an array or arrays
1557 ///
1558 /// This is the element-wise version of [rows].
1559 /// **This is often not what you want.** Prefer using pervasive functions or [table] when possible.
1560 ///
1561 /// The number of arrays used depends on how many arguments the function takes.
1562 /// ex: ∵(⊟.) 1_2_3_4
1563 /// ex: ∵⊂ 1_2_3 4_5_6
1564 /// ex: ∵⊂ 1_2 [4_5 6_7]
1565 ///
1566 /// If the function is already pervasive, then [each] is redundant.
1567 /// ex! ∵+ 1_2_3 4_5_6
1568 /// ex: + 1_2_3 4_5_6
1569 ///
1570 /// Subscripted [each] operates on rank-N subarrays.
1571 /// ex: ∵₀□ °△2_3_4
1572 /// : ∵₁□ °△2_3_4
1573 /// : ∵₂□ °△2_3_4
1574 /// : ∵₃□ °△2_3_4
1575 ([1], Each, IteratingModifier, ("each", '∵')),
1576 /// Apply a function to each row of an array or arrays
1577 ///
1578 /// ex: /+ [1_2_3 4_5_6 7_8_9] # Sum each row with the next
1579 /// ex: ≡/+ [1_2_3 4_5_6 7_8_9] # Sum the elements of each row
1580 ///
1581 /// The number of arrays used depends on how many arguments the function takes.
1582 /// ex: ≡/+ [1_2 3_4] 5_6 # One argument
1583 /// ex: ≡⊂ [1_2 3_4] 5_6 # Two arguments
1584 ///
1585 /// In general, when [rows] uses multiple arrays, the arrays must have the same number of rows.
1586 /// ex! ≡⊂ 1_2_3 4_5
1587 /// However, if any of the arrays have exactly one row, then that row will be reused for each row of the other arrays.
1588 /// Scalars are considered to have one row.
1589 /// ex: ≡⊂ 1_2_3 4
1590 /// ex: ≡⊂ 1 2_3_4
1591 /// ex: ≡(⊂⊂) 1 2_3_4 5
1592 /// You can use [fix] to take advantage of this functionailty and re-use an entire array for each row of another.
1593 /// ex: ≡⊂ ¤ 1_2_3 4_5_6
1594 /// ex: ≡⊂ ⊙¤ 1_2_3 4_5_6
1595 /// [fold] also has this behavior.
1596 ///
1597 /// Numeric subscripted [rows] operates on rank-N subarrays.
1598 /// ex: ≡₀□ °△2_3_4
1599 /// : ≡₁□ °△2_3_4
1600 /// : ≡₂□ °△2_3_4
1601 /// : ≡₃□ °△2_3_4
1602 /// Making the subscript negative instead operates N ranks deep.
1603 /// ex: ≡₋₁□ °△2_3_4
1604 /// : ≡₋₂□ °△2_3_4
1605 /// : ≡₋₃□ °△2_3_4
1606 /// Sided [rows] [fix]es either the first or last argument so that it can be reused in multiple iterations.
1607 /// ex: ≡⌞⊂ 1_2 3_4
1608 /// : ≡⌟⊂ 1_2 3_4
1609 /// The side quantifier specifies how many arguments to [fix].
1610 /// ex: ≡⌞₂(⊂⊂) 1_2 3_4 5_6
1611 /// : ≡⌟₂(⊂⊂) 1_2 3_4 5_6
1612 /// [rows] accepts mixed numeric and sided subscripts.
1613 ([1], Rows, IteratingModifier, ("rows", '≡')),
1614 /// Apply a function to each unboxed row of an array and re-box the results
1615 ///
1616 /// For box arrays, this is equivalent to `rows``under``un``box`.
1617 /// ex: ≡⍜°□(⊂⊙@!) {"a" "bc" "def"}
1618 /// : ⍚(⊂⊙@!) {"a" "bc" "def"}
1619 /// For non-box arrays, [inventory] works identically to [rows], except it [box]es each result row.
1620 /// ex: ≡⇌ [1_2_3 4_5_6]
1621 /// : ⍚⇌ [1_2_3 4_5_6]
1622 /// This can be useful when you expect the function to yield arrays of different [shape]s.
1623 /// ex: ⍚⇡ [3 8 5 4]
1624 /// ex: ⍚↙⊙¤ [2 0 3 4 1] [4 8 9 2]
1625 /// For a box and non-box array, [inventory] will unbox the box array's rows and then re-box the results.
1626 /// ex: ⍚⊂ {"a" "bc" "def"} "123"
1627 ///
1628 /// A common use case is in conjunction with [under] and boxing array notation as a sort of n-wise [both].
1629 /// ex: {⍜ {⊙⊙∘}⍚⊂ 1_2 3_4_5 6_7_8_9 10}
1630 /// : {⍜⊙{⊙⊙∘}⍚⊂ 10 1_2 3_4_5 6_7_8_9 }
1631 ///
1632 /// Subscripted [inventory] operates N subarrays deep.
1633 /// ex: ⍚₀∘ °△2_3_4
1634 /// : ⍚₁∘ °△2_3_4
1635 /// : ⍚₂∘ °△2_3_4
1636 /// : ⍚₃∘ °△2_3_4
1637 /// Sided [inventory] [fix]es either the first or last argument so that it can be reused in multiple iterations.
1638 /// ex: ⍚⌞⊂ 1_2 3_4
1639 /// : ⍚⌟⊂ 1_2 3_4
1640 /// The side quantifier specifies how many arguments to [fix].
1641 /// ex: ⍚⌞₂(⊂⊂) 1_2 3_4 5_6
1642 /// : ⍚⌟₂(⊂⊂) 1_2 3_4 5_6
1643 ([1], Inventory, IteratingModifier, ("inventory", '⍚')),
1644 /// Apply a function to each combination of rows of some arrays
1645 ///
1646 /// ex: ⊞+ 1_2_3 4_5_6_7
1647 /// ex: ⊞⊂ 1_2 3_4
1648 ///
1649 /// The resulting array will always have a shape starting with the lengths of the two inputs.
1650 /// ex: △⊞+ 1_2 3_4_5
1651 /// ex: △⊞⊂ 1_2 3_4_5
1652 /// ex: △⊞+ [1_2_3 4_5_6] [7 8 9 10]
1653 /// ex: △⊞⊂ [1_2_3 4_5_6] [7 8 9 10]
1654 ///
1655 /// [table] also works with more than two arrays.
1656 /// ex: ⊞(⊂⊂) 1_2 3_4 5_6
1657 /// If you want to fix one of the arrays so that it is present in every call of the function, you can simply add a dimension to it, though you may need to collapse it later.
1658 /// Here, we add a dimension to the second array to [fix] it, then collapse with `reduce``join`.
1659 /// ex: /⊂ ⊞(⊂⊂) ⊙¤ 1_2 3_4 5_6
1660 ([1], Table, IteratingModifier, ("table", '⊞')),
1661 /// Get permutations or combinations of an array
1662 ///
1663 /// When given a dyadic function, [tuples] takes two arguments.
1664 /// The first argument must be a natural number. The second argument may be any array.
1665 /// If the function takes 2 arguments, combinations of rows from the array whose indices pass the function will be returned.
1666 /// The most common functions to use are `less than`, `less or equal`, `greater than`, `greater or equal`, and `not equals`.
1667 ///
1668 /// The examples here are [transpose]d to take up less vertical space.
1669 ///
1670 /// `less than` and `greater than` will give all unique *combinations* of rows from the array.
1671 /// ex: ⍉ ⧅< 2 ⇡5
1672 /// : ⍉ ⧅> 2 ⇡5
1673 /// ex: ⍉ ⧅< 3 ⇡5
1674 /// ex: ⍉ ⧅< 4 ⇡5
1675 /// `less or equal` and `greater or equal` will include values that are the same.
1676 /// ex: ⍉ ⧅≤ 2 ⇡5
1677 /// ex: ⍉ ⧅≥ 2 ⇡5
1678 /// `not equals` will give all *permutations* of rows from the array.
1679 /// ex: ⍉ ⧅≠ 2 ⇡5
1680 /// ex: ⍉ ⧅≠ 3 ⇡5
1681 /// ex: ⍉ ⧅≠ 4 ⇡5
1682 /// `gap``gap``1` will give *all* ways of arranging the rows.
1683 /// ex: ⍉ ⧅⋅⋅1 2 ⇡4
1684 /// If the size is `2`, the function is allowed to return non-booleans. Tuples will be copied as many times as the value.
1685 /// ex: ⍉ ⧅(+1<) 2 ⇡4
1686 /// If the second argument is a scalar, the number of tuples that would be returned for the [range] of that number is returned.
1687 /// ex: ⧅≠ 2 4
1688 /// : ⍉ ⧅≠ 2 ⇡4
1689 /// A negative size will subtract from the length of the array. This is useful if you want to, for example, get a versions of the array with each row removed.
1690 /// A size of [infinity] will use the [length] of the array directly. This is useful for permutations.
1691 /// ex: ⧅<¯1 ⇡4
1692 /// : ⧅≠ ∞ ⇡4
1693 ///
1694 /// If [tuples] is given a monadic function, it takes only one argument.
1695 /// The function will be called on all prefixes of the array.
1696 /// The full-length prefix will be included, but not the empty prefix, so the output will have the same number of rows as the original array.
1697 /// ex: ⧅□ ⇡5
1698 /// ex: ⧅□ "Hello!"
1699 /// ex: ⧅□ °△5_2
1700 /// You can get suffixes with a few [reverse]s.
1701 /// ex: ⍜⇌⧅(□⇌) "Hello!"
1702 /// Monadic [tuples] is compatible with [fill].
1703 /// ex: ⬚@-⧅∘ "Uiua"
1704 ///
1705 /// With [un][where], we can see where the inspiration for [tuples]'s glyph comes from.
1706 /// ex: °⊚ ⧅< 2 ⇡50
1707 /// : °⊚ ⧅> 2 ⇡50
1708 /// : °⊚ ⧅≠ 2 ⇡50
1709 /// We can get something similar with the monadic form.
1710 /// ex: ⬚0⧅∘ +1⇡50
1711 ///
1712 /// The tuple size may be given as a subscript.
1713 /// ex: ⍉ ⧅₂< ⇡5
1714 ([1], Tuples, IteratingModifier, ("tuples", '⧅')),
1715 /// Call a function on windows of an array
1716 ///
1717 /// The first argument is the window size.
1718 /// The second argument is the array to be windowed.
1719 /// Sliding windows of the given size are passed to the function.
1720 /// ex: ⧈∘ 2 ⇡4
1721 /// : ⧈∘ 3 ⇡6
1722 /// ex: ⧈□ 2 ⇡4
1723 /// : ⧈□ 3 ⇡6
1724 /// Multi-dimensional window sizes are supported.
1725 /// ex: ⧈□ 3_3 °△5_5
1726 ///
1727 /// [fill] will pad the sides of the windows
1728 /// ex: ⬚0⧈∘ 3 [1 2 3]
1729 /// ex: ⬚0⧈□ 2_3 +1°△3_3
1730 ///
1731 /// A subscript sets the window size.
1732 /// ex: ⧈₃∘ ⇡6
1733 ///
1734 /// [stencil] only takes a window size if its function is monadic. For functions with 2 or more arguments, the window size is the number of arguments.
1735 /// This is useful for things like getting adjacent differences.
1736 /// ex: ⧈- [3 1 5 6 8]
1737 /// ex: ⧈⊟ [3 1 5 6 8]
1738 /// ex: ⧈{⊙⊟} ⇡5
1739 ///
1740 /// Negative window sizes gives the absolute value number of windows.
1741 /// ex: ⧈□¯2 °△4_4
1742 /// ex: ⧈□¯3 °△4_4
1743 /// This can be useful when you want to get horizontal windows.
1744 /// ex: ⧈□¯1_2 °△4_4
1745 ///
1746 /// [stencil] with a scalar or list window size will always produce overlapping windows that shift by one row at a time.
1747 /// 2-dimensional window sizes allow more control over the windows.
1748 /// A rank-2 array with only one row will "chunk" the array with non-overlapping windows.
1749 /// ex: ⧈∘[[4]] ⇡12
1750 /// ex: ⧈∘ ¤¤4 ⇡12
1751 /// ex: ⧈□ ¤[2 2] . °△4_6
1752 /// Negative sizes still specify the number of windows desired.
1753 /// ex: ⧈∘ ¤¤¯4 ⇡12
1754 /// ex: ⧈□ ¤[¯2 ¯2] . °△4_6
1755 /// A rank-2 array with two rows allows the "stride" of the windows to be specified.
1756 /// The first row specifies the window size, and the second row specifies the stride.
1757 /// ex: ⧈□ [¤3¤4] ⇡12
1758 /// ex: ⧈□ [¤4¤2] ⇡12
1759 /// ex: ⧈□ [2_2 1_3] . °△4_6
1760 /// By default, [fill]ed [stencil] pads each side of an axis with a number equal to the axis's window size [subtract]`1`.
1761 /// This number is then [multiply]d by the specified stride.
1762 /// ex: ⬚0⧈□ 2_2 +1°△2_2
1763 /// ex: ⬚0⧈□ ¤2_2 +1°△4_6
1764 /// Adding a third row to the array allows the fill amount to be specified for each axis.
1765 /// ex: ⬚0⧈□ [2_2 1_1 0_1] +1°△2_2
1766 /// ex: ⬚0⧈□ [2_2 2_2 0_1] +1°△4_6
1767 (2[1], Stencil, IteratingModifier, ("stencil", '⧈')),
1768 /// Repeat a function a number of times
1769 ///
1770 /// ex: ⍥(+2)5 0
1771 /// ex: ⍥(⊂2)5 []
1772 /// If the net stack change of the function is negative, then lower stack values will be preserved between iterations.
1773 /// In this example, `10` is added to `3` `5` times.
1774 /// ex: ⍥+5 3 10
1775 /// In this example, `2` is [join]ed with `1` `5` times.
1776 /// ex: ⍥⊂5 1 2
1777 ///
1778 /// If the net stack change of the function is positive, then outputs of the function that are lower on the stack exceeding the number of arguments will be collected into arrays.
1779 /// ex: ⌊×10 ⍥⚂5
1780 /// [by] or [below] can be used to put collected values below others.
1781 /// ex: ⍥⊸√4 6561
1782 /// ex: ⍥◡+10 1 1
1783 /// Note that depending on how the stack in the function is managed, the collected arrays may contain different results.
1784 /// ex: ⍥(⊸+1)4 10 # Omit final value
1785 /// : ⍥(.+1)4 10 # Omit initial value
1786 /// This is because length of the accumulated arrays will always be the same as the number of repetitions, so they cannot contain both the initial and final values.
1787 ///
1788 /// Repeating [infinity] times will do a fixed-point iteration.
1789 /// The loop will end when the top value of the function's output is equal to the top value of the function's input.
1790 /// For example, this could be used to flatten a deeply nested array.
1791 /// ex: ⍥/◇⊂∞ {1 {2 3} {4 {5 6 {7}}}}
1792 /// [un][repeat] will do something similar, except the number of repetitions required to converge will be returned as well. It may be necessary to [un] the inner function as well.
1793 /// ex: °⍥°/◇⊂ {1 {2 3} {4 {5 6 {7}}}}
1794 /// The number of repetitions may be non-scalar. In this case, the function will be repeated each row of the input a different number of times.
1795 /// ex: ⍥(×2) [1 2 3 4] [5 5 5 5]
1796 /// If you want to conditionally either run some function or not, you can use [repeat] to repeat `0` or `1` times.
1797 /// ex: F ← ⍥(×10)<10.
1798 /// : F 5
1799 /// : F 12
1800 /// [repeat]ing a negative number of times will repeat the function's [un]-inverse.
1801 /// ex: ⍥(×2)¯5 1024
1802 ///
1803 /// The repetition count may be given as a subscript.
1804 /// ex: ⍥₅(×2) 32
1805 /// : ⍥₋₅(×2) 1024
1806 ///
1807 /// [repeat]'s glyph is a combination of a circle, representing a loop, and the 𝄇 symbol from musical notation.
1808 ([1], Repeat, IteratingModifier, ("repeat", '⍥')),
1809 /// Group elements of an array into buckets by index
1810 ///
1811 /// [group] is similar to `group_by` functions in other languages.
1812 /// Takes a function and two arrays.
1813 /// The first array must contain integers and have a shape that is a prefix of the shape of the second array.
1814 /// Rows in the second array will be grouped into buckets by the indices in the first array.
1815 /// Keys `less than``0` will be omitted.
1816 /// The function then processes each group in order. The result depends on what the function is.
1817 /// If the function takes 0 or 1 arguments, then [group] behaves like [rows].
1818 /// ex: ⊕∘ [0 2 2 1 0 1] [1 2 3 4 5 6]
1819 /// If the values returned by the function do not have the same [shape], concatenation will fail unless a [fill] is supplied.
1820 /// ex! ⊕∘ [0 1 0 2 1 1] [1 2 3 4 5 6]
1821 /// ex: ⬚∞⊕∘ [0 1 0 2 1 1] [1 2 3 4 5 6]
1822 /// It is common to use [box] to encapsulate groups of different [shape]s.
1823 /// ex: ⊕□ [0 1 0 2 1 1] [1 2 3 4 5 6]
1824 ///
1825 /// When combined with [classify], you can do things like counting the number of occurrences of each character in a string.
1826 /// ex: $ Count the characters in this string
1827 /// : ⊟∩≡□ ⊕⊃⊢⧻ ⊛.
1828 ///
1829 /// If the function takes more than 1 argument, groups are extracted for each argument.
1830 /// This example combines each character with the index of its first appearance in the string as well as the number of times it appears.
1831 /// ex: ⊕{⊃∩⊢⧻} ⊛ ⟜°⊏ "mississippi"
1832 /// Note that multiple values can be returned in this way instead of combining them inside the function.
1833 /// ex: ⊕⊃∩⊢⧻ ⊛ ⟜°⊏ "mississippi"
1834 ///
1835 /// The indices may be multidimensional.
1836 /// ex: ⊕□ [0_2 2_1] ["ab" "cd"]
1837 ///
1838 /// [un][group] works if [group]'s function is monadic and [un]-invertible.
1839 /// A list of indices and a list of ungrouped values will be returned.
1840 /// The most common function to use with this is [box].
1841 /// ex: °⊕□ {1 2_3_4 5_6}
1842 ///
1843 /// [under][group] works if [group]'s function is [under]able.
1844 /// ex: ⍜⊕□≡⇌ ≠@ . $ These are some words
1845 /// The length of each group must not change.
1846 /// ex! ⍜⊕□⇌ ≠@ . $ These are some words
1847 ///
1848 /// [group] is closely related to [partition].
1849 (2[1], Group, AggregatingModifier, ("group", '⊕')),
1850 /// Group sequential sections of an array
1851 ///
1852 /// The most common use of [partition] is to split an array by a delimiter.
1853 ///
1854 /// Takes a function and two arrays.
1855 /// The arrays must be the same [length].
1856 /// The first array is called the "markers". It must be rank `1` and contain integers.
1857 /// Consecutive rows in the second array that line up with groups of the same key in the markers will be grouped together.
1858 /// Keys `less or equal``0` will be omitted.
1859 /// The function then processes each group in order. The result depends on what the function is.
1860 /// If the function takes 0 or 1 arguments, then [partition] behaves like [rows].
1861 /// ex: ⊜∘ [0 0 2 2 1 1 3 3] [1 2 3 4 5 6 7 8]
1862 /// If the values returned by the function do not have the same [shape], concatenation will fail unless a [fill] is supplied.
1863 /// ex! ⊜∘ [0 2 3 3 3 0 1 1] [1 2 3 4 5 6 7 8]
1864 /// ex: ⬚∞⊜∘ [0 2 3 3 3 0 1 1] [1 2 3 4 5 6 7 8]
1865 /// It is common to use [box] to encapsulate groups of different [shape]s.
1866 /// ex: ⊜□ [0 2 3 3 3 0 1 1] [1 2 3 4 5 6 7 8]
1867 ///
1868 /// This can be used to split an array by a delimiter.
1869 /// ex: ⊜□ ≠@ . $ Hey there friendo
1870 /// You can nest [partition]s to split by multiple delimiters and create a multi-dimensional array.
1871 /// ex: $ 1 1 2 3
1872 /// : $ 5 8 13 21
1873 /// : ⊜(⊜⋕≠@ .)≠@\n.
1874 ///
1875 /// If the function takes more than 1 argument, groups are extracted for each argument.
1876 /// This example couples each word from a string with its start index.
1877 /// ex: ⊜{⊢⊙∘} ≠@ ⟜°⊏ "Hello, how are you?"
1878 /// Note that multiple values can be returned in this way instead of combining them inside the function.
1879 /// ex: ⊜⊓⊢□ ≠@ ⟜°⊏ "Hello, how are you?"
1880 ///
1881 /// [partition] also works with multidimensional markers. Groups are formed from markers that are adjacent along any axis.
1882 /// Each group will be flattened before being passed to the function.
1883 /// ex: ⊜□.. ↯4_4 [0 1 1 2 2]
1884 /// If we wanted to group the indices that are adjacent, we could use the array to [partition] its own indices.
1885 /// ex: ⊜□⟜°⊡ ↯4_4 [0 1 1 2 2]
1886 ///
1887 /// [un][partition] works if [partition]'s function is monadic and [un]-invertible.
1888 /// A list of markers and a list of unpartitioned values will be returned.
1889 /// The most common function to use with this is [box].
1890 /// By default, the markers will be increasing integers starting from `1`.
1891 /// ex: °⊜□ {"Hey" "there" "buddy"}
1892 /// If a [fill] value is provided, the markers will all be `1`, and the gaps will be filled with the fill value.
1893 /// ex: ⬚@-°⊜□ {"Hey" "there" "buddy"}
1894 ///
1895 /// [under][partition] works if [partition]'s function is [under]able.
1896 /// ex: ⍜⊜□⇌ ≠@ . $ These are some words
1897 /// ex: ⍜⊜□≡⇌ ≠@ . $ These are some words
1898 /// ex: ⍜⊜⊢⌵ ≠@ . $ These are some words
1899 ///
1900 /// [partition] is closely related to [group].
1901 (2[1], Partition, AggregatingModifier, ("partition", '⊜')),
1902 /// Call a function with its arguments' axes reversed
1903 ///
1904 /// Uiua primitives tend to treat axes near the front of the shape as spanning items in a collection. Axes near the end of the shape are often treated as the items or components of the items.
1905 /// Consider a matrix of shape `N×2`. We can think of this as a list of `N` 2D vectors.
1906 /// ex: [1_2 2_0 3_4]
1907 /// Because the "list" part of the shape is the first axis, we can easily append or remove items from the list.
1908 /// ex: ⊂ 0_3 [1_2 2_0 3_4]
1909 /// However, if we wanted to shift all of the vectors by the same amount, naive [add]ing doesn't work.
1910 /// ex! + 2_4 [1_2 2_0 3_4]
1911 /// This is because [add] expects one of the shapes to be a prefix of the other, and `[2]` is not a prefix of `[3 2]`.
1912 /// One option is to use [fix]. This adds a length-1 axis to the first argument, and [add] knows to extend it.
1913 /// ex: + ¤2_4 [1_2 2_0 3_4]
1914 /// But what if our list of vectors is actually a table? [fix] works, but look closely. The result is actually not what we want!
1915 /// ex: + ¤2_4 [[1_2 2_0] [3_4 0_0] [10_0 5_1]]
1916 /// To make it work again, we need to [fix] a second time.
1917 /// ex: + ¤¤2_4 [[1_2 2_0] [3_4 0_0] [10_0 5_1]]
1918 /// But if we actually had a list of matrices, adding a matrix to each list item correctly would require going back to a single [fix].
1919 /// ex: + ¤[0_1 1_0] [[1_2 2_0] [3_4 0_0] [10_0 5_1]]
1920 /// The problem here is that the number of times we need to [fix] is highly dependent on the rank and interpretation of the arguments.
1921 /// This is because because dyadic pervasive functions in Uiua operate on the leading axes of their arguments rather than the trailing ones.
1922 /// [evert] reverses a function's arguments' axes so that the leading axes are the trailing ones. It reverses them back when the function is done.
1923 /// With this, we can use a single function for all of our shift operations!
1924 /// ex: # Experimental!
1925 /// : ⧋+ 2_4 [1_2 2_0 3_4]
1926 /// : ⧋+ 2_4 [[1_2 2_0] [3_4 0_0] [10_0 5_1]]
1927 /// : ⧋+ [0_1 1_0] [[1_2 2_0] [3_4 0_0] [10_0 5_1]]
1928 /// And it's not just pervasives. Suppose we wanted to elevate our table of 2D vectors to 3D. We could [evert][join].
1929 /// ex: # Experimental!
1930 /// : ⧋⊂⊙0 [[1_2 2_0] [3_4 0_0] [10_0 5_1]]
1931 /// The classic [divide][on][range] idiom generates `N` numbers between `0` and `1`.
1932 /// ex: ÷⟜⇡4
1933 /// But it doesn't work for multidimensional ranges.
1934 /// ex! ÷⟜⇡4_4
1935 /// [evert] makes it work with any rank!
1936 /// ex: # Experimental!
1937 /// : ⧋÷⟜⇡4
1938 /// : ⧋÷⟜⇡4_4
1939 /// : ⧋÷⟜⇡2_4_4
1940 ///
1941 /// While [evert] can technically be achieved with [under] and [orient], the spelling can be a bit long and is different for different numbers of arguments.
1942 /// ex: # Experimental!
1943 /// : ⍜∩⍜°⤸⇌+ 2_4 [1_2 2_0 3_4]
1944 /// : ⧋+ 2_4 [1_2 2_0 3_4]
1945 ///
1946 /// The word "evert" means to turn something inside out.
1947 ([1], Evert, OtherModifier, ("evert", '⧋'), { experimental: true }),
1948 /// Unbox the arguments to a function before calling it
1949 ///
1950 /// ex: ⊂ □[1 2 3] □[4 5 6]
1951 /// : ◇⊂ □[1 2 3] □[4 5 6]
1952 /// A common use of [content] is to collapse a list of [box]ed arrays with [reduce].
1953 /// ex: /◇⊂ {1_2_3 4_5 6}
1954 /// This case will still unbox a single element.
1955 /// ex: /◇⊂ {"Hi"}
1956 /// [content] is also good for deriving data from an array of boxes
1957 /// ex: ≡◇⧻ {"These" "are" "some" "words"}
1958 /// ex: ≡◇/+ {3_0_1 5 2_7}
1959 ([1], Content, OtherModifier, ("content", '◇')),
1960 /// Discard the top stack value then call a function
1961 ///
1962 /// See the [More Stack Manipulation Tutorial](/tutorial/morestack) for a more complete understanding of why [gap] is useful.
1963 ///
1964 /// ex: ⋅+ 1 2 3
1965 /// This may seem useless when [pop] exists, but [gap] really shines when used with [fork].
1966 /// In a [fork] expression, you can use [dip], [gap], and [identity] to select out values.
1967 /// For example, if you wanted to add 3 values but keep the last value on top of the stack:
1968 /// ex: [⊃⋅⋅∘(++) 3 5 10]
1969 /// By using fewer `gap`s, you can select a different value.
1970 /// ex: [⊃⋅∘(++) 3 5 10]
1971 /// ex! [⊃∘(++) 3 5 10]
1972 /// By replacing a `gap` with a `dip`, you keep the argument in that spot instead of popping it:
1973 /// ex: [⊃⊙⋅∘(++) 3 5 10]
1974 /// ex: [⊃⋅⊙∘(++) 3 5 10]
1975 /// ex: [⊃⊙⊙∘(++) 3 5 10]
1976 ([1], Gap, Planet, ("gap", '⋅')),
1977 /// Temporarily pop the top value off the stack and call a function
1978 ///
1979 /// See the [More Stack Manipulation Tutorial](/tutorial/morestack) for a more complete understanding of why [dip] is useful.
1980 ///
1981 /// ex: [⊙+ 1 2 3]
1982 /// ex: [⊙⊙+ 1 2 3 4]
1983 /// This is especially useful when used in a [fork].
1984 /// In a [fork] expression, you can use [dip], [gap], and [identity] to select out values.
1985 /// For example, if you wanted to add 3 values but keep all 3 on top of the stack:
1986 /// ex: [⊃⊙⊙∘(++) 3 5 10]
1987 /// By replacing a `dip` with a `gap`, you pop the argument in that spot instead of keeping it:
1988 /// ex: [⊃⊙⊙∘(++) 3 5 10]
1989 /// ex: [⊃⊙⋅∘(++) 3 5 10]
1990 /// ex: [⊃⋅⊙∘(++) 3 5 10]
1991 /// ex: [⊃⊙∘(++) 3 5 10]
1992 ///
1993 /// [dip] can be used with a function pack.
1994 /// `dip``(F|G|H|..)` is equivalent to `F``dip``(G``dip``(H``dip``(..)))`.
1995 /// ex: ⊙(+|×) 1 2 3
1996 /// ex: ⊙(⊂×10|□₂|⊟) 1 2 3 4
1997 ([1], Dip, Planet, ("dip", '⊙')),
1998 /// Call a function on the first and third values on the stack
1999 ///
2000 /// ex: # Experimental!
2001 /// : 𝄐+ 1 2 3
2002 /// This can simplify some common stack access patterns.
2003 /// ex: # Experimental!
2004 /// : [⊃⊟𝄐⊟ @a@b@c]
2005 /// ex: # Experimental!
2006 /// : [⊃⋅⊟𝄐⊟ @a@b@c]
2007 /// [reach] supports sided subscripts. They put the 2nd stack value above or below the outputs of the function.
2008 /// ex: # Experimental!
2009 /// : {𝄐⌞⊟ 1 2 3}
2010 /// ex: # Experimental!
2011 /// : {𝄐⌟⊟ 1 2 3}
2012 ([1], Reach, Planet, ("reach", '𝄐'), { experimental: true }),
2013 /// Call a function but keep its first argument on the top of the stack
2014 ///
2015 /// ex: [⟜+ 2 5]
2016 /// : [⟜- 2 5]
2017 /// ex: ÷⟜⇡ 10
2018 /// ex: +⟜(⇡-) 4 10
2019 /// ex: +⟜(×-) 10 20 0.3
2020 /// ex: ↯⟜⊚ 4
2021 ///
2022 /// [on] can be thought of as a compliment of [duplicate].
2023 /// ex: [¯. 1]
2024 /// : [⟜¯ 1]
2025 ///
2026 /// [on] in planet notation acts as a way of [duplicate]ing a value.
2027 /// You can read `on``dip` or `on``identity` as a single unit that keeps 2 copies of the value at that position.
2028 /// ex: [⟜⊙⋅⟜⊙◌ 1 2 3 4] # Easy to read with ⟜
2029 /// : [.⊙⋅(.⊙◌) 1 2 3 4] # Hard to read with .
2030 /// : [∩⊓.◌ 1 2 3 4] # Shorter, maybe hard to read
2031 /// ex: [⊙⟜⊙⋅⟜∘ 1 2 3 4] # Easy to read with ⟜
2032 /// : [⊙(.⊙⋅.) 1 2 3 4] # Hard to read with .
2033 /// : [⊙.⊙⊙⋅. 1 2 3 4] # Hard to read with .
2034 /// [on] can be used with a function pack. `on``(F|G)` becomes `on``F``on``G`.
2035 /// ex: [⟜(+1|×2|¯)] 5
2036 /// Subscripted [on] keeps the first N arguments on top of the stack.
2037 /// ex: {⟜₂[⊙⊙∘] 1 2 3}
2038 /// [on] is equivalent to [fork][identity], but can often be easier to read.
2039 ([1], On, Stack, ("on", '⟜')),
2040 /// Duplicate a function's last argument before calling it
2041 ///
2042 /// If you want to filter out every element of an array that is not [less than] 10, you can use [keep].
2043 /// ex: ▽<10. [1 27 8 3 14 9]
2044 /// However, if you want to make this a function, you have to [dip] below the first argument to [duplicate] the array.
2045 /// ex: F ← ▽<⊙.
2046 /// : F 10 [1 27 8 3 14 9]
2047 /// While this works, it may take a moment to process in your mind how the stack is changing.
2048 /// [by] expresses the common pattern of performing an operation but preserving the last argument so that it can be used again.
2049 /// With [by], the filtering function above can be written more simply.
2050 /// ex: F ← ▽⊸<
2051 /// : F 10 [1 27 8 3 14 9]
2052 /// Here are some more examples of [by] in action.
2053 /// ex: ⊂⊸↙ 2 [1 2 3 4 5]
2054 /// : ⊜□⊸≠ @ "Hey there buddy"
2055 /// : ⊕□⊸◿ 5 [2 9 5 21 10 17 3 35]
2056 /// Subscripted [by] keeps the last N arguments below the outputs on the stack.
2057 /// ex: {⊸₂[⊙⊙∘] 1 2 3}
2058 ([1], By, Stack, ("by", '⊸')),
2059 /// Call a function but keep its last argument on the top of the stack
2060 ///
2061 /// ex: [⤙+ 2 5]
2062 /// : [⤙- 2 5]
2063 /// [with] makes it easy to call multiple dyadic functions with the same last argument.
2064 /// There are many cases where this can read quite nicely.
2065 /// "Couple +1 with ×2"
2066 /// ex: ⊟+1⤙×2 5
2067 /// There is the common testing pattern "assert with match".
2068 /// ex: ⍤⤙≍ 5 +2 3
2069 /// ex! ⍤⤙≍ 5 +2 2
2070 /// [with] can be used to copy a value from deep in the stack, or to move it.
2071 /// ex: [⤙⊙⊙⊙∘ 1 2 3 4]
2072 /// : [⤙⊙⊙⊙◌ 1 2 3 4]
2073 /// If you do not want these behaviors, use [on] instead.
2074 /// Subscripted [with] keeps the last N arguments above the outputs on the stack.
2075 /// ex: {⤙₂[⊙⊙∘] 1 2 3}
2076 ([1], With, Stack, ("with", '⤙')),
2077 /// Call a function but keep its first argument under the outputs on the stack
2078 ///
2079 /// ex: [⤚+ 2 5]
2080 /// : [⤚- 2 5]
2081 /// [off] makes it easy to call multiple dyadic functions with the same first argument.
2082 /// This example keeps only 2D vectors in the first argument with `1`s in that position in the second argument.
2083 /// ex: ▽⤚⊡ [0_2 1_0 1_1] [0_1_1 1_0_1]
2084 /// Or you could quickly [join] a row to either side of an array.
2085 /// ex: ⊂⤚⊂ 0 [1 2 3 4]
2086 /// If [off]'s function is commutative, then it can be used in a place where [by] would work if the arguments were reversed.
2087 /// ex: ▽⤚≠ [1 2 3 4 5] 2
2088 /// : ▽⊸≠ 2 [1 2 3 4 5]
2089 /// [off] can be used to copy a value from the top of the stack to a position deeper, or to move it.
2090 /// ex: [⤚⊙⊙⊙∘ 1 2 3 4]
2091 /// : [⤚⋅⊙⊙∘ 1 2 3 4]
2092 /// If you do not want these behaviors, use [by] instead.
2093 /// Subscripted [off] keeps the first N arguments below the outputs on the stack.
2094 /// ex: {⤚₂[⊙⊙∘] 1 2 3}
2095 ([1], Off, Stack, ("off", '⤚')),
2096 /// Keep all arguments to a function above the outputs on the stack
2097 ///
2098 /// ex: # Experimental!
2099 /// : [◠+ 1 2]
2100 /// ex: # Experimental!
2101 /// : [◠(++) 1 2 3]
2102 ///
2103 /// See also: [below]
2104 ([1], Above, Stack, ("above", '◠'), { experimental: true }),
2105 /// Keep all arguments to a function below the outputs on the stack
2106 ///
2107 /// ex: [◡+ 1 2]
2108 /// ex: [◡(++) 1 2 3]
2109 /// This can be used with [gap] and [identity] to copy values from arbitrarily low in the stack.
2110 /// ex: [◡⋅⋅⋅⋅∘ 1 2 3 4 5]
2111 ///
2112 /// See also: [above]
2113 ([1], Below, Stack, ("below", '◡')),
2114 /// Call a function with the same array as all arguments
2115 ///
2116 /// ex: ˙+ 5
2117 /// ex: ˙⊞+ 1_2_3
2118 /// ex: ˙(⊂⊂) π
2119 ([1], Slf, Stack, ("self", '˙')),
2120 /// Call a function with its arguments swapped
2121 ///
2122 /// ex: - 2 5
2123 /// : ˜- 2 5
2124 /// ex: ˜⊂ 1 [2 3]
2125 /// ex: °˜⊂ [1 2 3]
2126 /// If the function takes 4 arguments, the second two arguments are swapped.
2127 /// ex: ˜⊟₄ 1 2 3 4
2128 /// ex: [˜∩⊟] 1 2 3 4
2129 /// [backward] is currently only allowed with dyadic and tetradic functions.
2130 ([1], Backward, Stack, ("backward", '˜')),
2131 /// Call a function on two sets of values
2132 ///
2133 /// For monadic functions, [both] calls its function on each of the top 2 values on the stack.
2134 /// ex: ∩⇡ 3 5
2135 ///
2136 /// For a function that takes `n` arguments, [both] calls the function on the 2 sets of `n` values on top of the stack.
2137 /// ex: [∩+ 1 2 3 4]
2138 /// ex: [∩(++) 1 2 3 4 5 6]
2139 ///
2140 /// [both] can also be chained. Every additional [both] doubles the number of arguments taken from the stack.
2141 /// ex: [∩∩(□+2) 1 @a 2_3 5]
2142 /// ex: [∩∩∩± 1 ¯2 0 42 ¯5 6 7 8 99]
2143 ///
2144 /// Subscripted [both] calls its function on N sets of arguments.
2145 /// ex: [∩₃+ 1 2 3 4 5 6]
2146 /// ex: [∩₃⊟ 1 2 3 4 5 6]
2147 ///
2148 /// There are two common patterns that involve a dyadic function and three values.
2149 /// If we call the function `f` and the values `a`, `b`, and `c`, then the patterns are:
2150 /// - `fac fbc`
2151 /// - `fab fac`
2152 /// These patterns can be achieved with [both] with sided subscripts.
2153 /// For example, if you wanted to check that a number is divisible by two other numbers:
2154 /// ex: F ← ∩⌟(=0◿)
2155 /// : F 3 5 ⇡16
2156 /// ex: G ← ∩⌞(=0˜◿)
2157 /// : G ⇡16 3 5
2158 ///
2159 /// [both] accepts mixed numeric and sided subscripts. The side quantifier determines how many arguments are reused on each call.
2160 /// ex: ∩₃⌞⊟ 1 2 3 4
2161 /// : ∩₃⌞₂⊟₃ 1 2 3 4 5
2162 ([1], Both, Planet, ("both", '∩')),
2163 /// Define the various inverses of a function
2164 ///
2165 /// [obverse] defines how a function should interact with [un], [anti], and [under].
2166 /// It can either take a single function, or a function pack with up to 5 functions.
2167 ///
2168 /// If only a single function is provided, its inverse will be nothing.
2169 /// This is useful when a function has to do some setup before the main [under]able part.
2170 /// Consider this function which [keep]s only odd numbers. While [keep] is compatible with [under], `by``modulo``2` is not.
2171 /// ex! F ← ▽⊸◿2
2172 /// : F [1 2 3 4 5]
2173 /// : ⍜F(×10) [1 2 3 4 5]
2174 /// Adding [obverse] makes it work.
2175 /// ex: F ← ▽⌅⊸◿2
2176 /// : F [1 2 3 4 5]
2177 /// : ⍜F(×10) [1 2 3 4 5]
2178 /// If given 2 functions, which inverse is set depends on the functions' signatures.
2179 /// If the functions have opposite signatures, then an [un]-compatible inverse is set.
2180 /// ex: F ← ⌅(+|⊃⌊⌈÷2)
2181 /// : F 1 2
2182 /// : [°F 25]
2183 /// If the functions have signatures `|a.b` and `|(b+1).(a-1)`, then an [anti]-compatible inverse is set.
2184 /// The most commonly used signatures for which this holds is when both signatures are `|2.1`.
2185 /// ex: F ← ⌅(+⊙(×10)|÷10-)
2186 /// : F 2 3
2187 /// : ⌝F 2 32
2188 /// This sort of inverse also works with [under].
2189 /// ex: F ← ⌅(+⊙(×10)|÷10-)
2190 /// : ⍜F? 2 5
2191 /// Otherwise, an [under]-compatible inverse is set.
2192 /// ex: F ← ⌅(+|¯)
2193 /// : ⍜F? 1 2
2194 /// If given 3 functions, an [under]-compatible inverse always set.
2195 /// The first function is the normal case.
2196 /// The second function is the "do" part of the [under].
2197 /// The third function is the "undo" part of the [under].
2198 /// ex: F ← ⌅(⊂10|⊂⊙1|⊂⊙2)
2199 /// : F 3
2200 /// : ⍜F⇌ 0_0
2201 /// If the second function returns more values than the first function, the excess values will be saved as "context". These context values will be passed to the "undo" part of the [under].
2202 /// Here is a manual implementation of [add]'s [under] behavior.
2203 /// ex: F ← ⌅(+|⟜+|-)
2204 /// : F 2 5
2205 /// : ⍜F(×10) 2 5
2206 /// If given 4 functions, both [un]-compatible and [under]-compatible inverses are set.
2207 /// The first function is the normal case.
2208 /// The second function is the [un]-compatible inverse.
2209 /// The third and fourth functions are for the [under]-compatible inverse.
2210 /// If the fourth function has the same signature as the first, it will also be used as the [anti]-compatible inverse.
2211 /// Finally, a fifth function can be given to specify the [anti]-compatible inverse.
2212 /// Here is our fully-specified [add] implementation.
2213 /// ex: F ← ⌅(+|⊃⌊⌈÷2|⟜+|-|$Anti -)
2214 /// : F 2 5
2215 /// : ⌝F 2 5
2216 /// : [°F] 15
2217 /// : ⍜F(÷3) 10 5
2218 /// Note that [anti] inverses also work with [un][on].
2219 /// ex: F ← ⌅(×|+÷2)
2220 /// : F 4 10
2221 /// : ⌝F 4 10
2222 /// : [°⟜F] 4 10
2223 ([1], Obverse, InversionModifier, ("obverse", '⌅')),
2224 /// Invert the behavior of a function
2225 ///
2226 /// A list of all [un]-compatible functions can be found [below](#uns).
2227 ///
2228 /// ex: °√ 5
2229 /// Two functions that are invertible alone can be inverted together
2230 /// ex: °(+1√) 5
2231 /// Most functions are not invertible.
2232 /// [under] also uses inverses, but expresses a different pattern and is generally more powerful.
2233 /// A function's [un]-inverse can be set with [obverse].
2234 /// For more about inverses, see the [Inverse Tutorial](/tutorial/inverses).
2235 ([1], Un, InversionModifier, ("un", '°')),
2236 /// Invert the behavior of a function, treating its first argument as a constant
2237 ///
2238 /// [un] has a guarantee that the inverted function will have a signature that is the inverse of original function's signature. For dyadic functions, if we want the inverse to *also* be dyadic, then we have to do some workarounds. We can either include the first argument in the inverted function, or we can use [on].
2239 /// For example, here are two ways to invert [rotate].
2240 /// ex: °(↻1) [1 2 3]
2241 /// : ◌°⟜↻ 1 [1 2 3]
2242 /// The first way requires the first argument to be a constant, which is not always applicable. The second way works but it is a bit verbose.
2243 /// [anti] does the [pop][un][on] for you.
2244 /// ex: ⌝↻ 1 [1 2 3]
2245 /// This simplifies some interesting inverses.
2246 /// ex: ⌝+ 1 5
2247 /// ex: ⌝↘ 3 [1 2 3]
2248 /// ex: ⬚@-⌝⊏ [0 2 5] "abc"
2249 /// ex: ⬚@-⌝⊡ [1_2 3_4] "xy"
2250 /// ex: ⌝⍥(+1) 3 10
2251 /// ex: ⌝⊂ 1 [1 2 3]
2252 /// ex! ⌝⊂ 1 [2 3 4]
2253 /// A function's [anti]-inverse can be set with [obverse].
2254 /// For more about inverses, see the [Inverse Tutorial](/tutorial/inverses).
2255 ([1], Anti, InversionModifier, ("anti", '⌝')),
2256 /// Operate on a transformed array, then reverse the transformation
2257 ///
2258 /// This is a more powerful version of [un].
2259 /// Conceptually, [under] transforms a value, modifies it, then reverses the transformation.
2260 ///
2261 /// A list of all [under]-compatible functions can be found [below](#unders).
2262 ///
2263 /// [under] takes 2 functions `f` and `g` and some other arguments `xs`.
2264 /// It applies `f` to `xs`, then applies `g` to the result.
2265 /// It then applies the inverse of `f` to the result of `g`.
2266 ///
2267 /// Any function that can be [un]ed can be used with [under].
2268 /// Some functions that can't be [un]ed can still be used with [under].
2269 ///
2270 /// Here, we [negate] 5, [subtract] 2, then [negate] again.
2271 /// ex: ⍜¯(-2) 5
2272 /// You can use [under][multiply][round] to round to a specific number of decimal places.
2273 /// ex: ⍜×⁅ 1e3 π
2274 ///
2275 /// In general, if two functions are compatible with [under] separately, then they are compatible together.
2276 /// ex: ⍜(↙⊙↘|×10) 2 1 [1 2 3 4 5]
2277 ///
2278 /// [under][both] works, and whether [both] is applied when undoing depends on the signature of `g`.
2279 /// For example, this hypotenuse function does not use [both] when undoing because its `g` (`add`) returns a single value.
2280 /// ex: ⍜∩˙×+ 3 4
2281 /// However, this function whose `g` returns *2* values *does* use [both] when undoing, in this case re-[box]ing the outputs.
2282 /// ex: ⍜∩°□(⊂◡⋅⊢) □[1 2 3] □[4 5 6 7 8]
2283 ///
2284 /// [obverse] can be used to define a function's [under] behavior.
2285 ///
2286 /// For more about [under] and inverses, see the [Inverse Tutorial](/tutorial/inverses).
2287 ([2], Under, InversionModifier, ("under", '⍜')),
2288 /// Call two functions on the same values
2289 ///
2290 /// [fork] is one of the most important functions for working with the stack.
2291 /// See the [More Stack Manipulation Tutorial](/tutorial/morestack) for a more complete understanding as to why.
2292 ///
2293 /// ex: ⊃⇌◴ 1_2_2_3
2294 /// ex: ⊃(+1)(×2) 5
2295 /// [fork] can be chained to apply more functions to the arguments. `n` functions require the chaining of `subtract``1n` [fork].
2296 /// ex: [⊃⊃⊃+-×÷ 5 8]
2297 /// If the functions take different numbers of arguments, then the number of arguments is the maximum. Functions that take fewer than the maximum will work on the top values.
2298 /// ex: [⊃+¯ 3 5]
2299 /// By default, [fork] can only work with two functions. However, a function pack can be used to pass the same arguments to many functions.
2300 /// ex: ⊃(+1|×3|÷|$"_ and _") 6 12
2301 ([2], Fork, Planet, ("fork", '⊃')),
2302 /// Call two functions on two distinct sets of values
2303 ///
2304 /// ex: ⊓⇌◴ 1_2_3 [1 4 2 4 2]
2305 /// Each function will always be called on its own set of values.
2306 /// ex: ⊓+× 1 2 3 4
2307 /// The functions' signatures need not be the same.
2308 /// ex: ⊓+(++) 1 2 3 4 5
2309 /// [bracket] can be chained to apply additional functions to arguments deeper on the stack.
2310 /// ex: ⊓⊓⇌(↻1)△ 1_2_3 4_5_6 7_8_9
2311 /// ex: [⊓⊓⊓+-×÷ 10 20 5 8 3 7 2 5]
2312 /// ex: [⊓(+|-|×|÷) 10 20 5 8 3 7 2 5]
2313 /// [bracket] with sided subscripts reuses a value in both functions.
2314 /// One use of this is to check if a number is within a range.
2315 /// ex: ◡×⊓⌟≥≤5 8 . [6 2 5 9 6 5 0 4]
2316 ([2], Bracket, Planet, ("bracket", '⊓')),
2317 /// Repeat a function while a condition holds
2318 ///
2319 /// The first function is the loop function, and it is run as long as the condition is true.
2320 /// The second function is the condition. Its top return value must be a boolean.
2321 /// ex: ⍢(×2|<1000) 1
2322 /// Return values from the condition function that are under the condition itself will be passed to the loop function.
2323 /// Here is an example that evaluates a [Collatz sequence](https://en.wikipedia.org/wiki/Collatz_conjecture).
2324 /// The next number in the sequence is calculated in the condition function but [join]ed to the sequence in the loop function.
2325 /// ex: C ← ⨬(+1×3|÷2)=0⊸◿2
2326 /// : ◌⍢⊂⊸(¬⊸∊⟜(C⊢)) [7]
2327 /// If the condition function consumes its only arguments to evaluate the condition, then those arguments will be implicitly copied.
2328 /// Consider this equivalence:
2329 /// ex: ⍢(×3|<100) 1
2330 /// : ⍢(×3|<100.) 1
2331 /// The net stack change of the two functions, minus the condition, is called the *composed signature*.
2332 /// A composed signature with a positive net stack change will collect the outputs into an array.
2333 /// ex: ⍢(⊸×2|≤1000) 10
2334 /// ex: ⍢(.×2|≤1000) 10
2335 /// A composed signature with a negative net stack change will reuse values lower on the stack.
2336 /// ex: ⍢(×|<100) 1 2
2337 /// ex: ⍢(⊂⤚(×⊢)|<100⊢) 1 2
2338 ([2], Do, IteratingModifier, ("do", '⍢')),
2339 /// Set the fill value for a function
2340 ///
2341 /// By default, some operations require that arrays' [shape]s are in some way compatible.
2342 /// [fill] allows you to specify a value that will be used to extend the shape of one or both of the operands to make an operation succeed.
2343 /// The function is modified to take a fill value which will be used to fill in shapes.
2344 ///
2345 /// A list of all [fill]-compatible functions can be found [below](#fills).
2346 ///
2347 /// ex: ⬚0[1 2_3_4 5_6]
2348 /// ex: ⬚10+ [1 2 3 4] [5 6]
2349 /// ex: ⬚0≡⇡ [3 6 2]
2350 /// A fill value can be pulled from the stack with [identity].
2351 /// ex: ⬚∘[1 2_3_4] 0
2352 /// ex: ⬚∘+ ∞ [1 2] [3 4 5 6]
2353 ///
2354 /// Fill values are temporarily removed for the body of looping modifiers that can use them to fix their row shapes.
2355 /// These include [reduce], [scan], [rows], [partition], and [group].
2356 /// ex! ⬚0≡(↙3) [3 4]
2357 /// [un][pop] can be used to retrieve the fill value. This ignores loop nesting and so can be used to "pull" the fill into the loop.
2358 /// ex: ⬚0≡(⬚°◌↙3) [3 4]
2359 ///
2360 /// Fill values cannot cross the boundary of a named function call.
2361 /// ex: ⬚0/⊂ [1 2 3]
2362 /// : F ← /⊂
2363 /// : ⬚0F [1 2 3]
2364 /// [un][pop] *can* get the fill value through the function call. This means you can use [fill][un][pop] to get the fill value into a function.
2365 /// ex: F ← ⬚°◌/⊂
2366 /// : ⬚0F [1 2 3]
2367 /// This property includes index macros, but *not* code macros.
2368 ///
2369 /// [fill][pop] can be used to temporarily remove the fill value.
2370 /// ex: ⬚0 ↻ 2 [1 2 3 4 5]
2371 /// : ⬚0⬚◌↻ 2 [1 2 3 4 5]
2372 /// This does not affect [un][pop].
2373 /// ex: ⬚0 °◌
2374 /// ex: ⬚0⬚◌°◌
2375 ///
2376 /// [fill] and [un][pop] can be used to make a sort of ad-hoc variable system.
2377 /// ex: a ← (°□⊡0°◌)
2378 /// : b ← (°□⊡1°◌)
2379 /// : c ← (°□⊡2°◌)
2380 /// : ⬚{⊙⊙∘}(×b+c×a a) 2 3 4
2381 ([2], Fill, OtherModifier, ("fill", '⬚')),
2382 /// Call the function at the given index
2383 ///
2384 /// [switch] takes at least 1 argument, an index.
2385 /// If the index is `0`, the first function is called.
2386 /// If the index is `1`, the second function is called.
2387 /// ex: ⨬+- 0 3 5
2388 /// : ⨬+- 1 3 5
2389 /// The signatures of the functions do not need to match exactly.
2390 /// Excess arguments will be discarded.
2391 /// ex: ⨬˙×+ 0 3 5
2392 /// : ⨬˙×+ 1 3 5
2393 /// A function pack can be used to switch between more than 2 functions.
2394 /// ex: ⨬(+|-|×|÷) 0 2 5
2395 /// : ⨬(+|-|×|÷) 1 2 5
2396 /// : ⨬(+|-|×|÷) 2 2 5
2397 /// : ⨬(+|-|×|÷) 3 2 5
2398 /// The index does not have to be a scalar.
2399 /// ex: ⨬(+|-|×|÷) [0 1 2 3] 2 5
2400 /// In this case, [switch] behaves similarly to [rows]. The index will be iterated along with other arguments.
2401 /// ex: ⨬(+|-|×|÷) [0 1 2 3] [1 6 10 2] 5
2402 ([2], Switch, OtherModifier, ("switch", '⨬')),
2403 /// Call a function and catch errors
2404 ///
2405 /// If the first function errors, the second function is called with the original arguments and the error value.
2406 ///
2407 /// If the handler function has 0 arguments, then it is simply called. This is a nice way to provide a default value.
2408 /// ex: ⍣⋕0 "5"
2409 /// : ⍣⋕0 "dog"
2410 /// The handler function will be passed the original arguments, followed by the error value below them on the stack. It will not be passed arguments it doesn't need.
2411 /// Normal runtime errors become strings. If you only care about the error, you can use [gap] or [pop] to ignore the arguments passed to the handler.
2412 /// ex: ⍣(+1)⋅$"Error: _" 2 # No error
2413 /// ex: ⍣(+@a)⋅$"Error: _" @b # Error
2414 /// Errors thrown with [assert] can be any value.
2415 /// ex: ⍣(⍤5⊸>10)⋅(×5) 12 # No error
2416 /// ex: ⍣(⍤5⊸>10)⋅(×5) 7 # Error
2417 /// We can see how values are passed to the handler by wrapping them in an array.
2418 /// ex: ⍣⋕{⊙∘} "5" # No error
2419 /// : ⍣⋕{⊙∘} "dog" # Error
2420 /// ex: ⍣(⍤0.+)10 3 5 # Ignore both arguments and error
2421 /// : ⍣(⍤0.+)⊟₁ 3 5 # First argument only
2422 /// : ⍣(⍤0.+)⊟₂ 3 5 # Both arguments
2423 /// : ⍣(⍤0.+)⊟₃ 3 5 # Both arguments and error
2424 /// If we want to provide a default value from the stack, we can ignore it in the tried function with [gap] and then use [identity] in the handler.
2425 /// ex: ⍣⋅⋕∘ 5 "12" # No error
2426 /// : ⍣⋅⋕∘ 5 "dog" # Error
2427 /// The handler function may actually take *more* arguments than the first function. These additional arguments will be passed above the error. This can be used to pass additional context to the handler.
2428 /// ex: F ← ⍣+$"You can't add _ and _ because _: _"
2429 /// : F 2 3 "...you can"
2430 /// : F @a @b "they are both characters"
2431 /// : F [1 2] [3 4 5] "they have wrong shapes"
2432 /// [try] works with function packs of more than 2 functions. Each function will by tried in order, and all functions after the first will be passed the error value from the previous function.
2433 /// ex: F ← ⍣(⋕|{⊂2⊙∘}|{⊙∘})
2434 /// : F "5"
2435 /// : F [1]
2436 /// : F "hi"
2437 ([2], Try, Misc, ("try", '⍣')),
2438 /// Call a pattern matching case
2439 ///
2440 /// [case] calls its function and allows errors to escape from a single [try].
2441 /// Its primary use is in pattern matching.
2442 /// Consider this function:
2443 /// ex: F ← ⍣(
2444 /// : ⊏3 °(⊂1)
2445 /// : | ⊏1 °(⊂2)
2446 /// : | 0
2447 /// : )
2448 /// `F` attempts to [un]`(`[join]`1)` from the input array. Failing that, it attempts to [un]`(`[join]`2)`. In either `un``join` case, we subsequently [select] from the array. If both pattern matches fail, it returns `0` as a default.
2449 /// ex: F ← ⍣(
2450 /// : ⊏3 °(⊂1)
2451 /// : | ⊏1 °(⊂2)
2452 /// : | 0
2453 /// : )
2454 /// : F [1 2 3 4 5]
2455 /// : F [2 3 4 5]
2456 /// : F [5 2 3]
2457 /// However, there is a problem with this code.
2458 /// Pattern matching in a [try] works by throwing an error and passing the inputs to the next handler. However, if an error is thrown in a branch *after a successful pattern match*, the next branch will still be tried anyway.
2459 /// This could lead to some unexpected behavior.
2460 /// ex: F ← ⍣(
2461 /// : ⊏3 °(⊂1)
2462 /// : | ⊏1 °(⊂2)
2463 /// : | 0
2464 /// : )
2465 /// : F [1 5 8]
2466 /// In the example above, we successfully `un``(``join``1)`. However, the code after that pattern match fails. [select] errors because the index `3` is out of bounds of our array `[5 8]`. Instead of failing the whole function, the next branch is tried. It fails too, so we end up with `0`.
2467 /// This could be especially problematic if the next branches have side-effects.
2468 /// ex: F ← ⍣(
2469 /// : ⊏3 &p"Matched 1!" °(⊂1)
2470 /// : | ⊏1 &p"Matched 2!" °(⊂2)
2471 /// : | 0 &p"Matched nothing!"
2472 /// : )
2473 /// : F [1 2 3 4]
2474 /// This prints 2 messages, even though the whole function should have failed.
2475 /// Code that doesn't fail when it should can lead to bugs that are hard to track down.
2476 /// We want our errors to be loud!
2477 ///
2478 /// This is where [case] comes in. [case] has one special thing it does that makes it useful: errors returned from [case]'s first function can escape a single [try].
2479 /// We can then arrange our [try] pattern matching with a [case] for each branch. The code in each branch that comes after the pattern match is wrapped in a [case].
2480 /// ex! F ← ⍣(
2481 /// : ⍩(⊏3) °(⊂1)
2482 /// : | ⍩(⊏1) °(⊂2)
2483 /// : | 0
2484 /// : )
2485 /// : F [1 2 3 4]
2486 /// And there we go. Task failed successfully!
2487 ([1], Case, Misc, ("case", '⍩')),
2488 /// Throw an error if a condition is not met
2489 ///
2490 /// Expects a message and a test value.
2491 /// If the test value is anything but `1`, then the message will be thrown as an error.
2492 /// ex! ⍤"Oh no!" "any array"
2493 /// ex: ⍤"Oh no!" 1
2494 /// ex! ⍤"Oh no!" 0
2495 /// As you can see, a top-level [assert] is interpreted as a test in some contexts. See the [Testing Tutorial](/tutorial/testing) for more information.
2496 /// Use [duplicate] if you do not care about the message.
2497 /// ex: ⍤. =6 6
2498 /// ex! ⍤. =8 9
2499 /// Errors thrown by [assert] can be caught with [try].
2500 (2(0), Assert, Misc, ("assert", '⍤'), Impure),
2501 /// Generate a random number in the range `[0, 1)`
2502 ///
2503 /// If you need a seeded random number, use [gen].
2504 ///
2505 /// ex: ⚂
2506 /// ex: [⚂⚂⚂]
2507 ///
2508 /// Use [multiply] and [floor] to generate a random integer in a range.
2509 /// ex: ⌊×10 ⍥⚂5
2510 /// The range can be given with a subscript.
2511 /// ex: ⍥⚂₁₀5
2512 ///
2513 /// `rows``gap``random` and `table``gap``gap``random` are optimized in the interpreter to generate a lot of random numbers very fast.
2514 /// ex: ⌊×10 ≡⋅⚂ ⇡10
2515 /// ex: ⌊×10 ⊞⋅⋅⚂ .⇡10
2516 (0, Rand, Rng, ("random", '⚂'), Impure),
2517 /// Memoize a function
2518 ///
2519 /// If a function is [memo]ized, then its results are cached.
2520 /// Calling the function with the same arguments will return the cached result instead of recalculating it.
2521 /// ex: F ← +⌊×10⚂
2522 /// : ≡F [1 1 2 2 3 3]
2523 /// ex: F ← memo(+⌊×10⚂)
2524 /// : ≡F [1 1 2 2 3 3]
2525 /// In general, this should only be used with functions that perform a potentially expensive calculation.
2526 ([1], Memo, OtherModifier, "memo"),
2527 /// Run a function at compile time
2528 ///
2529 /// ex: F ← (⌊×10[⚂⚂⚂])
2530 /// : [F F F]
2531 /// ex: F ← comptime(⌊×10[⚂⚂⚂])
2532 /// : [F F F]
2533 /// [comptime]'s function must take no arguments.
2534 /// If you would like to pass arguments to [comptime]'s function, make them part of the function
2535 /// ex! comptime(+) 1 2
2536 /// ex: comptime(+ 1 2)
2537 ([1], Comptime, Comptime, "comptime"),
2538 /// Spawn a thread
2539 ///
2540 /// Expects a function.
2541 /// In the native interpreter, the function is called in a new OS thread.
2542 /// In the web editor, the function is called and blocks until it returns.
2543 /// A thread id that can be passed to [wait] is pushed to the stack. Handles are just numbers.
2544 /// [wait] consumes the thread id and appends the thread's stack to the current stack.
2545 /// ex: spawn⇡ 10
2546 /// : wait spawn⇡ 10
2547 /// ex: spawn(+10+) 1 2
2548 /// : wait spawn(+10+) 1 2
2549 ///
2550 /// You can use [rows] to spawn a thread for each row of an array.
2551 /// ex: ≡spawn(/+⇡˙×) ⇡10
2552 ///
2553 /// [wait] is pervasive.
2554 /// ex: ↯3_3⇡9
2555 /// : wait≡spawn/+.
2556 ///
2557 /// To spawn threads in a thread pool, use [pool].
2558 ([1], Spawn, Thread, "spawn", Impure),
2559 /// Spawn a thread in a thread pool
2560 ///
2561 /// Has the same functionality as [spawn], but uses a thread pool instead of spawning a new thread.
2562 /// While [spawn]'s function will be called immediately, [pool]'s function will be called when a thread in the pool is available.
2563 /// The thread pool has as many threads as the machine has processors.
2564 /// If all threads in the pool are busy, then [pool] will block until a thread is available.
2565 ([1], Pool, Thread, "pool", Impure),
2566 /// Wait for a thread to finish and push its results to the stack
2567 ///
2568 /// The argument must be a thread id returned by [spawn] or [pool].
2569 /// ex: wait spawn(/+⇡) 10
2570 ///
2571 /// If the thread id has already been [wait]ed on, then an error is thrown.
2572 /// ex! h ← spawn(/+⇡) 10
2573 /// : wait h
2574 /// : wait h
2575 ///
2576 /// [wait] is pervasive.
2577 /// ex: ↯3_3⇡9
2578 /// : wait≡spawn/+.
2579 ///
2580 /// [wait] will always return a single value. If the spawned function returns multiple values, they will be put in an array.
2581 /// ex: wait spawn(1 2 3)
2582 /// This means you need to box incompatible values if you want to return multiple from the thread.
2583 /// ex! wait spawn(1 "yes")
2584 /// ex: wait spawn{1 "yes"}
2585 (1, Wait, Thread, "wait", Mutating),
2586 /// Send a value to a thread
2587 ///
2588 /// Expects a thread id returned by [spawn] or [pool] and a value to send.
2589 /// The thread id `0` corresponds to the parent thread.
2590 /// The sent-to thread can receive the value with [recv] or [tryrecv].
2591 (2(0), Send, Thread, "send", Impure),
2592 /// Receive a value from a thread
2593 ///
2594 /// Expects a thread id returned by [spawn] or [pool].
2595 /// The thread id `0` corresponds to the parent thread.
2596 /// The sending thread can send a value with [send].
2597 ///
2598 /// Unlike [tryrecv], [recv] blocks until a value is received.
2599 (1, Recv, Thread, "recv", Impure),
2600 /// Try to receive a value from a thread
2601 ///
2602 /// Expects a thread id returned by [spawn] or [pool].
2603 /// The thread id `0` corresponds to the parent thread.
2604 /// The sending thread can send a value with [send].
2605 ///
2606 /// Unlike [recv], [tryrecv] does not block.
2607 /// If no value is available, then an error is thrown.
2608 /// The error can be caught with [try].
2609 (1, TryRecv, Thread, "tryrecv", Impure),
2610 /// Generate an array of random numbers with a seed
2611 ///
2612 /// The first argument is the shape, the second argument is the seed. The returned array will have the given shape where each element is in the range [0, 1).
2613 /// If you don't care about the seed or shape, you can use [random] instead.
2614 /// ex: gen [] 0
2615 /// ex: gen [] 1
2616 /// ex: gen 3 0
2617 /// ex: gen 3 1
2618 /// Use [multiply] and [floor] to generate a random integer in a range.
2619 /// ex: ⌊×10 gen 10 42
2620 /// ex: ⌊×10 gen 2_3 0
2621 /// ex: ⌊×10 gen 2_3 1
2622 /// A rank-2 array or box array of shapes can be used to generate multiple arrays. The resulting arrays will be in a boxed list
2623 /// ex: ⌊×10 gen [2_3 3_4] 0
2624 /// ex: ⌊×10 gen {2_2 [] 2_3_3 4} 0
2625 /// If you want a seed to use for a subsequent [gen], you can use [fork] and `[]`.
2626 /// ex: gen 8 ⊃(gen[]|gen5) 0
2627 /// : ∩(⌊×10)
2628 /// For non-determinism, [random] can be used as a seed.
2629 /// ex: ⌊×10 gen 3_4 ⚂
2630 (2, Gen, Rng, "gen"),
2631 /// Match a regex pattern
2632 ///
2633 /// Returns a rank-2 array of [box]ed strings, with one string per matching group and one row per match
2634 /// ex: regex "h([io])" "hihaho"
2635 /// ex: regex "hi" "dog"
2636 /// : △.
2637 /// ex: regex "[a-z]+" "hello world"
2638 /// If the pattern contains escaped characters such as `\w`, either these must be double escaped or the whole pattern must be represented with a raw string.
2639 /// ex: regex "\\d+" "123"
2640 /// ex: P ← $$ (\d{_})
2641 /// : regex $"_-_-_"P3P3P4 "123-456-7890"
2642 /// Regex patterns with optional captures can be used with [fill].
2643 /// ex: ⬚""regex "a(b)?" "a ab"
2644 /// [under] can be used to run arbitrary regex-based substitutions.
2645 /// ex: Lorem
2646 /// : ⍜regex≡(□⊂⋅⊙⇌°□₃) $ (\w)(\w+)
2647 ///
2648 /// Uiua uses the [Rust regex crate](https://docs.rs/regex/latest/regex/) internally.
2649 (2, Regex, Algorithm, "regex"),
2650 /// Convert a color array from RGB to HSV
2651 ///
2652 /// The last axis of the array must be `3` or `4`. This axis is the color channels. If present, a fourth channel is interpreted as an alpha channel and will be ignored.
2653 /// Hue is in radians between `0` and `tau`. Saturation and value are between `0` and `1`.
2654 /// ex: hsv [1 0 0]
2655 /// ex: hsv [0 1 0]
2656 /// ex: hsv [0 0 1]
2657 /// ex: hsv [Yellow Cyan Magenta]
2658 /// ex: hsv [Orange Purple Black White]
2659 /// [un][hsv] converts from HSV to RGB. This means it can be used with [under] to do various color manipulations.
2660 /// ex: ⍜(⊡0°⍉hsv|+π) ▽⟜≡▽0.5 Lena # Opposite hue
2661 /// ex: ⍜(⊡1°⍉hsv|÷2) ▽⟜≡▽0.5 Lena # Half saturation
2662 /// ex: ⍜(⊡2°⍉hsv|÷2) ▽⟜≡▽0.5 Lena # Half value
2663 (1, Hsv, Algorithm, "hsv"),
2664 /// Convert a string to UTF-8 bytes
2665 ///
2666 /// ex: utf₈ "hello!"
2667 /// ex: utf₈ "❤️"
2668 /// You can use [un] to convert UTF-8 bytes back to a string.
2669 /// ex: °utf₈ [226 156 168 32 119 111 119 33]
2670 ///
2671 /// [utf₈] is different from just [add]ing or [subtracting] `@\0`.
2672 /// Character math can only convert to and from UTF-32.
2673 /// ex: -@\0 "👩🏽👩🏻👦🏻👧🏽"
2674 /// ex: utf₈ "👩🏽👩🏻👦🏻👧🏽"
2675 ///
2676 /// You can subscript with `16` instead of `8` to get UTF-16.
2677 /// ex: utf₁₆ "Hi! 😀"
2678 /// If you are reading from a file, you'll have to convert the bytes to base 16 before decoding.
2679 /// ex: [0 87 0 104 0 121 0 63 0 32 216 61 222 53]
2680 /// : °utf₁₆ ≡/(+×256) ↯∞_2
2681 (1, Utf8, Encoding, "utf₈"),
2682 /// Convert a string to a list of UTF-8 grapheme clusters
2683 ///
2684 /// A Uiua character is a single Unicode code point.
2685 /// A [grapheme cluster](https://unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries) is a sequence of Unicode code points that combine into a single visual character.
2686 /// [graphemes] splits a string into its grapheme clusters.
2687 /// ex: graphemes "🏳️⚧️ 👩🏼🤝👩🏽 ȗ̵̬ị̶̿u̴̠͘ă̸̰"
2688 /// : ≡¤
2689 /// : -@\0.
2690 ///
2691 /// [graphemes] works with [un] and [under].
2692 /// ex: ⍜graphemes≡◇⊢ "ų̶̢̢̛̥͈̖̦̜̥͔͕̙͚̜͚͊͋̑̿̔̓̐͐̀̓̐̈́̑͆͆͘į̴̥̞̀̑̋̀̽̌̓̐̓̚ư̷̯̖͈͇̌͌́̄̿͑̓̚͜͜à̶͓̜̗̩̝̰̲͎͉̲͆̇͗̄̆̏̍̑̍͌͝ͅ"
2693 (1, Graphemes, Encoding, "graphemes"),
2694 /// Generate a unique tag
2695 ///
2696 /// Tags are just numbers and are unique across multiple threads, but not across multiple runs.
2697 /// ex: ⍥tag5
2698 /// : ⍥tag5
2699 (0, Tag, Misc, "tag", Impure),
2700 /// Check the type of an array
2701 ///
2702 /// `0` indicates a number array.
2703 /// `1` indicates a character array.
2704 /// `2` indicates a box array.
2705 /// `3` indicates a complex array.
2706 /// ex: type 5
2707 /// ex: type i
2708 /// ex: type "hello"
2709 /// ex: type □[5 6]
2710 /// ex: ≡type {10 "dog" [1 2 3]}
2711 /// : ≡◇type {10 "dog" [1 2 3]}
2712 (1, Type, Misc, "type"),
2713 /// Get the current time in seconds
2714 ///
2715 /// Time is expressed in seconds since the Unix epoch.
2716 /// ex: now
2717 /// [under][now] can be used to time a function.
2718 /// ex: ⍜now(5&sl1)
2719 (0, Now, Time, "now", Impure),
2720 /// Get the date and time information from a time
2721 ///
2722 /// You can use [now] to get the current time in seconds since the Unix epoch.
2723 /// [datetime] turns a time into an array with 6 numbers:
2724 /// - Year
2725 /// - Month (1-12)
2726 /// - Day (1-31)
2727 /// - Hour (0-23)
2728 /// - Minute (0-59)
2729 /// - Second (0-59)
2730 ///
2731 /// ex: datetime now
2732 /// The time is always in UTC.
2733 /// [datetime] is semi-pervasive.
2734 /// ex: datetime [1e8 1e9 1e10]
2735 /// You can format the time like this:
2736 /// ex: datetime now # Time
2737 /// : ⍚(⬚@0↙¯⊙°⋕) [4....2] # Pad
2738 /// : °[°$"_-_-_ _:_:_"] # Format
2739 ///
2740 /// You can use [un][datetime] to convert an array back into a time.
2741 /// An array with fewer than 6 numbers will be padded with zeros.
2742 /// ex: °datetime [2023 2 28 1 2 3]
2743 /// ex: °datetime [2014_4_1 2022_3_31]
2744 /// Invalid numbers in the datetime will be normalized.
2745 /// ex: ⍜°datetime∘ [2023 2 29]
2746 /// ex: ⍜°datetime∘ [1917 5 0]
2747 /// ex: ⍜°datetime∘ [1996 12 ¯100]
2748 (1, DateTime, Time, "datetime"),
2749 /// Get the local timezone offset
2750 ///
2751 /// ex: timezone
2752 /// ex: datetime +×3600 timezone now
2753 (0, TimeZone, Time, "timezone", Impure),
2754 /// The number of radians in a quarter circle
2755 ///
2756 /// Equivalent to `divide``2``pi` or `divide``4``tau`
2757 /// ex: [η π/2 τ/4]
2758 (0, Eta, Constant, ("eta", 'η')),
2759 /// The ratio of a circle's circumference to its diameter
2760 ///
2761 /// Equivalent to `multiply``2``eta` or `divide``2``tau`
2762 /// ex: [2η π τ/2]
2763 (0, Pi, Constant, ("pi", 'π')),
2764 /// The ratio of a circle's circumference to its radius
2765 ///
2766 /// Equivalent to `multiply``4``eta` or `multiply``2``pi`
2767 /// ex: [4η 2π τ]
2768 (0, Tau, Constant, ("tau", 'τ')),
2769 /// The biggest number
2770 ///
2771 /// ex: ∞
2772 /// ex: +1 ∞
2773 /// ex: -1 ∞
2774 /// ex: ↧5 ∞
2775 /// ex: ↥5 ∞
2776 (0, Infinity, Constant, ("infinity", '∞')),
2777 /// Create a hashmap from a list of keys and list values
2778 ///
2779 /// A hashmap is a normal array that is used as a mapping from keys to values.
2780 /// The related map functions [insert], [has], and [get], all treat the array as an actual hashmap, so they have O(1) amortized time complexity.
2781 /// Because the values array maintains insertion order, the [remove] function has O(n) time complexity.
2782 ///
2783 /// ex: map 1_2 3_4
2784 /// : map {"Alice" "Bob" "Carol"} [3_8 12_2 4_5]
2785 /// Use [get] to get the value corresponding to a key.
2786 /// ex: map 1_2 3_4
2787 /// : get 2 .
2788 /// Use [insert] to insert additional key-value pairs.
2789 /// ex: map 1_2 3_4
2790 /// : insert 5 6
2791 /// An empty array can be used as an empty map, even if it was not created with [map].
2792 /// ex: has 5 []
2793 /// : insert 1 2 []
2794 /// You can use [un][map] to get the keys list and values list back.
2795 /// ex: °△0_2
2796 /// : insert 1 2_3
2797 /// : insert 4 5_6
2798 /// : insert 7 8_9
2799 /// : °map .
2800 ///
2801 /// Pervasive operations work on the values of a map, but not on the keys.
2802 /// ex: ×10 map 1_2_3 4_5_6
2803 /// Some normal array operations work on maps:
2804 /// - [reverse]
2805 /// - [rotate]
2806 /// - [sort]
2807 /// - [classify]
2808 /// - [deduplicate]
2809 /// - [take]
2810 /// - [drop]
2811 /// - [join]
2812 /// - [select] (if every index is covered exactly once)
2813 /// - [rows]
2814 /// Operations that do not specifically work on maps will remove the keys and turn the map into a normal array.
2815 ///
2816 /// [fix]ing a map will [fix] the keys and values. This exposes the true structure of the keys array.
2817 /// ex: ¤ map 3_10_5 "abc"
2818 /// This is usually only useful with [rows].
2819 /// ex: ≡get [1 3 3 2] ¤ map 1_2_3 4_5_6
2820 /// But you can normally do this without [rows] at all.
2821 /// ex: get [1 3 3 2] map 1_2_3 4_5_6
2822 ///
2823 /// Map keys are stored as metadata on the values array. For this reason, they cannot be put in arrays together without being [box]ed, as the metadata for each map would be lost.
2824 ///
2825 /// Regardless of the size of the map, operations on it have O(1) amortized time complexity.
2826 /// In this example, we time [get] and [insert] operations on maps from 10 entries up to 100,000 entries.
2827 /// ex: Times ← (
2828 /// : map.⇡
2829 /// : ⊟⊃(
2830 /// : ⊙◌⍜now(get 5)
2831 /// : | ⊙◌⍜now(insert 1 2))
2832 /// : )
2833 /// : ˜ⁿ10+1⇡5
2834 /// : ≡Times.
2835 (2, Map, Map, "map"),
2836 /// Insert a key-value pair into a map array
2837 ///
2838 /// See [map] for an overview of map arrays.
2839 ///
2840 /// The array is used as an actual hashmap, so some entries may be empty.
2841 /// ex: []
2842 /// : insert 1 2
2843 /// : insert 3 4
2844 /// : insert 5 6
2845 /// If the key is already present, it is replaced.
2846 /// ex: []
2847 /// : insert 1 2
2848 /// : insert 3 4
2849 /// : insert 3 5
2850 // /// Multiple key-value pairs can be inserted at once. This will happen when the inserted key has the same rank and type as the map's keys.
2851 // /// ex: map 1_2 "ab"
2852 // /// : insert 3_4 "cd"
2853 // /// Notice that only a single key-value pair will be inserted if the inserted value has a lower rank than the map's value.
2854 // /// ex: map [1_2] ["ab"]
2855 // /// : insert 3_4 "cd"
2856 /// Keys that are already present keep their order.
2857 /// ex: map 1_2_3 4_5_6
2858 /// : insert 1 10
2859 /// Here is a pattern for [remove]ing a key if it is present before [insert]ing it, so that the key moves to the end.
2860 /// ex: map 1_2_3 4_5_6
2861 /// : insert⟜⍜⊙◌remove 1 10
2862 /// All keys (and all values) must have the same shape and type.
2863 /// ex! map 1 ["wow"]
2864 /// : insert "hi" "there"
2865 /// [box] keys or values if you need to. Values will coerce to boxes if necessary.
2866 /// ex: map 1 ["wow"]
2867 /// : insert □"hi" □"there"
2868 /// ex: map □1 □"wow"
2869 /// : insert "hi" "there"
2870 ///
2871 /// See also: [has], [get], [remove]
2872 (3, Insert, Map, "insert"),
2873 /// Check if a map array has a key
2874 ///
2875 /// See [map] for an overview of map arrays.
2876 ///
2877 /// ex: map 1_2 3_4
2878 /// : has 2 .
2879 /// The presence of multiple keys can be checked at once.
2880 /// ex: map 1_2 3_4
2881 /// : has 2_5 .
2882 ///
2883 /// See also: [insert], [get], [remove]
2884 (2, Has, Map, "has"),
2885 /// Get the value corresponding to a key in a map array
2886 ///
2887 /// See [map] for an overview of map arrays.
2888 ///
2889 /// ex: map 1_2_3 4_5_6
2890 /// : get 2 .
2891 /// Multiple values can be retrieved at once.
2892 /// ex: map 1_2_3 4_5_6
2893 /// : get 1_3 .
2894 /// If the key is not found, an error is thrown.
2895 /// ex! map 1_2 3_4
2896 /// : get 5 .
2897 /// You can use [fill], [try], or [has] to avoid the error.
2898 /// ex: map 1_2 3_4
2899 /// : ⬚0get 5 .
2900 /// ex: map 1_2 3_4
2901 /// : ⍣get0 5 .
2902 /// ex: map 1_2 3_4
2903 /// : ⨬⋅⋅0get ◡has 5 .
2904 /// You can provide a default value with [fill].
2905 /// ex: map 1_2 3_4
2906 /// : ⊃(⬚0get 1|⬚0get 5)
2907 /// You can use [under][get] to modify the value at the key.
2908 /// ex: /map⍉ [1_2 3_4 5_6]
2909 /// : ⍜(get3|×10)
2910 ///
2911 /// See also: [insert], [has], [remove]
2912 (2, Get, Map, "get"),
2913 /// Remove the value corresponding to a key from a map array
2914 ///
2915 /// See [map] for an overview of map arrays.
2916 ///
2917 /// The key is removed if it is present.
2918 /// If the key is not present, the array is unchanged.
2919 /// ex: map 1_2 3_4
2920 /// : remove 2 .
2921 /// : remove 5 .
2922 /// Multiple values can be removed at once
2923 /// ex: map 1_2_3 4_5_6
2924 /// : remove 1_3_4
2925 ///
2926 /// Unlike the other map functions, [remove] has O(n) time complexity.
2927 ///
2928 /// See also: [insert], [has], [get]
2929 (2, Remove, Map, "remove"),
2930 /// Debug print all stack values without popping them
2931 ///
2932 /// This is equivalent to [dump][identity], but is easier to type.
2933 ///
2934 /// This is useful when you want to inspect the current ordering of the stack.
2935 /// For example, if you are juggling some values on the stack, you can use [stack] to inspect the stack afterwards:
2936 /// ex: 1 2 3
2937 /// : ◡⊙∘˜⊙.
2938 /// : ?
2939 /// : +×-×+
2940 /// ex: 2_3_10 ? 17 ↯3_4⇡12
2941 /// : ++
2942 /// Subscripted [stack] prints that many values from the stack.
2943 /// ex: ?₂ 1 2 3 4
2944 /// If you type `N+1` `?`s, it will format to [stack] subscripted with `N`.
2945 /// A subscripted `?` will merge adjacent `?s` into its subscript.
2946 /// ex: # Try formatting!
2947 /// : ???? 1 2 3 4
2948 /// : ?₂?? 5 6 7 8
2949 (0(0), Stack, Debug, ("stack", '?'), Mutating),
2950 /// Preprocess and print all stack values without popping them
2951 ///
2952 /// [dump][identity] is equivalent to [stack].
2953 /// ex: dump∘ 1 2 3
2954 /// This is useful when you want to inspect the current ordering of the stack.
2955 /// For example, if you are juggling some values on the stack, you can use [dump] to inspect the stack afterwards:
2956 /// ex: 1 2 3
2957 /// : ◡⊙∘˜⊙.
2958 /// : dump∘
2959 /// : +×-×+
2960 /// [dump][shape] is useful if your raw array data isn't worth looking at, but the shapes are.
2961 /// ex: 2_3_10 17 ↯3_4⇡12
2962 /// : dump△
2963 /// : ++
2964 /// ex: ↯¯1_5 ⇡30
2965 /// : ⍉.⊃≡(⊟.)(⊞+.).
2966 /// : dump△
2967 /// : +++∩∩⧻
2968 /// Errors encountered within [dump]'s function are caught and dumped as strings.
2969 /// ex: 1_2_3 [] 5_6_7
2970 /// : dump⊢
2971 (0(0)[1], Dump, Debug, "dump", Mutating),
2972 /// Convert a string into code at compile time
2973 ///
2974 /// ex: # Experimental!
2975 /// : quote("+1") 5
2976 ///
2977 /// The opposite of [quote] is [stringify].
2978 (0[1], Quote, Comptime, "quote", { experimental: true }),
2979 /// Run the Fast Fourier Transform on an array
2980 ///
2981 /// The Fast Fourier Transform (FFT) is an optimized algorithm for computing the Discrete Fourier Transform (DFT). The DFT is a transformation that converts a signal from the time domain to the frequency domain.
2982 ///
2983 /// The input array must be either real or complex.
2984 /// The result will always be complex.
2985 /// Multi-dimensional arrays are supported. The algorithm will be run across each axis.
2986 ///
2987 /// In this example, we generate some data that is the sum of some [sine] waves.
2988 /// We then run [fft] on it and create a plot of the resulting frequency bins.
2989 /// ex: ÷⟜⇡200 # 200 numbers between 0 and 1
2990 /// : /+∿⊞×[100 200 400] # Add some frequencies
2991 /// : ⌵ fft # Run the FFT
2992 /// : ↘⌊÷2⧻. # Drop the top half
2993 /// : ⬚0≡▽⊙1 ×15 # Render
2994 ///
2995 /// You can use [un][fft] to calculate the inverse FFT.
2996 /// In this example, we generate a list of `1`s representing frequency bins and run `un``fft` on it to get time-domain data. We can listen to the result as audio.
2997 /// ex: [220 277 330 440] # Frequencies
2998 /// : ⬚0↙ &asr °⊚ # Put 1 in buffer for each frequency
2999 /// : ◌°ℂ °fft # Run inverse FFT and get the real part
3000 ///
3001 /// Because [fft] runs on every axis of an array, we can get the frequency domain of each color channel of an image using [under][un][transpose][fft].
3002 /// ex: Lena
3003 /// : ▽₂ 0.5
3004 /// : ⌵⍜°⍉≡fft .
3005 (1, Fft, Algorithm, "fft"),
3006 /// Convert an operation to be in geometric algebra
3007 ///
3008 /// You can read more about [geometric] and its uses [here](/docs/experimental#geometric-algebra). This page only covers its use for complex numbers.
3009 ///
3010 /// [geometric] treats numeric arrays with a shape ending in `2` as an array of complex numbers. This is different than existing [complex] arrays, and this system would potentially replace that one.
3011 /// We can see the basic complex identity by multiplying two arrays that represent `i`. [geometric] [multiply] forms the geometric product, which is equivalent to the complex product in this case.
3012 /// ex: # Experimental!
3013 /// : ⩜× [0 1] [0 1]
3014 /// [geometric] treats most operations as pervasive down to that last axis.
3015 /// ex: # Experimental!
3016 /// : ⩜× [0 1] [1_2 3_4 5_6]
3017 /// : ⩜+ [0 1] [1_2 3_4 5_6]
3018 /// [geometric] [sign] normalizes a complex number.
3019 /// ex: # Experimental!
3020 /// : ⩜± [3_4 ¯2_0]
3021 /// [geometric] [absolute value] gives the magnitude of a complex number.
3022 /// ex: # Experimental!
3023 /// : ⩜⌵ [3_4 5_12]
3024 /// [geometric] [divide] produces a complex number that, when [multiply]d, rotates the first complex number to the second.
3025 /// ex: # Experimental!
3026 /// : ⩜÷ [0 1] [1 0]
3027 /// ex: # Experimental!
3028 /// : ⩜(×÷) [0 1] [1 0] [2_3 4_5 6_7]
3029 /// [geometric] [couple] creates a complex number array from real and imaginary parts.
3030 /// ex: # Experimental!
3031 /// : ⩜⊟ 1_2 [3_4 5_6]
3032 /// [geometric][un][parse] formats a complex array as complex numbers.
3033 /// ex: # Experimental!
3034 /// : ⩜°⋕ 5_1
3035 /// : ⩜°⋕ [1_2 3_4]
3036 ([1], Geometric, Algorithm, ("geometric", '⩜'), { experimental: true }),
3037 /// Find the shortest path between two things
3038 ///
3039 /// Expects 2 functions and at least 1 value.
3040 /// The value is the starting node.
3041 /// The first function should return 1 or 2 arrays of equal [length].
3042 /// - An array of the neighboring nodes must always be returned.
3043 /// - An array of costs may be returned above the nodes array on the stack. If ommitted, all costs are assumed to be 1.
3044 /// The second function should return whether or not the goal node has been reached.
3045 ///
3046 /// When called, [path] will pop any additional arguments its functions need from the stack.
3047 /// On each iteration, the current node will be passed to each function, along with any of the additional arguments that each function needs.
3048 ///
3049 /// If a path is found, a list of arrays of all shortest paths is returned.
3050 /// If costs were returned from the neighbors functions, then each path array will be [box]ed, and the cost will be returned as well.
3051 /// If costs were not returned, then all paths must necessarily be the same length, so boxing is not necessary, and the cost is just the length of any path.
3052 /// If no path is found, an empty list and a cost of `infinity` are returned.
3053 ///
3054 /// In this example, we find the shortest path from the 2D point `0_0` to `3_5` in a grid.
3055 /// We use the `A₂` constant to get an array of offsets for adjacent neighbors in two dimensions.
3056 /// The goal function simply checks if the current node [match]es the given goal node.
3057 /// ex: $Neighbors A₂ # Side-adjacent neighbors offsets
3058 /// :
3059 /// : 0_0 3_5 # Start and goal
3060 /// : °□⊢path(
3061 /// : ≡⊸1 +A₂¤ # Costs and neighbors
3062 /// : | ≍ # Check if goal
3063 /// : )
3064 /// : ⊓$Path$Cost
3065 /// As stated before, the costs can be omitted. Notice [un][box]ing is not necessary in this case, and a cost is not returned.
3066 /// ex: ⊢ path(+A₂¤)≍ 0_0 3_5
3067 /// In the examples above, we use [first] to get only the first path. [first][path], [pop][path] and [sign][length][path] are optimized to not do extra work.
3068 /// If we want *all* shortest paths, we can omit [first].
3069 /// ex: path(+A₂¤)≍ 0_0 1_2
3070 /// If pathing on a grid like the examples above, we can use [un][where] to visualize the path that was taken!
3071 /// ex: ⊢ path(+A₂¤)≍ 3_4 10_14
3072 /// : °⊚
3073 /// : ▽⟜≡▽8 # Upscale
3074 /// There are no guarantees about the order of the paths, only that they all have the same cost.
3075 ///
3076 /// If given a function pack with 3 functions, [path] uses the [A*](https://en.wikipedia.org/wiki/A*_search_algorithm) algorithm.
3077 /// The third function should return a heuristic cost to reach the goal node from the current node.
3078 /// - The heuristic should return a value [less or equal] the actual cost
3079 /// - It must *never* overestimate the cost, or the algorithm may not find the shortest path
3080 /// The heuristic function `absolute value``reduce``complex``subtract` calculates the euclidean distance between two points.
3081 /// ex: ⊢ path(+A₂¤|≍|⌵/ℂ-) 0_0 3_5
3082 /// With a good heuristic, A* is generally faster than [path], which uses a [Dijkstra](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)-like algorithm.
3083 ///
3084 /// Pathfinding isn't just good for solving problems with grids or graphs.
3085 /// Anything that involves finding a series of steps to get from one point to another is a good candidate for [path].
3086 /// For example, you can use it to find edits to a string to turn it into another string.
3087 /// ex: ⊢path(+⊙¤ ⊂¯.⊞=. °⊏)≍ "thud" "uiua"
3088 /// [path] is designed to be maximally flexible, so it can be used with graphs or grids or any other structure.
3089 ((2)[2], Path, Algorithm, "path"),
3090 /// Execute a recursive or tree algorithm
3091 ///
3092 /// Takes three functions.
3093 /// The first function checks for a base case.
3094 /// The second function gets a node's children.
3095 /// The third function combines parent and child nodes.
3096 ///
3097 /// If the first function returns a boolean, it determines whether a given node is a leaf node. The second function will only be called on non-leaf nodes.
3098 /// The third function is passed the results of all a node's children. If it takes at least 2 arguments, it will also be passed the parent node on top.
3099 ///
3100 /// Many of the examples here can be better expressed using array operations, and are merely demonstrative.
3101 ///
3102 /// We can express a simple recursive factorial function like so.
3103 /// ex: # Experimental!
3104 /// : Fact ← recur(<2|-1|×)
3105 /// : Fact 5
3106 /// : Fact 7
3107 /// The [less than]`2` determines when to cease recursion. The [subtract]`1` is the next recursive call, the "child node". The [multiply] gets called on a node and the result of its child.
3108 ///
3109 /// We can express the classic recursive fibanacci function in a similar way. In this example, the second function returns two children. The third function ignores the parent node and simply adds the results of the children.
3110 /// ex: # Experimental!
3111 /// : Fib ← recur(<2|⊃[-1|-2]|/+)
3112 /// : Fib 10
3113 /// Because a boolean result from the first function returns a node as its own result, that example interprets the 0th fibonacci number to be `0`.
3114 /// If we instead want the 0th fibonacci to be `1`, we can return a list of 0 or 1 items from the first function instead. A 1-item list is interpreted as a leaf node, with that item as the result.
3115 /// In this example, we use `keep``dip``1` to return `[1]` if the node is `less than``2` or `[]` if it is not.
3116 /// ex: # Experimental!
3117 /// : Fib ← recur(▽⊙1<2|⊃[-1|-2]|/+)
3118 /// : Fib 10
3119 ///
3120 /// The results of a node's children will be passed to the third function as an array. The creation of this array will fail if the results of the children have incompatible shapes. There is an exception for box lists, which will be [join]ed instead of used as rows. This makes it possible to combine variable-length lists.
3121 /// One example use case for this is listing all files in all subdirectories.
3122 /// ex: # Experimental!
3123 /// : ListFiles ← recur&fif&fld∘
3124 /// : ListFiles "."
3125 ///
3126 /// This simple example inverts a binary tree.
3127 /// ex: # Experimental!
3128 /// : {1_2_3 4 {5_6_7 8 9}}
3129 /// : °□recur(≤1◇⧻|◇⇌|□)
3130 ([3], Recur, Algorithm, "recur", { experimental: true }),
3131 /// Calculate the derivative of a mathematical expression
3132 ///
3133 /// Basic polynomials are supported, along with [sine] and [logarithm].
3134 /// ex: # Experimental!
3135 /// : # x² → 2x
3136 /// : ∂˙× 5
3137 /// ex: # Experimental!
3138 /// : # √x → 1/(2√x)
3139 /// : ∂√ 1/9
3140 /// ex: # Experimental!
3141 /// : # x² - 2x - 4 → 2x - 2
3142 /// : ∂(++⊃(ⁿ2|ׯ2|¯4)) [0 1 2]
3143 /// [derivative][sine] is a simple way to make a cosine function.
3144 /// ex: # Experimental!
3145 /// : # sin(x) → cos(x)
3146 /// : ⁅₃ ∂∿ ×τ÷⟜⇡8
3147 /// Most derivatives that would require the chain or product rule are not supported.
3148 /// ex! # Experimental!
3149 /// : # xsin(x) → sin(x) + xcos(x)
3150 /// : ∂(×∿.) ×τ÷⟜⇡8
3151 /// They do work if the inner derivative is a constant.
3152 /// ex: # Experimental!
3153 /// : # sin(2x) → 2cos(2x)
3154 /// : ∂(∿×2) ×τ÷⟜⇡8
3155 ///
3156 /// See also: [integral]
3157 ([1], Derivative, Algorithm, ("derivative", '∂'), { experimental: true }),
3158 /// Calculate an antiderivative of a mathematical expression
3159 ///
3160 /// Basic polynomials are supported, along with [sine].
3161 /// ex: # Experimental!
3162 /// : # x² → x³/3
3163 /// : ∫˙× 3
3164 /// ex: # Experimental!
3165 /// : # √x → (2x^1.5)/3
3166 /// : ∫√ 1
3167 /// ex: # Experimental!
3168 /// : # 2x + 5 → x² + 5x
3169 /// : ∫(+5×2) 2
3170 /// You can compute the integral over a range with [subtract][both].
3171 /// ex: # Experimental!
3172 /// : # 1/x → ln(x)
3173 /// : -∩∫(÷⊙1) 1 e
3174 /// Most integrals that would require u-substitution or integration by parts are not supported.
3175 /// ex! # Experimental!
3176 /// : # xsin(x) → sin(x) - xcos(x)
3177 /// : ∫(×∿.) ×τ÷⟜⇡8
3178 ///
3179 /// See also: [derivative]
3180 ([1], Integral, Algorithm, ("integral", '∫'), { experimental: true }),
3181 /// Encode an array into a JSON string
3182 ///
3183 /// ex: json [1 2 3]
3184 /// ex: json {"some" "words"}
3185 /// ex: json map {"hey" "there" "buddy"} {1 2 [3 4 5]}
3186 /// You can use [un][json] to decode a JSON string back into an array.
3187 /// ex: °json "[4,5,6]"
3188 /// ex: °json $ ["what's","the","plan"]
3189 /// ex: °json $ {"foo": "bar", "baz": [1, 2, 3]}
3190 ///
3191 /// While the number literals `0` and `1` are converted to their number equivalents in JSON, the shadowable constants `True` and `False` are converted to JSON `true` and `false`.
3192 /// ex: json {0 1 2 3 True False}
3193 ///
3194 /// [un][json] will never form multidimensional arrays, as the shape data is lost.
3195 /// ex: °json json [1_2_3 4_5_6]
3196 ///
3197 /// While [json] always produces ECMA-compliant JSON, [un][json] can parse [JSON5](https://json5.org/).
3198 /// This means that you can use single quotes, unquoted keys, trailing commas, and comments.
3199 /// ex: °json $ {foo: 'bar', /* cool */ baz: [1, 2, 3,],}
3200 ///
3201 /// Note that `NaN` and [infinity] convert to JSON `null`, and JSON `null` converts to `NaN`.
3202 /// This means that [infinity] is converted to `NaN` in a round-trip.
3203 /// ex: json [1 ¯5 NaN ∞]
3204 /// ex: °json "[1,null,-3,null]"
3205 (1, Json, Encoding, "json"),
3206 /// Encode an array into a CSV string
3207 ///
3208 /// The input array must be at most rank-`2`.
3209 /// ex: csv [1 2 3]
3210 /// ex: csv ↯3_4⇡12
3211 /// ex: csv [{"Foo" "Bar"} [1 2] [3 4] [5 6]]
3212 /// You can use [un][csv] to decode a CSV string back into an array.
3213 /// ex: °csv "#,Count\n1,5\n2,21\n3,8\n"
3214 /// By default, rows of mismatched length are padded with empty strings.
3215 /// ex: °csv "1,2,3\n4\n5,6"
3216 /// This can be changed with [fill].
3217 /// ex: ⬚"x"°csv "1,2,3\n4\n5,6"
3218 /// The default delimiter is (of course) a comma. However, [fill] can be used to change it.
3219 /// ex: °⬚@;csv "1;2;3\n4\n5,6;7"
3220 /// [fill] outside the [un] pads rows of different lengths. [fill] inside the [un] chooses the delimiter.
3221 /// ex: ⬚"x"°⬚@;csv "1;2;3\n4\n5,6;7"
3222 /// The decoding result will always be a rank-`2` array of boxed strings.
3223 /// You can use `rows``try``parse``gap``identity` to convert the strings that represent numbers.
3224 /// ex: ≡₀⍣⋕∘ °csv "#,Count\n1,5\n2,21\n3,8\n"
3225 /// If you know there are headers, you can use [un][join] to separate them.
3226 /// ex: ⊙⋕°⊂ °csv "#,Count\n1,5\n2,21\n3,8\n"
3227 /// You can easily create a [map] with the headers as keys.
3228 /// ex: map⊙(⍉⋕)°⊂ °csv "#,Count\n1,5\n2,21\n3,8\n"
3229 (1, Csv, Encoding, "csv"),
3230 /// Encode an array into XLSX bytes
3231 ///
3232 /// XLSX is a spreadsheet format that can be edited in programs like Microsoft Excel, Google Sheets, and LibreOffice Calc.
3233 /// Spreadsheets are just arrays, so array languages like Uiua are a natural fit for working with them.
3234 ///
3235 /// The input value must be a sheet array or a [map] array with sheet names as keys and sheet arrays as values.
3236 /// Sheet arrays may be at most rank `2`.
3237 /// XLSX is a binary format, so the output is a byte array.
3238 ///
3239 /// You can use [un][xlsx] to decode an XLSX byte array back into a sheet map.
3240 /// In the resulting sheet map, each sheet will be a boxed rank-`2` array of boxed values.
3241 ///
3242 /// While it is not useful to display the output bytes here, we can see how the result of decoding works:
3243 /// ex: °xlsx xlsx . ↯3_6⇡18
3244 (1, Xlsx, Encoding, "xlsx"),
3245 /// Encode an array into a compact binary representation
3246 ///
3247 /// This is useful for saving Uiua arrays to files.
3248 /// Being `# Experimental`, the format is currently subject to backward-incompatible changes.
3249 /// The format is not meant to be compatible with any other existing format. For en/decoding binary data from external sources, use [bytes].
3250 ///
3251 /// Any array can be encoded unless it:
3252 /// - contains an I/O handle or FFI pointer
3253 /// - has a rank `greater than``255`
3254 /// - has a very high nesting via [box] or [map]
3255 ///
3256 /// ex: # Experimental!
3257 /// : binary [1 2 3 4]
3258 /// ex: # Experimental!
3259 /// : binary {"Hello" "World!"}
3260 /// ex: # Experimental!
3261 /// : binary {1 η_π_τ 4_5_6 "wow!"}
3262 ///
3263 /// You can use [un][binary] to decode a binary byte array into its original value.
3264 /// ex: # Experimental!
3265 /// : °binary . binary . map [1 2 3] [4 5 6]
3266 /// ex: # Experimental!
3267 /// : °binary . binary . {1 η_π_τ 4_5_6 "wow!"}
3268 ///
3269 /// [binary] adds at *least* 6 bytes of overhead to the encoded array. This includes at least 6 bytes for every box element.
3270 /// The overhead is type, shape, and metadata information.
3271 /// ex: # Experimental!
3272 /// : binary [1 2 3 4 5]
3273 /// : binary.
3274 /// : binary.
3275 ///
3276 /// For number arrays, the smallest type that can represent all the numbers is used so that the encoded array is as small as possible.
3277 /// ex: # Experimental!
3278 /// : ÷∩⧻⟜binary ⇡256 # u8s
3279 /// : ÷∩⧻⟜binary ⇡257 # u16s
3280 /// : ÷∩⧻⟜binary ÷⟜⇡256 # f32s
3281 /// : ÷∩⧻⟜binary ×π ⇡256 # f64s
3282 ///
3283 /// Complex arrays are always encoded as f64 pairs.
3284 /// ex: # Experimental!
3285 /// : ÷∩⧻⟜binary ℂ0 ⇡256
3286 (1, Binary, Encoding, "binary", { experimental: true }),
3287 /// Convert a value to its code representation
3288 ///
3289 /// ex: repr π
3290 /// Use [&p][repr] to produce a representation that can be pasted directly into the
3291 /// interpreter.
3292 /// ex: &p repr ↯2_2_2 0
3293 /// ex: &p repr {"Uiua" @A [1 2 3] □4}
3294 ///
3295 /// Append commas to whitespace for a more traditional notation:
3296 /// ex: -5↯2_2_3⇡12
3297 /// : ⍜⊜□⍚(⊂@,)⊸∊" \n" repr # add commas
3298 /// : &p ⍜▽≡⋅@-⊸=@¯ # replace negate glyphs with minus signs
3299 (1, Repr, Misc, "repr"),
3300 /// Convert a value to its pretty-printed output representation
3301 ///
3302 /// The output will always be a rank-2 character array.
3303 /// ex: pretty 5
3304 /// ex: pretty 1_2_3
3305 /// ex: pretty °△2_3
3306 /// ex: pretty {[1_2] [3_4 5_6]}
3307 /// ex: pretty [{[1_2] 3} {4_5 [6_7 8_9]}]
3308 ///
3309 /// [&s] is equivalent to `rows``&p``pretty`.
3310 (1, Pretty, Misc, "pretty"),
3311 /// Convert a value to a byte representation
3312 ///
3313 /// This function exists to interface with externally en/decoded binary data.
3314 /// If you want to simply read/write Uiua arrays to/from files, you may want to use [binary] instead.
3315 ///
3316 /// The first argument is the format, and the second is a number array to encode.
3317 ///
3318 /// Supported formats are:
3319 /// - `u8`
3320 /// - `i8`
3321 /// - `u16`
3322 /// - `i16`
3323 /// - `u32`
3324 /// - `i32`
3325 /// - `u64`
3326 /// - `i64`
3327 /// - `u128`
3328 /// - `i128`
3329 /// - `f32`
3330 /// - `f64`
3331 ///
3332 /// Numbers that do not fit in a format's range will be clamped to the range.
3333 /// ex: # Experimental!
3334 /// : bytes "u8" [1 2 3 256]
3335 /// : bytes "u16" [1 2 3 256]
3336 /// The result will always be a byte array. If the specified format takes more than 1 byte, the result will have an additional axis in its shape.
3337 /// ex: # Experimental!
3338 /// : bytes "u8" [1_2_3 4_5_6e9]
3339 /// : bytes "f32" [1_2_3 4_5_6e9]
3340 /// : bytes "f64" [1_2_3 4_5_6e9]
3341 ///
3342 /// [anti][bytes] will decode a byte array based on the format.
3343 /// ex: # Experimental!
3344 /// : [[24 45 68 84 251 33 249 63]
3345 /// : [24 45 68 84 251 33 9 64]
3346 /// : [24 45 68 84 251 33 25 64]
3347 /// : [0 0 0 0 0 0 240 127]]
3348 /// : ⌝bytes "f64"
3349 ///
3350 /// By default, [bytes] en/decodes in native endianness.
3351 /// You can specify little or big endian with a sided subscript.
3352 /// Left (`⌞`) is little endian, and right (`⌟`) is big endian.
3353 /// ex: # Experimental!
3354 /// : bytes "u64" 1234567890 # Native endian
3355 /// : bytes⌞ "u64" 1234567890 # Little endian
3356 /// : bytes⌟ "u64" 1234567890 # Big endian
3357 (2, EncodeBytes, Encoding, "bytes", { experimental: true }),
3358 /// Encode an image into a byte array with the specified format
3359 ///
3360 /// The first argument is the format, and the second is the image.
3361 ///
3362 /// The image must be a rank 2 or 3 numeric array or a rank 2 complex array.
3363 /// Axes 0 and 1 contain the rows and columns of the image.
3364 /// A rank 2 array is a grayscale image.
3365 /// A rank 3 array is an RGB image.
3366 /// A complex array is colored with domain coloring and no alpha channel.
3367 /// In a rank 3 image array, the last axis must be length 1, 2, 3, or 4.
3368 /// A length 1 last axis is a grayscale image.
3369 /// A length 2 last axis is a grayscale image with an alpha channel.
3370 /// A length 3 last axis is an RGB image.
3371 /// A length 4 last axis is an RGB image with an alpha channel.
3372 ///
3373 /// You can decode a byte array into an image with [un][img].
3374 ///
3375 /// Supported formats are `jpg`, `png`, `bmp`, `gif`, `ico`, and `qoi`.
3376 ///
3377 /// See also: [&ims]
3378 (2, ImageEncode, Encoding, "img"),
3379 /// Encode an array of image frames in a GIF-encoded byte array
3380 ///
3381 /// The first argument is a framerate in seconds.
3382 /// The second argument is the gif data and must be a rank 3 or 4 numeric array.
3383 /// The rows of the array are the frames of the gif, and their format must conform to that of [img].
3384 ///
3385 /// You can decode a byte array into a gif with [un][gif].
3386 ///
3387 /// See also: [&gifs]
3388 (2, GifEncode, Encoding, "gif"),
3389 /// Encode an array of image frames in an APNG-encoded byte array
3390 ///
3391 /// The first argument is a framerate in seconds.
3392 /// The second argument is the APNG data and must be a rank 3 or 4 numeric array.
3393 /// The rows of the array are the frames of the APNG, and their format must conform to that of [img].
3394 ///
3395 /// See also: [&apngs]
3396 (2, ApngEncode, Encoding, "apng"),
3397 /// Encode audio into a byte array
3398 ///
3399 /// The first argument is the format, the second is the audio sample rate, and the third is the audio samples.
3400 ///
3401 /// The sample rate must be a positive integer.
3402 /// The audio samples must be a rank 1 or 2 numeric array.
3403 /// A rank 1 array is a list of mono audio samples.
3404 /// For a rank 2 array, each row is a sample with multiple channels.
3405 /// The samples must be between -1 and 1.
3406 ///
3407 /// You can decode a byte array into audio with [un][audio].
3408 /// This returns the audio format as a string, the audio sample rate, and an array representing the audio samples.
3409 ///
3410 /// Currently, only the `wav` format is supported.
3411 ///
3412 /// This simple example will load an audio file, halve its sample rate, and re-encode it.
3413 /// ex! ⍜(°audio &frab "test.wav")⊙⊓(⌊÷2|▽0.5)
3414 ///
3415 /// See also: [&ap]
3416 (3, AudioEncode, Encoding, "audio"),
3417 /// Get the current operating system
3418 ///
3419 /// Returns a string representation.
3420 /// ex: os
3421 (0, Os, Environment, "os", Impure),
3422 /// Get the current operating system family
3423 ///
3424 /// Returns a string representation.
3425 /// ex: osfamily
3426 (0, OsFamily, Environment, "osfamily", Impure),
3427 /// Get the current architecture
3428 ///
3429 /// Returns a string representation.
3430 /// ex: arch
3431 (0, Arch, Environment, "arch", Impure),
3432 /// Get the DLL extension for the current platform
3433 ///
3434 /// Does not include the leading dot.
3435 /// ex: dllext
3436 (0, DllExt, Environment, "dllext", Impure),
3437 /// Get the executable extension for the current platform
3438 ///
3439 /// Does not include the leading dot.
3440 /// ex: exeext
3441 (0, ExeExt, Environment, "exeext", Impure),
3442 /// Get the primary path separator for the current platform
3443 ///
3444 /// Returns a scalar character.
3445 /// ex: pathsep
3446 (0, PathSep, Environment, "pathsep", Impure),
3447 /// Get the number of processors on the current system
3448 ///
3449 /// Returns a scalar integer.
3450 /// ex: numprocs
3451 (0, NumProcs, Environment, "numprocs", Impure),
3452 /// Render text into an image array
3453 ///
3454 /// The first argument is a font size and the second argument is the text to render.
3455 /// The result is a rank-2 array of pixel values.
3456 /// In this example, we map the pixel values to ASCII characters to visualize the result.
3457 /// ex: # Experimental!
3458 /// : layout 12 "Hello!"
3459 /// : ⊏⊙" @" ⁅ +0.1
3460 /// Multi-line text is supported.
3461 /// ex: # Experimental!
3462 /// : layout 30 "Hello,\nWorld!"
3463 /// The text to be rendered can be a character array or box array where all leaf nodes are strings.
3464 /// The top-level rows are treated as lines and will be separated by newlines.
3465 /// The bottom-level rows are treated as words and will be separated by spaces.
3466 /// ex: # Experimental!
3467 /// : {{"Words" "can" "be" "on"}
3468 /// : {"multiple" "lines"}}
3469 /// : layout 30
3470 /// ex: # Experimental!
3471 /// : layout 15 ⬚""↯∞_12 ⊜□⊸≠@ Lorem
3472 ///
3473 /// 4 optionals arguments are accepted:
3474 /// - `LineHeight` - The height of a line relative to the font size (default 1)
3475 /// - `Size` - The size of the rendering area. Use `∞` to use the smallest possible size.
3476 /// - `Color` - The text color. If set, the background defaults to transparent.
3477 /// - `Bg` - The background color.
3478 /// ex: # Experimental!
3479 /// : $ Uiua is an
3480 /// : $ array-oriented
3481 /// : $ programming
3482 /// : $ language
3483 /// : layout!(°⊸LineHeight 1.5 °⊸Size 300_350 °⊸Color 0.5_0.5_1) 30
3484 /// ex: # Experimental!
3485 /// : layout!(°⊸Color Green °⊸Bg Red) 100 "Green on Red!"
3486 (2, Layout, Encoding, "layout", Impure, { experimental: true }),
3487 /// Project a voxel array to an image
3488 ///
3489 /// [voxels] orthographically projects a 3D array of voxels to an image.
3490 ///
3491 /// The input voxel array must be rank 3 or 4.
3492 /// - A rank 3 array produces a grayscale image with no alpha channel.
3493 /// - A rank 4 array with last axis `2` produces a grayscale image with an alpha channel.
3494 /// - A rank 4 array with last axis `3` produces an color image with no alpha channel.
3495 /// - A rank 4 array with last axis `4` produces an color image with an alpha channel.
3496 ///
3497 /// Accepts 3 optionals arguments:
3498 /// - `Fog` - A "fog" color. Fog helps to give a sense of depth to the image. Can be scalar or a list of 3 numbers.
3499 /// - `Scale` - The ratio of voxel size to pixel size
3500 /// - `Camera` - A camera position vector. It will be normalized and placed outside the array.
3501 ///
3502 /// Here is a simple 5x5x5 voxel scene.
3503 /// ex: ⍥(⍉⊂1)3⬚0↯4_4_4⋯131191
3504 /// If we project it with no parameters, the result is not very interesting.
3505 /// ex: # Experimental!
3506 /// : ⍥(⍉⊂1)3⬚0↯4_4_4⋯131191
3507 /// : voxels
3508 /// We can scale up the image by passing a `Scale` factor.
3509 /// ex: # Experimental!
3510 /// : ⍥(⍉⊂1)3⬚0↯4_4_4⋯131191
3511 /// : voxels!°⊸Scale 20
3512 /// We can change the camera position with a 3D `Camera` vector
3513 /// ex: # Experimental!
3514 /// : ⍥(⍉⊂1)3⬚0↯4_4_4⋯131191
3515 /// : voxels!(°⊸Scale 20 °⊸Camera [1 2 1])
3516 /// Passing a `Fog` color lets us see depth.
3517 /// ex: # Experimental!
3518 /// : ⍥(⍉⊂1)3⬚0↯4_4_4⋯131191
3519 /// : voxels!(°⊸Scale 20 °⊸Camera [1 2 1] °⊸Fog Black)
3520 ///
3521 /// The image will be transparent if the voxel array has transparency.
3522 /// ex: # Experimental!
3523 /// : ↯5_5_5_2 0
3524 /// : ⍜⊢⋅1
3525 /// : [1_0_0 1_2_2 2_2_2 2_2_3 2_2_4]
3526 /// : [[1 1][1 1][1 0.3][1 0.3][1 0.3]]
3527 /// : ∧⍜⊙⊡⊙◌
3528 /// : voxels!(°⊸Scale 20 °⊸Camera [1.2 2 0.5] °⊸Fog Black)
3529 /// Color is also supported.
3530 /// ex: # Experimental!
3531 /// : ↯5_5_5_4 0
3532 /// : ⍜⊢⋅1
3533 /// : [1_0_0 1_2_2 2_2_2 2_2_3 2_2_4]
3534 /// : [[1 1 1 1][1 1 1 1][1 0 0 0.3][0 1 0 0.3][0 1 1 0.3]]
3535 /// : ∧⍜⊙⊡⊙◌
3536 /// : voxels!(°⊸Scale 20 °⊸Camera [1 2 0.5] °⊸Fog Black)
3537 ///
3538 /// Like with normal image arrays, [voxels] supports complex numbers.
3539 /// The same domain coloring algorithm is used as in [img] and [&ims].
3540 /// By default, because there is no alpha channel, only numbers with 0 magnitude are transparent.
3541 /// ex: # Experimental!
3542 /// : ⊞+⟜⊞ℂ. -⊸¬ ÷⟜⇡30
3543 /// : voxels!°⊸Scale 2
3544 /// We can set transparency by adding a 4th axis to the array. This is a complex alpha channel where the opacity is the magnitude of the complex number.
3545 /// ex: # Experimental!
3546 /// : ⊞+⟜⊞ℂ. -⊸¬ ÷⟜⇡30
3547 /// : ⍜°⍉(⊟⟜(<1⌵)) # Only show <1 magnitude
3548 /// : voxels!(°⊸Scale 2 °⊸Camera [0.5 2 2])
3549 ///
3550 /// You can show rotation of a voxel array by turning it into a gif.
3551 /// In this example, we create a list of angles and apply each one to the camera position using [un][atangent].
3552 /// ex: # Experimental!
3553 /// : -⊸¬ ÷⟜(°⍉⇡)↯3 50 # Cube from ¯1 to 1
3554 /// : <0.4⌵-⊙(+∩∿) °⊟₃ ×τ # z = (sin(τx) + sin(τy))/τ
3555 /// : ×τ÷⟜⇡30 # Rotation angles
3556 /// : ≡⌟voxels!(°⊸Scale 2 °⊸Fog [0 0.5 1] °⊸Camera [1 °∠])
3557 (1, Voxels, Encoding, "voxels", { experimental: true }),
3558);
3559
3560macro_rules! sys_op {
3561 ($(
3562 #[doc = $doc_rust:literal]
3563 $(#[doc = $doc:literal])*
3564 (
3565 $args:literal$(($outputs:expr))?$([$mod_args:expr])?,
3566 $variant:ident, $class:ident, $name:literal, $long_name:literal
3567 $(,$purity:ident)* $(,{experimental: $experimental:literal})?
3568 )
3569 ),* $(,)?) => {
3570 /// A system function
3571 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Sequence, Serialize, Deserialize)]
3572 #[serde(rename_all = "snake_case")]
3573 pub enum SysOp {
3574 $(
3575 #[doc = $doc_rust]
3576 $variant
3577 ),*
3578 }
3579
3580 impl SysOp {
3581 /// All system functions
3582 pub const ALL: [Self; 0 $(+ {stringify!($variant); 1})*] = [
3583 $(Self::$variant,)*
3584 ];
3585 /// Get the system function's short name
3586 pub fn name(&self) -> &'static str {
3587 match self {
3588 $(Self::$variant => $name),*
3589 }
3590 }
3591 /// Get the system function's long name
3592 pub fn long_name(&self) -> &'static str {
3593 match self {
3594 $(Self::$variant => $long_name),*
3595 }
3596 }
3597 /// Get the number of arguments the system function expects
3598 pub fn args(&self) -> usize {
3599 match self {
3600 $(SysOp::$variant => $args,)*
3601 }
3602 }
3603 /// Get the number of function arguments the system function expects if it is a modifier
3604 pub fn modifier_args(&self) -> Option<usize> {
3605 match self {
3606 $($(
3607 SysOp::$variant => Some($mod_args),
3608 )?)*
3609 _ => None
3610 }
3611 }
3612 /// Get the number of outputs the system function returns
3613 pub fn outputs(&self) -> usize {
3614 match self {
3615 $($(SysOp::$variant => $outputs as usize,)?)*
3616 _ => 1
3617 }
3618 }
3619 /// Get the system function's class
3620 pub fn class(&self) -> SysOpClass {
3621 match self {
3622 $(SysOp::$variant => SysOpClass::$class),*
3623 }
3624 }
3625 /// Whether the system function is pure
3626 pub fn purity(&self) -> Purity {
3627 match self {
3628 $($(SysOp::$variant => Purity::$purity,)*)*
3629 _ => Purity::Impure
3630 }
3631 }
3632 /// Check if the system function is experimental
3633 #[allow(unused_parens)]
3634 pub fn is_experimental(&self) -> bool {
3635 match self {
3636 $($(SysOp::$variant => $experimental,)*)*
3637 _ => false
3638 }
3639 }
3640 /// Get the system function's documentation string
3641 pub fn doc(&self) -> &'static str {
3642 match self {
3643 $(SysOp::$variant => concat!($doc_rust, $($doc, "\n"),*),)*
3644 }
3645 }
3646 }
3647 };
3648}
3649
3650sys_op! {
3651 /// Pause the execution and print the stack
3652 ///
3653 /// This is useful for debugging.
3654 /// On the website, each [&b], in the same editor, with the same input code, will end execution and print the stack.
3655 /// Running the code multiple times will allow the code to advance to the next [&b].
3656 /// Try it out!
3657 /// ex: # Experimental!
3658 /// : ≡(&b⇌&b) &b °△ &b 3_3 &b
3659 /// Once the execution has completed, the final stack state will be shown as normal. Running again will start from the beginning.
3660 ///
3661 /// In the native interpreter, [&b] pauses execution, prints the stack, and waits for the user to press enter.
3662 (0(0), Breakpoint, Misc, "&b", "breakpoint", Mutating, { experimental: true }),
3663 /// Print a nicely formatted representation of a value to stdout
3664 ///
3665 /// [&s] will print the value the same way it would appear at the end of a program, or from [?].
3666 /// ex: &s ℂ0 1
3667 /// : &s "hello,\tworld"
3668 /// : &s 1/3
3669 /// : &s π
3670 /// : &s @U
3671 ///
3672 /// [&s] is equivalent to `rows``&p``pretty`.
3673 ///
3674 /// To print values in a more standard way, see [&p].
3675 (1(0), Show, StdIO, "&s", "show", Mutating),
3676 /// Print a value to stdout
3677 ///
3678 /// Exactly like [&p], except that there is no trailing newline.
3679 ///
3680 /// See also: [&p], [&epf]
3681 (1(0), Prin, StdIO, "&pf", "print and flush", Mutating),
3682 /// Print a value to stdout followed by a newline
3683 ///
3684 /// [&p] differs from [&s] when printing rank 0 or 1 character arrays, and when printing scalar number types.
3685 /// ex: &p ℂ0 1
3686 /// : &p "hello,\tworld"
3687 /// : &p 1/3
3688 /// : &p π
3689 /// : &p @U
3690 /// [&p] will ignore any level of [box]ing.
3691 /// ex: &p □□□"In a box"
3692 ///
3693 /// See also: [&pf], [&ep]
3694 (1(0), Print, StdIO, "&p", "print with newline", Mutating),
3695 /// Print a value to stderr
3696 ///
3697 /// Exactly like [&ep], except that there is no trailing newline.
3698 ///
3699 /// See also: [&pf], [&ep]
3700 (1(0), PrinErr, StdIO, "&epf", "print error and flush", Mutating),
3701 /// Print a value to stderr followed by a newline
3702 ///
3703 /// See also: [&p], [&epf]
3704 (1(0), PrintErr, StdIO, "&ep", "print error with newline", Mutating),
3705 /// Read a line from stdin
3706 ///
3707 /// The normal output is a string.
3708 /// If EOF is reached, the number `0` is returned instead.
3709 /// Programs that wish to properly handle EOF should check for this.
3710 (0, ScanLine, StdIO, "&sc", "scan line", Mutating),
3711 /// Get the size of the terminal
3712 ///
3713 /// The result is a 2-element array of the height and width of the terminal.
3714 /// Height comes first so that the array can be used as a shape in [reshape].
3715 (0, TermSize, Env, "&ts", "terminal size", Mutating),
3716 /// Exit the program with a status code
3717 (1(0), Exit, Misc, "&exit", "exit", Mutating),
3718 /// Set the terminal to raw mode
3719 ///
3720 /// Expects a boolean.
3721 /// If enabled, the terminal will not echo characters or wait for a newline.
3722 ///
3723 /// [&sc] will still work, but it will not return until the user presses enter.
3724 /// To get individual characters, use [&rs] or [&rb] with a count of `1` and a handle of `0`, which is stdin.
3725 ///
3726 /// [un][&raw] will return the current state of the terminal.
3727 /// [under][&raw] will set raw mode, and then revert it to the previous state.
3728 (1(0), RawMode, Env, "&raw", "set raw mode", Mutating),
3729 /// Get the command line arguments
3730 ///
3731 /// The first element will always be the name of your script
3732 // Doesn't actually mutate, but this is necessary for the LSP
3733 (0, Args, Env, "&args", "arguments", Mutating),
3734 /// Get the value of an environment variable
3735 ///
3736 /// Expects a string and returns a string.
3737 /// If the environment variable does not exist, an error is thrown.
3738 (1, Var, Env, "&var", "environment variable"),
3739 /// Run a command and wait for it to finish
3740 ///
3741 /// Standard IO will be inherited. Returns the exit code of the command.
3742 ///
3743 /// Expects either a string, a rank `2` character array, or a rank `1` array of [box] strings.
3744 (1, RunInherit, Command, "&runi", "run command inherit", Mutating),
3745 /// Run a command and wait for it to finish
3746 ///
3747 /// Standard IO will be captured. The exit code, stdout, and stderr will each be pushed to the stack.
3748 ///
3749 /// Expects either a string, a rank `2` character array, or a rank `1` array of [box] strings.
3750 (1(3), RunCapture, Command, "&runc", "run command capture", Mutating),
3751 /// Run a command with streaming IO
3752 ///
3753 /// Expects either a string, a rank `2` character array, or a rank `1` array of [box] strings.
3754 /// Returns 3 stream handles.
3755 /// The first can be written to with [&w] to send input to the command's stdin.
3756 /// The second and third can be read from with [&rs], [&rb], or [&ru] to read from the command's stdout and stderr.
3757 /// Using [&cl] on *all 3* handles will kill the child process.
3758 /// [under][&runs] calls [&cl] on all 3 streams automatically.
3759 (1(3), RunStream, Command, "&runs", "run command stream", Mutating),
3760 /// Change the current directory
3761 ///
3762 /// [un][&cd] will output the current working directory.
3763 /// [under][&cd] will return to the original directory afterward.
3764 (1(0), ChangeDirectory, Filesystem, "&cd", "change directory", Mutating),
3765 /// Get the contents of the clipboard
3766 ///
3767 /// Returns a string of the clipboard's contents.
3768 /// This is not supported on the web.
3769 ///
3770 /// The inverse sets the clipboard, expecting a string.
3771 /// ex: °&clip +@A⇡6 # Try running then pasting!
3772 (0, Clip, Misc, "&clip", "get clipboard contents"),
3773 /// Sleep for n seconds
3774 ///
3775 /// On the web, this example will hang for 1 second.
3776 /// ex: ⚂ &sl 1
3777 (1(0), Sleep, Misc, "&sl", "sleep", Mutating),
3778 /// Read characters formed by at most n bytes from a stream
3779 ///
3780 /// Expects a count and a stream handle.
3781 /// The stream handle `0` is stdin.
3782 /// ex: &rs 4 &fo "example.txt"
3783 /// Using [infinity] as the count will read until the end of the stream.
3784 /// ex: &rs ∞ &fo "example.txt"
3785 ///
3786 /// [&rs] will attempt to read the given number of *bytes* from the stream.
3787 /// If the read bytes are not valid UTF-8, up to 3 additional bytes will be read in an attempt to finish a valid UTF-8 character.
3788 ///
3789 /// See also: [&rb]
3790 (2, ReadStr, Stream, "&rs", "read to string", Mutating),
3791 /// Read at most n bytes from a stream
3792 ///
3793 /// Expects a count and a stream handle.
3794 /// The stream handle `0` is stdin.
3795 /// ex: &rb 4 &fo "example.txt"
3796 /// Using [infinity] as the count will read until the end of the stream.
3797 /// ex: &rb ∞ &fo "example.txt"
3798 ///
3799 /// See also: [&rs]
3800 (2, ReadBytes, Stream, "&rb", "read to bytes", Mutating),
3801 /// Read from a stream until a delimiter is reached
3802 ///
3803 /// Expects a delimiter and a stream handle.
3804 /// The result will be a rank-`1` byte or character array. The type will match the type of the delimiter.
3805 /// The stream handle `0` is stdin.
3806 /// ex: &ru "Uiua" &fo "example.txt"
3807 (2, ReadUntil, Stream, "&ru", "read until", Mutating),
3808 /// Read lines from a stream
3809 ///
3810 /// [&rl] calls its function on each line in the stream without reading the entire stream into memory.
3811 /// Lines are delimited by either `\n` or `\r\n`.
3812 /// For each line, it will be pushed onto the stack and the function will be called.
3813 /// Additional arguments to the function will be bellow the line.
3814 /// Outputs in excess of the number of accumulators will be collected into arrays.
3815 (1[1], ReadLines, Stream, "&rl", "read lines", Mutating),
3816 /// Write an array to a stream
3817 ///
3818 /// If the stream is a file, the file may not be written to until it is closed with [&cl].
3819 /// The stream handle `1` is stdout.
3820 /// The stream handle `2` is stderr.
3821 /// ex: &cl &w "Hello, world!" . &fc "file.txt"
3822 /// : &fras "file.txt"
3823 (2(0), Write, Stream, "&w", "write", Mutating),
3824 /// Move to an absolute position in a file stream
3825 ///
3826 /// If the position is negative, it is an offset from the file end.
3827 ///
3828 /// ex: &rs 4 ⊸&seek 47 &fo "example.txt"
3829 (2(0), Seek, Stream, "&seek", "seek", Mutating),
3830 /// Invoke a path with the system's default program
3831 (1(1), Invoke, Command, "&invk", "invoke", Mutating),
3832 /// Close a stream by its handle
3833 ///
3834 /// This will close files, tcp listeners, and tcp sockets.
3835 (1(0), Close, Stream, "&cl", "close handle", Mutating),
3836 /// Open a file and return a handle to it
3837 ///
3838 /// ex: &fo "example.txt"
3839 /// The file can be read from with [&rs], [&rb], or [&ru].
3840 /// The file can be written to with [&w].
3841 /// In some cases, the file may not be actually written to until it is closed with [&cl].
3842 /// [under][&fo] calls [&cl] automatically.
3843 (1, FOpen, Filesystem, "&fo", "file - open"),
3844 /// Create a file and return a handle to it
3845 ///
3846 /// ex: &fc "file.txt"
3847 /// The file can be read from with [&rs], [&rb], or [&ru].
3848 /// The file can be written to with [&w].
3849 /// In some cases, the file may not be actually written to until it is closed with [&cl].
3850 /// [under][&fc] calls [&cl] automatically.
3851 (1, FCreate, Filesystem, "&fc", "file - create", Mutating),
3852 /// Create a directory
3853 ///
3854 /// ex: &fmd "path/to/dir"
3855 /// Nested directories will be created automatically.
3856 (1(0), FMakeDir, Filesystem, "&fmd", "file - make directory", Mutating),
3857 /// Delete a file or directory
3858 ///
3859 /// ex: &fde "example.txt"
3860 /// Deletes the file or directory at the given path.
3861 /// Be careful with this function, as deleted files and directories cannot be recovered!
3862 /// For a safer alternative, see [&ftr].
3863 (1(0), FDelete, Filesystem, "&fde", "file - delete", Mutating),
3864 /// Move a file or directory to the trash
3865 ///
3866 /// ex: &ftr "example.txt"
3867 /// Moves the file or directory at the given path to the trash.
3868 /// This is a safer alternative to [&fde].
3869 (1(0), FTrash, Filesystem, "&ftr", "file - trash", Mutating),
3870 /// Check if a file, directory, or symlink exists at a path
3871 ///
3872 /// ex: &fe "example.txt"
3873 /// ex: &fe "foo.bar"
3874 (1, FExists, Filesystem, "&fe", "file - exists"),
3875 /// List the contents of a directory
3876 ///
3877 /// The result is a list of boxed strings.
3878 /// ex: &fld "."
3879 (1, FListDir, Filesystem, "&fld", "file - list directory"),
3880 /// Check if a path is a file
3881 ///
3882 /// ex: &fif "example.txt"
3883 (1, FIsFile, Filesystem, "&fif", "file - is file"),
3884 /// Read all the contents of a file into a string
3885 ///
3886 /// Expects a path and returns a rank-`1` character array.
3887 ///
3888 /// ex: &fras "example.txt"
3889 /// You can use [under][&fras] to write back to the file after modifying the string.
3890 /// ex: ⍜&fras(⊂⊙"\n# Wow!") "example.txt"
3891 /// : &p&fras "example.txt"
3892 ///
3893 /// See [&frab] for reading into a byte array.
3894 (1, FReadAllStr, Filesystem, "&fras", "file - read all to string"),
3895 /// Read all the contents of a file into a byte array
3896 ///
3897 /// Expects a path and returns a rank-`1` numeric array.
3898 ///
3899 /// ex: &frab "example.txt"
3900 /// You can use [under][&frab] to write back to the file after modifying the array.
3901 /// ex: ⍜&frab(⊂⊙-@\0"\n# Wow!") "example.txt"
3902 /// : &p&fras "example.txt"
3903 ///
3904 /// See [&fras] for reading into a rank-`1` character array.
3905 (1, FReadAllBytes, Filesystem, "&frab", "file - read all to bytes"),
3906 /// Write the entire contents of an array to a file
3907 ///
3908 /// Expects a path and a rank-`1` array of either numbers or characters.
3909 /// The file will be created if it does not exist and overwritten if it does.
3910 ///
3911 /// The editor on the website has a virtual filesystem. Files written with [&fwa] can be read with [&fras] or [&frab].
3912 /// ex: Path ← "test.txt"
3913 /// : &fwa Path +@A⇡26
3914 /// : &fras Path
3915 (2(0), FWriteAll, Filesystem, "&fwa", "file - write all", Mutating),
3916 /// Show an image
3917 ///
3918 /// How the image is shown depends on the system backend.
3919 ///
3920 /// In the default backend, the image is shown in the terminal. Here you can make it use sixel by setting the `UIUA_ENABLE_SIXEL` environment variable to `1`. Otherwise it will try to use the `kitty` or `iTerm` image protocols and fall back to half-block image printing.
3921 /// On the web, the image is shown in the output area.
3922 ///
3923 /// The image must be a rank 2 or 3 numeric array or a rank 2 complex array.
3924 /// Axes 0 and 1 contain the rows and columns of the image.
3925 /// A rank 2 array is a grayscale image.
3926 /// A rank 3 array is an RGB image.
3927 /// A complex array is colored with domain coloring and no alpha channel.
3928 /// In a rank 3 image array, the last axis must be length 1, 2, 3, or 4.
3929 /// A length 1 last axis is a grayscale image.
3930 /// A length 2 last axis is a grayscale image with an alpha channel.
3931 /// A length 3 last axis is an RGB image.
3932 /// A length 4 last axis is an RGB image with an alpha channel.
3933 ///
3934 /// See also: [img]
3935 (1(0), ImShow, Media, "&ims", "image - show", Mutating),
3936 /// Show a gif
3937 ///
3938 /// The first argument is a framerate in seconds.
3939 /// The second argument is the gif data and must be a rank 3 or 4 numeric array.
3940 /// The rows of the array are the frames of the gif, and their format must conform to that of [img].
3941 ///
3942 /// See also: [gif]
3943 (2(0), GifShow, Media, "&gifs", "gif - show", Mutating),
3944 /// Show an APNG
3945 ///
3946 /// The first argument is a framerate in seconds.
3947 /// The second argument is the gif data and must be a rank 3 or 4 numeric array.
3948 /// The rows of the array are the frames of the gif, and their format must conform to that of [img].
3949 ///
3950 /// See also: [apng]
3951 (2(0), ApngShow, Media, "&apngs", "apng - show", Mutating),
3952 /// Play some audio
3953 ///
3954 /// The audio must be a rank 1 or 2 numeric array.
3955 ///
3956 /// A rank 1 array is a list of mono audio samples.
3957 /// For a rank 2 array, each row is a sample with multiple channels.
3958 ///
3959 /// The samples must be between -1 and 1.
3960 /// The sample rate is [&asr].
3961 ///
3962 /// See also: [audio]
3963 (1(0), AudioPlay, Media, "&ap", "audio - play", Mutating),
3964 /// Get the sample rate of the audio output backend
3965 ///
3966 /// ex: &asr
3967 /// Here is how you can generate a list of sample times for `4` seconds of audio:
3968 /// ex: ÷⤙(⇡×) 4 &asr
3969 /// Pass that to a periodic function, and you get a nice tone!
3970 /// ex: ÷4∿×τ×220 ÷⤙(⇡×) 4 &asr
3971 (0, AudioSampleRate, Media, "&asr", "audio - sample rate"),
3972 /// Synthesize and stream audio
3973 ///
3974 /// Expects a function that takes a list of sample times and returns a list of samples.
3975 /// The samples returned from the function must either be a rank 1 array or a rank 2 array with 2nd axis length 2.
3976 /// The function will be called repeatedly to generate the audio.
3977 /// ex: Sp ← 1.5
3978 /// : Mod ← ∿×π× # Modulate ? Freq Time
3979 /// : Note ← +110×20⌊÷4◿8
3980 /// : Bass ← ⟜(
3981 /// : ×0.2Mod×
3982 /// : | ×2+1⌊◿2 # Volume modulation freq
3983 /// : | ±Mod÷Sp⟜Note # Note
3984 /// : )
3985 /// : Kick ← Mod80√√◿1
3986 /// : Noise ← [⍥⚂10000]
3987 /// : Noisy ← ×⊸(↯△⊙Noise)
3988 /// : Hit ← Noisy /≠⊞<0.5_0.6 ÷⟜◿2
3989 /// : Hat ← ×0.3 Noisy <0.1 ÷⟜◿0.25
3990 /// : &ast(÷3/+⊃[Hat|Kick|Hit|Bass]×Sp)
3991 /// On the web, this will simply use the function to generate a fixed amount of audio.
3992 /// How long the audio is can be configured in the editor settings.
3993 (0(0)[1], AudioStream, Media, "&ast", "audio - stream", Mutating),
3994 /// Create a TCP listener and bind it to an address
3995 ///
3996 /// Use [&tcpa] on the returned handle to accept connections.
3997 ///
3998 /// See also: [&tlsl]
3999 (1, TcpListen, Tcp, "&tcpl", "tcp - listen", Mutating),
4000 /// Create a TLS listener and bind it to an address
4001 ///
4002 /// This function is currently untested.
4003 ///
4004 /// Use [&tcpa] on the returned handle to accept connections.
4005 ///
4006 /// The first argument is an IP address and port to bind.
4007 /// The second argument is a cert string.
4008 /// The third argument is a key string.
4009 ///
4010 /// See also: [&tcpl]
4011 (3, TlsListen, Tcp, "&tlsl", "tls - listen", Mutating),
4012 /// Accept a connection with a TCP or TLS listener
4013 ///
4014 /// Returns a stream handle
4015 /// [under][&tcpa] calls [&cl] automatically.
4016 (1, TcpAccept, Tcp, "&tcpa", "tcp - accept", Mutating),
4017 /// Create a TCP socket and connect it to an address
4018 ///
4019 /// Returns a stream handle
4020 /// You can make a request with [&w] and read the response with [&rs], [&rb], or [&ru].
4021 /// [under][&tcpc] calls [&cl] automatically.
4022 /// ex: $ GET / HTTP/1.1
4023 /// : $ Host: example.com
4024 /// : $ Connection: close
4025 /// : $
4026 /// : $
4027 /// : ⍜(&tcpc "example.com:80"|&rs∞⊸&w:)
4028 ///
4029 /// See also: [&tlsc]
4030 (1, TcpConnect, Tcp, "&tcpc", "tcp - connect", Mutating),
4031 /// Create a TCP socket with TLS support
4032 ///
4033 /// Returns a stream handle
4034 /// You can make a request with [&w] and read the response with [&rs], [&rb], or [&ru].
4035 /// [under][&tlsc] calls [&cl] automatically.
4036 /// ex: $ GET / HTTP/1.1
4037 /// : $ Host: example.com
4038 /// : $ Connection: close
4039 /// : $
4040 /// : $
4041 /// : ⍜(&tlsc "example.com:443"|&rs∞⊸&w:)
4042 ///
4043 /// See also: [&tcpc]
4044 (1, TlsConnect, Tcp, "&tlsc", "tls - connect", Mutating),
4045 /// Set a TCP socket to non-blocking mode
4046 (1, TcpSetNonBlocking, Tcp, "&tcpsnb", "tcp - set non-blocking", Mutating),
4047 /// Set the read timeout of a TCP socket in seconds
4048 (2(0), TcpSetReadTimeout, Tcp, "&tcpsrt", "tcp - set read timeout", Mutating),
4049 /// Set the write timeout of a TCP socket in seconds
4050 (2(0), TcpSetWriteTimeout, Tcp, "&tcpswt", "tcp - set write timeout", Mutating),
4051 /// Get the connection address of a TCP socket
4052 (1, TcpAddr, Tcp, "&tcpaddr", "tcp - address", Mutating),
4053 /// Bind a UDP socket
4054 ///
4055 /// Returns a UDP socket handle
4056 /// You can receive messages with [&udpr] or send messages with [&udps].
4057 /// You can adjust the maximum length of received messages using [&udpsml].
4058 /// The default is 256 bytes.
4059 ///
4060 /// ex: S ← &udpb "0.0.0.0:7777"
4061 /// :
4062 /// : ⍢(&p ˜$"_: _" °utf₈ &udpr S)1
4063 /// This example binds a socket and then continuously prints received UTF₈ messages.
4064 (1, UdpBind, Udp, "&udpb", "udp - bind", Mutating),
4065 /// Receive a single message from a UDP socket
4066 ///
4067 /// Uses the internal maximum length, the default is 256 bytes.
4068 /// To change the maximum length use [&udpsml]
4069 /// Returns data and the address of the sender.
4070 (1(2), UdpReceive, Udp, "&udpr", "udp - receive", Mutating),
4071 /// Send a message on a UDP socket
4072 ///
4073 /// The first argument is the message to send in bytes.
4074 /// The second argument is the address to send to.
4075 /// The third argument is the UDP socket to send on.
4076 (3(0), UdpSend, Udp, "&udps", "udp - send", Mutating),
4077 /// Set the maximum message length for incoming messages
4078 ///
4079 /// Warning: Messages that contain more bytes than the max length will be chopped off.
4080 ///
4081 /// The default is 256 bytes.
4082 (2(0), UdpSetMaxMsgLength, Udp, "&udpsml", "udp - set maximum message length", Mutating),
4083 /// Capture an image from a webcam
4084 ///
4085 /// Takes the index of the webcam to capture from.
4086 ///
4087 /// Returnes a rank-3 numeric array representing the image.
4088 (1, WebcamCapture, Misc, "&camcap", "webcam - capture", Mutating),
4089 /// Call a foreign function interface
4090 ///
4091 /// *Warning ⚠️: Using FFI is deeply unsafe. Calling a function incorrectly is undefined behavior.*
4092 ///
4093 /// The first argument is a list of boxed strings specifying the source and signature of the foreign function.
4094 /// The second argument is a list of values to pass to the foreign function.
4095 ///
4096 /// The first argument must be of the form `{"lib_path" "return_type" "function_name" "arg1_type" "arg2_type" …}`.
4097 /// The lib path is the path to the shared library or DLL that contains the function.
4098 /// Type names roughly match C types. The available primitive types are:
4099 /// - `void`
4100 /// - `char`
4101 /// - `short`
4102 /// - `int`
4103 /// - `long`
4104 /// - `long long`
4105 /// - `float`
4106 /// - `double`
4107 /// - `unsigned char`
4108 /// - `unsigned short`
4109 /// - `unsigned int`
4110 /// - `unsigned long`
4111 /// - `unsigned long long`
4112 /// Any unsigned type can be written with just a `u` such as `uint` instead of `unsigned int`.
4113 /// Suffixing any of these with `*` makes them a pointer type.
4114 /// Struct types are defined as a list of types between `{}`s separated by `;`s, i.e. `{int; float}`. A trailing `;` is optional.
4115 /// A struct with all fields of the same type can be written like `type[number]`. For example a pair of `int`s would be written `int[2]`.
4116 ///
4117 /// Arguments are passed as a list of boxed values.
4118 /// If we have a C function `int add(int a, int b)` in a shared library `example.dll`, we can call it like this:
4119 /// ex! # Experimental!
4120 /// : Lib ← &ffi ⊂□"example.dll"
4121 /// : Add ← Lib {"int" "add" "int" "int"}
4122 /// : Add {2 3} # 5
4123 ///
4124 /// Uiua arrays can be passed to foreign functions as pointer-length pairs.
4125 /// To do this, specify the type of the list items followed by `:n`, where `n` is the index of the parameter that corresponds to the length.
4126 /// The interpreter will automatically pass the number of elements in the array to this parameter.
4127 /// If we wave a C function `int sum(const int* arr, int len)` in a shared library `example.dll`, we can call it like this:
4128 /// ex! # Experimental!
4129 /// : Lib ← &ffi ⊂□"example.dll"
4130 /// : Sum ← Lib {"int" "sum" "int:1" "int"}
4131 /// : Sum {[1 2 3 4 5]} # 15
4132 ///
4133 /// [&ffi] calls can return multiple values.
4134 /// In addition to the return value, any parameters prefixed with `out` will be sent and returned as out parameters.
4135 /// If the out parameter is not marked as a pointer, it will be interpreted as a single value, and will be read back into an array when returned.
4136 /// If the out parameter is marked as a pointer, it will be interpreted as an array and will be returned as a pointer value. This is allows you to keep memory that is allocated by passing an array valid.
4137 /// If there is more than one output value (including the return value), [&ffi] will return a list of the boxed output values.
4138 ///
4139 /// Structs can be passed either as lists of boxed values or, if all fields are of the same type, as a normal array.
4140 /// If all fields of a struct returned by a foreign function are of the same type, the interpreter will automatically interpret it as an array rather than a list of boxed values.
4141 /// If we have a C struct `struct Vec2 { float x; float y; }` and a function `Vec2 vec2_add(Vec2 a, Vec2 b)` in a shared library `example.dll`, we can call it like this:
4142 /// ex! # Experimental!
4143 /// : Lib ← &ffi ⊂□"example.dll"
4144 /// : Vec₂ ← "float[2]"
4145 /// : Add ← Lib {Vec₂ "vec2_add" Vec₂ Vec₂}
4146 /// : Add {[1 2] [3 4]} # [4 6]
4147 ///
4148 /// If a foreign function returns or has an out-parameter that is a pointer type, a special array is returned representing the pointer. This array is not useful as a normal array, but it can be passed back as an [&ffi] argument, read from with [&memcpy], or freed with [&memfree].
4149 ///
4150 /// Coverage of types that are supported for binding is currently best-effort.
4151 /// If you encounter a type that you need support for, please [open an issue](https://github.com/uiua-lang/uiua/issues/new).
4152 (2, Ffi, Ffi, "&ffi", "foreign function interface", Mutating, { experimental: true }),
4153 /// Copy data from a pointer into an array
4154 ///
4155 /// *Warning ⚠️: [&memcpy] can lead to undefined behavior if used incorrectly.*
4156 ///
4157 /// This is useful for complex [&ffi] calls that are meant to return arrays.
4158 /// Expects a string indicating the type, a pointer, and a length.
4159 ///
4160 /// The returned array will always be rank-`1`.
4161 /// The type of the array depends on the pointer's type.
4162 ///
4163 /// For example, if we have a C function `int* get_ints(int len)` in a shared library `example.dll`, we can call it and copy the result like this:
4164 /// ex! # Experimental!
4165 /// : Lib ← &ffi ⊂□"example.dll"
4166 /// : GetInts ← Lib {"int*" "get_ints" "int"}
4167 /// : &memcpy ⊸GetInts 3
4168 ///
4169 /// Importantly, [&memcpy] does *not* free the memory allocated by the foreign function.
4170 /// Use [&memfree] to free the memory.
4171 /// ex! # Experimental!
4172 /// : Lib ← &ffi ⊂□"example.dll"
4173 /// : GetInts ← Lib {"int*" "get_ints" "int"}
4174 /// : ⊃&memfree&memcpy ⊸GetInts 3
4175 (2, MemCopy, Ffi, "&memcpy", "foreign function interface - copy", Mutating, { experimental: true }),
4176 /// Write data from an array into a pointer
4177 ///
4178 /// Expects a pointer, an index, and a value.
4179 ///
4180 /// This function is equivalent to the C syntax `pointer[index] = value`
4181 (3(0), MemSet, Ffi, "&memset", "write to memory", Mutating, { experimental: true }),
4182 /// Free a pointer
4183 ///
4184 /// *Warning ⚠️: [&memfree] can lead to undefined behavior if used incorrectly.*
4185 ///
4186 /// This is useful for freeing memory allocated by a foreign function, or by an out-pointer.
4187 /// Expects a pointer. If the pointer is `NULL` [&memfree] will do nothing.
4188 /// See [&memcpy] for an example.
4189 (1(0), MemFree, Ffi, "&memfree", "free memory", Mutating, { experimental: true }),
4190}