Program{}
The program{} feature allows for in-line programming in regular Blockpad equations. It can be useful for performing complex calculations or creating custom functions inside of a Blockpad report.
It also can be a useful way to transfer programming from Mathcad into Blockpad. In many cases, the Mathcad import tool will generate program{} code when importing a Mathcad file.
This page mostly consists of examples of the program{} function, of varying complexity. For many of the simpler ones, there are actually better ways to handle the example using regular functions in a Blockpad equation. For some of them, the simpler example is shown.
Some good things to know about syntax:
- All regular Blockpad functionality works inside a program{}, including units intelligence, all functions, arrays, text, and logic.
- A good understanding of how arrays work in Blockpad is useful, especially array indexing and advanced array functions.
- In many ways, the syntax is similar to Stemscript, the Blockpad scripting language, so the Stemscript page can be a good reference.
- Semi-colons mark the end of a programming line. Lines or spacing in the text do not affect the calculation.
- Semi-colons do not follow {}, for loops, or if/then statements.
- Variables and inside of program{} can be overwritten. Similary, items inside of arrays defined locally to a program{} can be over written by specifying it with an index.
- To specify what program{} should output, name or specify the output at the end.
- This can be the last line in an if/else statement, but not if the statement is inside of a for loop.
- In addition to the If() function, there are also if/else/else if statements, like in most scripting languages.
- The basic syntax is this:
- if (condition) {what to do if true)
- if (condition) {what to do if true} else {what to do if false}
- if (condition) {what to do if true} else if (condition2) {what to do if condition2 is true} else {what to do if false}
- If there is only one line after an if statement, then the braces {} may be ommited.
- There are two types of for loops.
- for (var name in array) {what to do in the loop}
- example: for (var step in stepsArray) {A(step) = step^2;}
- for (initial value, condition, step at end) {what to do in the loop}
- example: for (step = 1; step <=3; step++) {A(step) = step^2;}
- To get multiple results out of one program, use either arrays or objects. (See the end of the Stemscript page for how to use objects).
- Use shift+enter to create a new line in a formula. The visual editor can also be useful.
Examples
You can copy and paste the formulas/code from these examples into Blockpad equations.
Very basic program with multiple steps and output
Hypotenuse = program {
A = 3;
B = 4;
C = Sqrt(A^2 + B^2);
C;
}
Custom function with if statements
IsBetween0and100(number) = program {
if (number < 0) {
false;
} else if (number > 100) {
false;
} else {
true;
}
}
Simple formula alternative:
IsBetween0and100(number) = 0 < number < 100
Example with for loop
SumOfSquares(array) = program {
result = 0;
for (var item in array) {
result = result + item^2;
}
result;
}
Aside from just using the SumSq() function, you could also create your own using the Reduce() function without program{}:
SumOfSquares(array) = Reduce(array, x => x^2, (itemRes, Agg) => itemRes + Agg)
Example with for loop 2
SquareEveryItem(array) = program {
NewArray = Copy(array);
for (i = 1; i <= Rows(array); i++) {
for (j = 1; j <= Columns(array); j++) {
NewArray(i, j) = array(i, j)^2;
} }
NewArray;
}
Simple formula alternative:
SquareEveryItem(array) = Each(array, item => item^2)
Just using an exponent on the array will also work in most situations, unless the array is a square matrix, then it will do matrix math.
Custom function to replace an item in a one dimensional array
ReplaceItem(array, index, value) =program { result=Copy(array); result(index)=value; result; }
Custom function to test if a number is prime
IsPrime(number)=program {
if (number<1) {
false;
} else {
result = true;
for (var i in LinearSeries(2, number/2)) {
if (Modulus(number, i)==0) {
result = false;
} }
result;
} }
It is possible to do this in a single equation without program{}, but it makes more sense to create multiple custom function equations that build on each other (each line below should go in a separate equation):
PotentialDivisors(num) = LinearSeries(2, num/2)
ModOfEachDivisor(num) = Each(PotentialDivisors(num), div => Modulus(num, div))
IsAnyModulusZero(num) = CountIf(ModOfEachDivisor(num), ==0) > 0
IsPrime_4(num)=If(num<1, false, Not(IsAnyModulusZero(num)))
Generate an array of the fibonacci sequence out to n
fibonacci(n) = program {
if (n < 1) {
"n must not be less than 1";
} else if (n < 2) {
[0];
} else if (n < 3) {
[0; 1];
} else if (n < 4) {
[0; 1; 1];
} else {
result = MakeArray(n, 1);
result(1) = 0;
result(2) = 1;
for (var i in LinearSeries(3, n)) {
result(i) = result(i - 1) + result(i-2);
}
result;
}
}
Custom function that returns every other item from an array
EveryOtherItem(array) = program {
result=[];
counter=1;
for (var a in array) {
if (IsOdd(counter)) {
result=ConcatItems(result, [a]);
} counter=counter+1;
} result; }