Procedure Brackets
Overview
Procedure brackets provide an additional level of functionality beyond that given by the
equality expression brackets.
The procedure brackets are given
by brackets such as {: :}. Inside the bracket is a block of executable script. When the
text renderer ecounters the procedure brackets, it runs the code contained within the brackets. It also provides a function
called print to the code within the brackets.
Anything passed to the print function is displayed in the text where the procedure brackets are located.
Simple Example
The following example demonstrates a simple hello world type use of the procedure brackets. The program within
the brackets contains two print statements, each of which prints text to the document.
{#
print('hello');
print(' world');
#}
LaTeX - Repeating by Groups
Sometimes you have datasets where there are natural groupings of the data, and you wish to have your latex document repeat a section
for each group. This can easily be accomplished using dynamic text and the
$group
service.
{#
let gp = await import('/lib/group/v1.0.0/group.mjs');
for(let group of gp.group($val('data'), p=>p.ticker).toArray()){
print(`
Ticker is `+group.key+`
Sum of prices = `+$list(group.value).map(p=>p.price).sum()+`
`);
}
#}
Creating Side Effects
Because the procedural brackets just run the code contained within the brackets, the code
can have side effects. In particular, the code can be used to set data within your workspace.
For example, the following code sets and array of numbers in your workspace to the name "data".
{#
$val.set('data', [1,2,3,4,5,6]);
#}
This data can then be referenced later in your document as follows, which just inserts the length of the array
(in this case, 6) into your text.
{= $val('data').length =}
This can be particulary useful when dealing with
boilerplate text.
In such a case, the boilerplate may have text such as the following:
The {= $val('model').name =} model is an ordinary least squares model.
In this case, the boilerplate expects an object in your workspace data which contains the name of the model. It then
inserts the model name in the text. This allows it to function for many similar models by cjust changing the name of the
model in your workspace data.
One way to address this would be to add the following:
{# $val.set('model', { name:'regression model' }) #}
The {= $val('model').name =} model is an ordinary least squares model.
Notice how just prior to using the model name, the model object is set in your workspace. For most boilerplate text, you will
want to set all your data objects at the top of the document.