User-defined functions
Nerdamer can register symbolic functions in two ways: inside Nerdamer Scripting with :=, or from JavaScript / TypeScript with nerdamer.setFunction(...). Functions defined either way can be called later from Nerdamer Notation.
Define a function in Nerdamer Scripting
Write the function name and parameters on the left of :=, followed by the symbolic body.
import nerdamer from 'nerdamer';
nerdamer('f(x):=x^2');
nerdamer('f(3)').text();
// 9
Functions can accept more than one parameter:
nerdamer('g(x,y):=x^2+y');
nerdamer('g(3,4)').text();
// 13
nerdamer(...) calls can use it. Defining the same function name again replaces the previous definition.Define the same kind of function from JavaScript / TypeScript
Use nerdamer.setFunction(name, parameters, body) when the function definition comes from application code. The parameter names and function body are strings interpreted by Nerdamer.
import nerdamer from 'nerdamer';
nerdamer.setFunction('f', ['x', 'y'], 'x^2+y');
nerdamer('f(4,7)').text();
// 23
See nerdamer.setFunction() for the generated JavaScript / TypeScript API reference.
Which form should I use?
Use f(x):=... when the definition naturally belongs to Nerdamer input or a Nerdamer script. Use nerdamer.setFunction(...) when your JavaScript or TypeScript application is creating the definition directly. Both forms register a symbolic function that can be used in later Nerdamer expressions.
Redefining a function
nerdamer('f(x):=x^2');
nerdamer('f(3)').text();
// 9
nerdamer('f(x):=x+1');
nerdamer('f(3)').text();
// 4
For scripting constructs such as if, for, while, let, and block, return to Nerdamer Scripting.
