• Operating mode
  • Existing computations
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WindowsLinuxPHPWEBDEV - Browser code
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Stored procedures
<Array>.Reduce (Function)
In french: <Tableau>.Agrège
Applies a computation to each element of a WLanguage array.
tabValeur est un tableau d'entiers = [1,2,3,4,5]
LaSomme est un entier
LaSomme = tabValeur.Agrège((Valeur, LaSomme) => { RENVOYER Valeur + LaSomme }, 0)
// Note: cet exemple est didactique ; pour faire la somme des éléments d'un tableau,
// il est préférable d'utiliser la fonction Somme
<Result> = <Array>.Reduce(<Operation to perform> [, <InitialValue>])
<Result>: Type corresponding to the result
Result of the requested reduce operation.
<Array>: WLanguage array
Name of the Array variable to use. This array must be a one-dimensional array.
<Operation to perform>: WLanguage procedure
Name of WLanguage procedure to run. This procedure can be:
  • a global or local procedure,
  • an internal procedure.
It is also possible to directly use a lambda.
<InitialValue>: Optional parameter, type corresponding to the initial value
Initial value to use the first time the <Operation to perform> procedure is run.

Operating mode

The <Operation to perform> procedure is called a first time with two parameters:
  • the first element of the array,
  • the initial value specified with <Initial value>.
The procedure is then called for all other elements of the array with two parameters:
  • the element of the array,
  • the value returned by the previous call.
Illustrative example
Let's study the following example:
LaSomme = tabValeur.Agrège(Additionne, 0)
PROCEDURE INTERNE Additionne(Valeur, Total)
RENVOYER Valeur + Total
In this example, the code:
LaSomme = tabValeur.Agrège((Valeur, LaSomme) => { RENVOYER Valeur + LaSomme }, 0)
is equivalent to the following code:
tabValeur est un tableau d'entiers = [1,2,3,4,5]
LaSomme est un entier
LaSomme = tabValeur.Agrège((Valeur, LaSomme) => { RENVOYER Valeur + LaSomme }, 0)
With this internal procedure, the calculation can be broken down as follows:
  • IntermediateResult1 = Adds (t[1], InitialValue): in the example Adds( 1, 0 ) -> 1
  • IntermediateResult2 = Adds (t[2], IntermediateResult1): in the example Adds( 2, 1 ) -> 3
  • ...
Note: this example is for illustrative purposes; to find the sum of elements of an array, it is preferable to use <Array>.Sum.

Existing computations

The most common computations are available in WLanguage:
<Array>.MeanCalculates the mean of several elements:
  • elements found in an array,
  • numeric values, ...
<Array>.SumCalculates the sum of the array elements.

Sequence of functions
You can use array functions in a sequence.
The following functions can be used in a sequence: This sequence can be used as a source for a FOR ALL statement or it can end with one of the following functions:
gnMean = garrUser.Filter(cbFilter).Map(cbMap).Mean()
Related Examples:
Filter / Map / Reduce Unit examples (WINDEV): Filter / Map / Reduce
[ + ] This example shows how to use the Filter / Map / Reduce functions.
The Filter / Map / Reduce concept allows performing operations on sets of data in a remarkably concise way.
Filter is used to filter an array of elements in a customized way (with a callback procedure).
Map is used to transform an array of elements, also with a callback procedure.
Reduce is used to aggregate data in a customized way with a callback procedure.
Here, this example is used to calculate the average age of women among the users displayed in the table below.
Business / UI classification: Neutral code
Component: wd280vm.dll
Minimum version required
  • Version 25
This page is also available for…
Click [Add] to post a comment