Nerdamer Scripting
Nerdamer Scripting is the compact programming layer available inside text passed to nerdamer(...). Use
it when a single mathematical expression is not enough and you need assignments, user-defined functions, conditions,
loops, or local scope.
Notation and scripting side by side
import nerdamer from 'nerdamer';
// Nerdamer Notation: one mathematical expression
const expression = nerdamer('factor(x^2-1)');
// Nerdamer Scripting: define and use a function
const script = nerdamer(`
square(x):=x^2;
square(6)
`);
User-defined functions
Define a symbolic function in scripting with f(x):=..., or define it from application code with
nerdamer.setFunction(...). See
User-defined functions for both forms and how they relate.
For larger workflows built entirely in the scripting language, see the Scripting examples.
Scripting reference
The entries below are the parser functions classified as scripting by the generated documentation data.
and(arg1, arg2, ...)Returns 1 when every argument is nonzero and 0 otherwise.
block(...)Evaluates a sequence of Nerdamer Scripting expressions in order.
break()Exits the nearest Nerdamer Scripting loop.
continue()Skips the remainder of the current Nerdamer Scripting loop iteration.
each(container, value, body)Evaluates a body once for each value in a parser container.
evaluate(value[, substitutions]) | evaluate(value, variables, point)Evaluates a parser value immediately, optionally with substitutions.
for(arg1, arg2, arg3, arg4)Runs a four-part Nerdamer Scripting loop.
if(condition, whenTrue[, whenFalse])Evaluates one branch based on a Nerdamer Scripting condition.
iferror(arg1, arg2)Evaluates a fallback expression only when the first expression throws an ordinary error.
iserror(arg1)Returns 1 when evaluating an expression throws an ordinary error and 0 otherwise.
let(arg1, arg2, arg3, ...)Creates temporary Nerdamer Scripting bindings for a body expression.
not(arg1)Returns 1 for a zero argument and 0 for a nonzero argument.
or(arg1, arg2, ...)Returns 1 when at least one argument is nonzero and 0 otherwise.
return(arg1)Returns a value from the current symbolic function or outer scripting block.
while(arg1, arg2)Repeats a Nerdamer Scripting body while its condition is nonzero.
xor(arg1, arg2, ...)Returns 1 when an odd number of arguments are nonzero.
Assignment
Use : to assign a parser variable. Assignments are retained by the parser and can affect later calls until they are removed.
a:5;
b:a+2;
b
Use unassign(a) to remove one stored assignment, nerdamer.clearVars() from application code to clear them all, or let(...) for temporary local bindings.
Statement separators
Newlines are formatting whitespace, not statement separators. Use ; between multiple statements passed in one call.
f(x):=x^2+1;
f(12)
Blocks and local scope
block(...) evaluates expressions in sequence. let(...) creates temporary local bindings
while its body is evaluated.
block(a:2, b:3, a+b)
let(x, 2, x^3)
Conditions and loops
if, while, and for let a script choose or repeat expressions.
if(x>0, x, -x)
let(k,0,block(
while(3-k, k:k+1),
k
))
Control flow
return, break, and continue control how blocks, functions, and loops finish.
Their individual reference pages describe where each form applies.
