The "Fold" function
You will find the following description of this function in the reference:
Fold | |||
---|---|---|---|
List.fold[function;start value] | |||
Result type | Parameters | Module | Result |
Object | Function Start value | The <function> is applied successively to each list element in the second argument and the previous function result (the <start value> the first time) in the first argument. The overall result is the result of the last evaluation. The"Fold" iterator function is very abstract and versatile, as the intermediate results can be of any object type (e.g. lists, time series). |
Beispiel 1
List(1;2;3).Fold[#Plus;0]
This example returns the sum of the numbers in the list, i.e. (((0 + 1) + 2) + 3) = 6.
Beispiel 2
$Liste.Fold[#Append;EmptyList]
This example merges all lists, so it returns the same as"$List.Concatenate".
In addition to these examples, cases can also be constructed for the "Fold" function in which the order of the parameters plays a role. If you have two vectors (in the form of two equally long lists of numbers), you can use the "Fold" function to calculate their Euclidean distance (the distance between two points, so to speak). The correlation matrix in the Infront Portfolio Manager can serve as an example:
$list1.length.makelist.fold[
#[$idx](object + ($list1.nth[$idx] - $list2.nth[$idx]).expX[2]);0].sqrt
This practically creates a for loop.