Anonymous macros
An anonymous macro, like a named macro, essentially consists of formula text and a parameter list. You therefore make a combination of functions and macros available in the form of a new function.
However, anonymous macros also have other properties that go beyond the possibilities of named macros:
- They have implicit parameters.
- You have an inner state. If an anonymous macro is evaluated several times (e.g. within the Map and MapZfunctions), this internal state is retained from evaluation to evaluation.
Syntax of anonymous macros
An anonymous macro is written as a hash "#", followed by a variable list in square brackets, followed by an expression in round brackets. This expression can itself be a sequence of assignments followed by a result term:
- #[](object) is the same as #Object, i.e. a function that returns the input object itself.
- #[$y](Plus[$y]) or #[$y](object+$y) is the same as #Plus, i.e. the addition function.
- #[$y]((object+$y)/2) is a mean value function.
- #[$y]($sum:=object+$y; $sum/2) is the same mean value function, but in this case an auxiliary variable was used.
Implicit parameters
In contrast to named macros, anonymous macros can record and use values that are given in their definition context - regardless of when, where and by what they are evaluated.
On the one hand, this avoids multiple evaluation of the same formula. On the other hand, values derived from external formula parameters can be incorporated into functions. Example:
$gewicht:=2/($Zeitraum+1);
#[](object+$gewicht).Map[$liste]
- let$list be an arbitrary list of numbers.
- During the evaluation, the anonymous macro #[](object+$weight) is applied to each element of $list The results generated in this way are collected and compiled in a list of numbers.
- The function #[](object+$weight) adds the value $weightto each number. Although the value of $weight is included in the calculation, the function is one-digit, i.e. it requires only one input and no other parameters (which the Map function would otherwise have to provide). The value of the $weight variable is determined by the definition context, i.e. the position of the diamond definition (and not the call context, i.e. the position where the map is applied in the example). Example:
$weight:=2/($period+1);
$function:=#[](object+$weight);
$weight:=0;
$function.map[$list]
$gewicht:=2/($Zeitraum+1);
$funktion:=#[](object+$gewicht);
$gewicht:=0;
$funktion.Map[$liste]
The result here is the same as above, although the value of the $weight variable in the macro application is different, namely 0. Even if, for example, the function object is passed to any named macro XX , which finally evaluates the function, e.g. by map , the function remains unaffected by any variables that are also called $weight and occur in the macro definition of XX .
Inner state of anonymous macros
Within functions that evaluate a function object multiple times (e.g. Map, Fold, MapZ), the variable assignments are retained from evaluation to evaluation. This can be used to include auxiliary values in an evaluation sequence that are derived from the results and auxiliary values of the previous macro application.
An example of this is the calculation of the exponential average or the Parabolic SAR indicator.
The principle can be illustrated with a simple example: The series of Fibonacci numbers 1, 1, 2, 3, 5, 8, 13,... results from the fact that (with the exception of the first two numbers) the last and the penultimate number in the list are added together.
The following formula calculates a list of $N Fibonacci numbers:
$n1:=0;
$n2:=1;
$liste:=MakeList($N);
#[]($n3:=$n1+$n2;
$n4:=$n1;
$n1:=$n2;
$n2:=$n3;
$n4).Map[$liste]
$list is generated here as an $N-elementlist. However, their values are irrelevant as they are not included in the calculation.
The anonymous macro contains the next two Fibonacci numbers in the variables $n1 and $n2 . The next Fibonacci number is calculated for each evaluation by Map . The contents of the variables are then moved accordingly. The function finally returns the old content of $n1, i.e. the next number.