MuleSoft Technical Guides

Reduce Function in DataWeave

User MuleSoft Integration Team
Calendar November 30, 2021

Reduce is a useful operator that applies a reduction expression on an array. This function can operate on each of the elements in an array.

Syntax:

Array<T> reduce( (item: T, accumulator: T) -> T): T | Null

OR

reduce(Array<T>, (item: T, accumulator: T) -> T): T | Null

Here,

Array

is an array on which we want to apply reduce function.

Item

In the input array, this is an item.

Refer to it as $.

If acc is defined, it takes the first array item value.

If acc isn’t defined, it uses the second array item value.

Accumulator (acc)

Reduce function saves the results after each iteration.

Refer to it as $$.

The initial value can be written as  [0, 1, 2, 3 ,4, 5] reduce ((item, acc = 2) → acc * item).

Refer to it as $$.

If no starting value is specified, then in this case the value of the first item of array will be used.

Iteration

Number of item in array minus 1 in case acc is not initialized.

Number of item in array in case acc is initialized.

Syntax Example:
Input :

%dw 2.0
output application/json
---
{
    "method1": [0, 1, 2, 3, 4, 5] reduce (item, acc) -> item + acc,
    "method2": reduce ([0, 1, 2, 3, 4, 5], (item, acc) -> item + acc)
}

Output:

{
  "method1": 15,
  "method2": 15
}

 

Let’s see the use case when

  • Accessing array items through ‘$’ and ‘$$’: In this we’ll be using the reduce function with the ‘$’ and ‘$$’.

Where,

‘$’ represents the current value of the iteration.

‘$$’ represents the aggregation of the result which could be anything i.e., a Number, an Object, an Array etc.

Input:

%dw 2.0
output application/json

var myArr = [0, 1, 2, 3, 4, 5]
---
{
    "Addition": myArr reduce $ + $$,
    "Subtraction": myArr reduce $ - $$,
    "Multiplication": myArr reduce $ * $$,
}

Output:-

{
  "Addition": 15,
  "Subtraction": 3,
  "Multiplication": 0
}

 

  • Accessing array items through ‘item’ and ‘accumulator’:

Where,

‘item’ represents the current value of the iteration.

‘accumulator’ represents the aggregation of the result which could be anything i.e., a Number, an Object, an Array etc.

Input:

%dw 2.0
output application/json

var myArr = [0, 1, 2, 3, 4, 5]
---
{
    "Addition": myArr reduce (item, acc) -> item + acc,
    "Subtraction": myArr reduce (item, acc) -> item - acc,
    "Multiplication": myArr reduce (item, acc) -> item * acc,
}

Output:

{
  "Addition": 15,
  "Subtraction": 3,
  "Multiplication": 0
}

 

  • Accumulator (acc) value is not initialized: Here we did not assign any value to the accumulator(acc) so it will take the 0th index value of an array and item will equal to the 1st index of an array.

For example: In the below mention code, for first iteration the acc = myArr[0], and an item = myArr[1]. i.e., acc = 1 and item = 2.

Input:

%dw 2.0
output application/json

var myArr = [1, 2, 3, 4, 5]
---
{
    "Addition": myArr reduce (item, acc) -> acc + item,
    "Subtraction": myArr reduce (item, acc) -> acc - item,
    "Multiplication": myArr reduce (item, acc) -> acc * item,
    "Divide": myArr reduce (item, acc) -> acc / item
}

Output :

{
  "Addition": 15,
  "Subtraction": -13,
  "Multiplication": 120,
  "Divide": 0.008333333333333333333333333333333336
}

 

  • Accumulator(acc) value is initialized: Here we assign the value of accumulator with 2 i.e., now the acc = 2 and the item will be equal to the 0th index of an array.
    For example: In the below mention code, for first iteration an item = myArr[0]. i.e., acc = 2 and item = 1.

Input:

%dw 2.0
output application/json

var myArr = [1, 2, 3, 4, 5]
---
{
    "Addition": myArr reduce (item, acc=2) -> acc + item,
    "Subtraction": myArr reduce (item, acc=2) -> acc - item,
    "Multiplication": myArr reduce (item, acc=2) -> acc * item,
    "Divide": myArr reduce (item, acc=2) -> acc / item
}

Output:

{
  "Addition": 17,
  "Subtraction": -13,
  "Multiplication": 240,
  "Divide": 0.01666666666666666666666666666666666
}

 

  • + and ++ in reduce function:

Input:

%dw 2.0
output application/json

var myArr = [0, 1, 2, 3, 4, 5]
---
{
    "sum": myArr reduce ($$ + $),
    "concat": myArr reduce ($$ ++ $)
}

Output:

{
  "sum": 15,
  "concat": "012345"
}

 

  • When the array’s items are in bollean form: Here all the items of an array is in Bollean form.

Input:

%dw 2.0
output application/json

var myBolleanArr = [true, true, false, true, true]
---
{
    "AND operator": myBolleanArr reduce ($$ and $),
    "OR operator": myBolleanArr reduce ($$ or $)
}

Output:

{
  "AND operator": false,
  "OR operator": true
}

 

  • To fetch first and last item from an array: We can fetch the first item of an element by returning the ‘$’ and last item by return the ‘$$’.

Input:

%dw 2.0
output application/json

var myBolleanArr = [true, true, false, true, false]
---
{
    "last_item_of_array": myBolleanArr reduce $,
    "first_item_of_array": myBolleanArr reduce $$,
}

Output:

{
  "last_item_of_array": false,
  "first_item_of_array": true
}

 

  • Fold Left and Fold Right in Reduce function

Fold Left: In this case, we mention the accumulator to the left side of an item.

Input:

%dw 2.0
output application/json

var myArr = [1, 2, 3, 4, 5]
---
{
    "Addition": myArr reduce (item, acc) -> acc + item,
    "Subtraction": myArr reduce (item, acc) -> acc - item,
    "Multiplication": myArr reduce (item, acc) -> acc * item,
    "Divide" : myArr reduce (item, acc) -> acc / item
}

Output:

{
  "Addition": 15,
  "Subtraction": -13,
  "Multiplication": 120,
  "Divide": 0.008333333333333333333333333333333336
}

 

Fold Right: In this case, we mention the accumulator to the right side of an item.

Input:

%dw 2.0
output application/json

var myArr = [1, 2, 3, 4, 5]
---
{
    "Addition": myArr reduce (item, acc) -> item + acc,
    "Subtraction": myArr reduce (item, acc) -> item - acc,
    "Multiplication": myArr reduce (item, acc) -> item * acc,
    "Divide" : myArr reduce (item, acc) -> item / acc
}

Output:

{
  "Addition": 15,
  "Subtraction": 3,
  "Multiplication": 120,
  "Divide": 1.875000000000000000000000000000000
}

Here the result for ‘Addition’ and ‘Multiplication’ will always be same for both the cases but vary for the ‘Subtraction’ and ‘Divide’.

Thanks for reading and please add your comments in the section below. Find more Mule best practices by Caelius Consulting at Resource Centre.

Leave a comment

Your email address will not be published. Required fields are marked *