Skip to main content

reduce

Reduces a list of values to a single value by applying a reducer function to each value in turn.

The first parameter defines the input list, the second defines a starting value, and the third defines the reducer function. You can write the reducer function as a lambda expression or use a predefined accumulator function.

Parameters

  • LIST (list)

    The input list.

  • START (number)

    The starting value for the reduce operation.

  • FUNCTION (function)

    The reducer function. Two predefined accumulator functions — multiply() and sum() — can be passed directly into the parameter. Alternatively, write your own lambda expression.

Examples

These examples use a hard-coded list as the input to the first parameter. Normally, the input is a variable that returns a list or a like-like object such as a JSON array. This latter scenario is shown here.

Predefined accumulator functions

These examples use a predefined accumulator function in the third parameter:

ATL in Script

Result

Notes

[[reduce((1, 4, 2), 0, sum())]]

7

Reduce operation is 0 + (1 + 4 + 2).

[[reduce((1, 4, 2), 5, sum())]]

12

Reduce operation is 5 + (1 + 4 + 2).

[[reduce((1, 4, 2), 0, multiply())]]

0

Reduce operation is 0 x (1 x 4 x 2).

[[reduce((1, 4, 2), 5, multiply())]]

40

Reduce operation is 5 x (1 x 4 x 2).

Simple lambda expressions

You can perform the same operations using a lambda expression in the third parameter.

ATL in Script

Result

Notes

[[reduce((1, 4, 2), 0, (x,y) -> x + y)]]

7

Reduce operation is 0 + (1 + 4 + 2).

[[reduce((1, 4, 2), 5, (x,y) -> x + y)]]

12

Reduce operation is 5 + (1 + 4 + 2).

[[reduce((1, 4, 2), 0, (x,y) -> x * y)]]

0

Reduce operation is 0 x (1 x 4 x 2).

[[reduce((1, 4, 2), 5, (x,y) -> x * y)]]

40

Reduce operation is 5 x (1 x 4 x 2).

Tip

Your lambda expression must take two parameters — e.g. (x,y). The first parameter (represented by x) is the starting value, and the second parameter (represented by y) is the element from the list that's being reduced.

Complex lambda expression

Assume a "Describe a JSON Object" project with this data:

{
    "stores":[
        {
         "name": "New York",
         "brands": ["Adidas", "Converse", "Nike", "Reebok", "Spalding"]
        },
        {
         "name": "London",
         "brands": ["Adidas", "Mizuno", "Nike", "Puma", "Reebok"]
        },
        {
         "name": "Paris",
         "brands": ["Adidas", "Converse", "Lacoste", "Nike", "Salomon"]
        }
    ]
}

The "stores" array is a list-like object, so you can input it to the reduce function. We could reduce the array in many different ways, but here we'll reduce it to a list of brands that are sold in all three stores.

[[global.storesArray = WholeJSON.stores;'']]

[[global.commonBrands = storesArray[0].brands;'']]

[[reduce(storesArray, commonBrands, (x,y) -> filter(x, a -> contains(y.brands,a)))]] are sold in all stores. 
  • The first line defines the storesArray variable, which points to the "stores" array.

  • The second line defines the commonBrands variable, which returns a list of all brands sold in New York.

  • The third line includes a call to the reduce function.

The reduce function takes the input array (storesArray) and a starting list (commonBrands). It loops through each object in storesArray, applying the reducer function (a lambda expression) each time. The function takes two parameters: x is the starting list (commonBrands) and y is the array object that's under evaluation.

The lambda applies the filter function to the starting list so that a brand (a in the filter lambda) is retained when it's also in the list of brands for the object that's under evaluation (y.brands in the lambda).

To put it another way, it starts with a list of all brands sold in New York (commonBrands), loops through the data for the other stores, and filters out any starting-list brand not sold by the store currently being processed.

The output text is:

Adidas and Nike are sold in all stores.