Skip to main content

The "Fold" function

You will find the following description of this function in the reference:

Fold

List.fold[function;start value]

Result typeParametersModuleResult
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


CODE
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


CODE
$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:

CODE
$list1.length.makelist.fold[

#[$idx](object + ($list1.nth[$idx] - $list2.nth[$idx]).expX[2]);0].sqrt

This practically creates a for loop.



JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.