Values and primitives
20. Conditionals
setif(condition if_expr else_expr)
Returns if_expr when condition is non-zero, otherwise else_expr.
Both expressions are always evaluated, so both must be valid.
let N = 5
let size = setif(lt(4 N) "large" "small")
let USE_BUFFER = 1
define make_buffer(n) { rect(0 0 n 1) }
let r = setif(USE_BUFFER make_buffer(N) tile())
Value rules that differ from C and Python
| You write | You get |
|---|---|
define f(n) { setif(lt(n 2) 1 n*f(n-1)) } | A crash: both branches are evaluated, so a function cannot end its own recursion with setif. Build repetition with hstackrep, vstackrep, rep, arrayplace or range. |
let b = a then arrayupdate(b 0 9) | a changes too: arrays are shared, not copied. arrayupdate and arraydelete change the array in place and return nothing, so do not assign their result. An all-constant literal such as [1 2 3] is shared as well, so updating it inside a function changes it for later calls. Copy with arrayslice(a 0 arraylen(a)) first. |
query([b:20 cc:30] [a:1 b:2 c:3]) | 1 20 3: keys missing from the defaults are ignored without a message, so a misspelt key gets the default. |
range(10 0 0-1) | [ ]: range counts up only. |
21. Arrays
let a = [1 2 3 4 5] (* array literal *)
let b = range(8) (* [0 1 2 3 4 5 6 7] *)
let c = range(2 6) (* [2 3 4 5] *)
let d = range(0 10 2) (* [0 2 4 6 8] *)
Elements are separated by whitespace (no commas).
22. Dictionaries
let opts = [a:1 b:2 c:3]
Used with query() for default-value lookups (similar to keyword arguments):
define module(options) {
let [a b c] = query(options [a:1 b:2 c:3])
}
23. Arithmetic Primitives
| Primitive | Description |
|---|---|
add(x y) | Addition (alternative to +) |
sub(x y) | Subtraction (alternative to -) |
mul(x y) | Multiplication (alternative to *) |
div(x y) | Division (alternative to /) |
idiv(x y) | Integer division, truncated toward zero (alternative to //) |
mod(x y) | Remainder with the sign of x (alternative to %) |
cmpeq(x y) | Equality (alternative to ==) |
lt(x y) | Less-than (returns 0 or 1) |
max(x y ...) | Maximum of arguments |
min(x y ...) | Minimum of arguments |
pi(x y ...) | Product of arguments |
int(x) | Cast to integer |
sqrt(x) | Square root (returns float) |
split(x y) | Ceiling division: ⌈x / y⌉ |
cliff(x y) | Cliff distribution: returns [normal last] |
assert(x msg) | Asserts x ≠ 0, otherwise error with message |
range(stop) | Array [0, 1, ..., stop−1] |
range(start stop) | Array [start, start+1, ..., stop−1] |
range(start stop step) | Array with step |
24. String Primitives
| Primitive | Description |
|---|---|
join(delim a b ...) | Join strings with delimiter |
symjoin(a b) | The kernel symbol $a.b (omits empty parts), for link; it is a symbol, not a string, so name() rejects it. join("." a b) gives the string |
strcmp(a b) | Compare strings (returns 0 if equal) |
str(x) | Convert integer to string |
chop(delim s) | Split string at first occurrence of delimiter |
25. Array Primitives
| Primitive | Description |
|---|---|
arraylen(a) | Length of array |
arraymax(a) | Maximum element (integer arrays) |
arrayidx(a i) | Element at index i |
arrayidxarray(a idxs) | Index array a with index array idxs |
arrayslice(a start end) | Slice [start, end) |
arrayslice(a start end step) | Slice with step |
arraydelete(a i) | Delete element at index i (in-place) |
arrayupdate(a i val) | Update element at index i (in-place) |
arrayrepoffset(a nrep offset) | Replicate array nrep times, adding offset each repetition |
arrayintersect(a b) | Sorted unique intersection of integer arrays |
arrayexclude(a b) | Sorted unique elements in a not in b |
array_scalar_add(a s) | Add scalar s to every element |
array_scalar_mult(a s) | Multiply every element by scalar s |
length(a) | Length of array (alias for arraylen) |
26. Debug Primitives
| Primitive | Description |
|---|---|
show(x ...) | Print values to stdout |