Skip to main content
Skip table of contents

reduce Statement

Syntax

reduce anArray using <expression with element and nextElement> if single use <expression with element>

Semantics

reduce allows you to reduce an array having elements of base or complex types to a scalar value by recursively applying an expression to each array element and its next element.
if single use is a mandatory extension and allows to define the action for an array containing only one single element.

If the array exists, but has no array elements, an exception is raised: "The first item of Reduce is NULL".

Substitutables

anArray

Can be any variable or object attribute having the type Array

<expression with element and next element>

This expression must use the currently evaluated array element and its next neighbor and evaluates to a scalar having the same type as the result value.

<expression with element>

The expression after if single use uses the sole array element and evaluates to a scalar having the same type as the result value.

Examples

Concatenate all strings to a list, separated by space

NONE
set value = reduce myList using concat(element, " ", nextElement)
            if single use element;

Reduce complex types and calculate a number of characters in a String

NONE
set outLength = reduce inArray 
                using element.stringAttr.stringLength()
                      + nextElement.stringAttr.stringLength()
                if single use element.stringAttr.stringLength();

Calculate a sum through all array elements

NONE
set sum = reduce myNumbers using element + nextElement
          if single use element;

Concatenate blobs

NONE
set reducedBlob = reduce blobs using concat(element, nextElement)
                  if single use element;

Usage of if single

NONE
set reduceString = reduce stringArray
                   using concat("many elements: ", element, " ", nextElement)
                   if single use concat("only one element: ", element);

Reducing Arrays with Elements of Base Type

For example, in an array of strings, you can reduce the list to a simple string consisting of a concatenation of all single elements.

Suppose you have an array of strings and you wish to copy all of them to a single string, separated by a space. To do this, use operation reduce in combination with concat(element, " ", nextElement). Note that the second parameter of the concat() operation is the string that should be used as separator between any given array element and its neighbor. In this case, this is a space string literal.

CODE
set value = reduce myList using concat(element, " ", nextElement)
            if single use element;

element and nextElement are keywords and allow you to use relative references (as opposed to normal, absolute indexes) to single elements of an array (see Get Array Element Operator []).

Based on the type of the array elements, you can use different operations. For example, elements of base type Integer can be computed (addition, subtraction, division, or multiplication).

CODE
set sum = reduce myNumbers using element + nextElement
          if single use element;

Reducing Arrays with Elements of Complex Type

The same algorithm applies to arrays with elements of complex types as described in the previous chapter. Since each element or nextElement refers to an object of complex type, you can access the attributes of the object like any other instance (e.g. element.name).
In the example below, array inArray contains objects of complex type ReduceStructure. Now, you wish to copy attribute strAttr of each array element to a single string separated by a semicolon. To do this, use the reduce operation in combination with concat(element, ";", nextElement).

CODE
set outString = reduce inArray using concat(element.stringAttr, ";", nextElement.stringAttr)
                if single use element.stringAttr; 

Single Array Elements

For arrays that contain only one single element, you can use the (mandatory) extension if single use in the reduce statement.

CODE
set reduceString = reduce stringArray using concat(element, " ", nextElement)
                   if single use concat("The array has only one element: ", element);

If an array has only one single element, the statement following if single use will be evaluated. In the example above, element refers to the only string in the array. If the string was Hello World! , reduceString will end up with the content "The array has only one element: Hello World!".

Reduce Algorithm

The reduce algorithm works as follows. Suppose you want to reduce an array myNumbers containing the numbers 1, 2, and 3.

  1. In the first iteration, the first occurrence of the element expression is initialized (here: take the first integer 1). Then the next element is evaluated and added (+ nextElement). The result of the first iteration is 3.

  2. In the second iteration, element refers to the intermediate result of the first iteration. Then, nextElement is added again, resulting in the value 6. This value is finally assigned to integer sum.

JavaScript errors detected

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

If this problem persists, please contact our support.