reduce

DataWeave 2.2 is compatible and bundled with Mule 4.2. Standard Support for Mule 4.2 ended on May 2, 2021, and this version of Mule will reach its End of Life on May 2, 2023, when Extended Support ends.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

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

Applies a reduction expression to the elements in an array.

For each element of the input array, in order, reduce applies the reduction lambda expression (function), then replaces the accumulator with the new result. The lambda expression can use both the current input array element and the current accumulator value.

Note that if the array is empty and no default value is set on the accumulator parameter, a null value is returned.

Parameters

Name Description

item

Item in the input array. It provides the value to reduce. Can also be referenced as $.

acc

The accumulator. Can also be referenced as $$. Used to store the result of the lambda expression after each iteration of the reduce operation.

The accumulator parameter can be set to an initial value using the syntax acc = initValue. In this case, the lambda expression is called with the first element of the input array. Then the result is set as the new accumulator value.

If an initial value for the accumulator is not set, the accumulator is set to the first element of the input array. Then the lambda expression is called with the second element of the input array.

The initial value of the accumulator and the lambda expression dictate the type of result produced by the reduce function. If the accumulator is set to acc = {}, the result is usually of type Object. If the accumulator is set to acc = [], the result is usually of type Array. If the accumulator is set to acc = "", the result is usually a String.

Example

This example returns the sum of the numeric values in the first input array.

Source

%dw 2.0
output application/json
---
[2, 3] reduce ($ + $$)

Output

5

Example

This example adds the numbers in the sum example, concatenates the same numbers in concat, and shows that an empty array [] (defined in myEmptyList) returns null in emptyList.

Source

%dw 2.0
var myNums = [1,2,3,4]
var myEmptyList = []
output application/json
---
{
   "sum" : myNums reduce ($$ + $),
   "concat" : myNums reduce ($$ ++ $),
   "emptyList" : myEmptyList reduce ($$ ++ $)
}

Output

{ "sum": 10, "concat": "1234", "emptyList": null }

Example

This example sets the first element from the first input array to "z", and it adds 3 to the sum of the second input array. In multiply, it shows how to multiply each value in an array by the next ([2,3,3] reduce ((item, acc) → acc * item)) to produce a final result of 18 (= 2 * 3 * 3). The final example, multiplyAcc, sets the accumulator to 3 to multiply the result of acc * item (= 12) by 3 (that is, 3 (2 * 2 * 3) = 36), as shown in the output.

Source

%dw 2.0
output application/json
---
{
   "concat" : ["a", "b", "c", "d"] reduce ((item, acc = "z") -> acc ++ item),
   "sum": [0, 1, 2, 3, 4, 5] reduce ((item, acc = 3) -> acc + item),
   "multiply" : [2,3,3] reduce ((item, acc) -> acc * item),
   "multiplyAcc" : [2,2,3] reduce ((item, acc = 3) -> acc * item)
}

Output

{ "concat": "zabcd", "sum": 18, "multiply": 18, "multiplyAcc": 36 }

Example

This example shows a variety of uses of reduce, including its application to arrays of boolean values and objects.

Source

%dw 2.0
output application/json
var myVar =
{
  "a": [0, 1, 2, 3, 4, 5],
  "b": ["a", "b", "c", "d", "e"],
  "c": [{ "letter": "a" }, { "letter": "b" }, { "letter": "c" }],
  "d": [true, false, false, true, true]
}
---
{
  "a" : [0, 1, 2, 3, 4, 5] reduce $$,
  "b": ["a", "b", "c", "d", "e"] reduce $$,
  "c": [{ "letter": "a" }, { "letter": "b" }, { "letter": "c" }] reduce ((item, acc = "z") -> acc ++ item.letter),
  "d": [{ letter: "a" }, { letter: "b" }, { letter: "c" }] reduce $$,
  "e": [true, false, false, true, true] reduce ($$ and $),
  "f": [true, false, false, true, true] reduce ((item, acc) -> acc and item),
  "g": [true, false, false, true, true] reduce ((item, acc = false) -> acc and item),
  "h": [true, false, false, true, true] reduce $$,
  "i": myVar.a reduce ($$ + $),
  "j": myVar.a reduce ((item, acc) -> acc + item),
  "k": myVar.a reduce ((item, acc = 3) -> acc + item),
  "l": myVar.a reduce $$,
  "m": myVar.b reduce ($$ ++ $),
  "n": myVar.b reduce ((item, acc) -> acc ++ item),
  "o": myVar.b reduce ((item, acc = "z") -> acc ++ item),
  "p": myVar.b reduce $$,
  "q": myVar.c reduce ((item, acc = "z") -> acc ++ item.letter),
  "r": myVar.c reduce $$,
  "s": myVar.d reduce ($$ and $),
  "t": myVar.d reduce ((item, acc) -> acc and item),
  "u": myVar.d reduce ((item, acc = false) -> acc and item),
  "v": myVar.d reduce $$,
  "w": ([0, 1, 2, 3, 4] reduce ((item, acc = {}) -> acc ++ { a: item })) pluck $,
  "x": [] reduce $$,
  "y": [] reduce ((item,acc = 0) -> acc + item)
}

Output

"a": 0,
"b": "a",
"c": "zabc",
"d": { "letter": "a" },
"e": false,
"f": false,
"g": false,
"h": true,
"i": 15,
"j": 15,
"k": 18,
"l": 0,
"m": "abcde",
"n": "abcde",
"o": "zabcde",
"p": "a",
"q": "zabc",
"r": { "letter": "a" },
"s": false,
"t": false,
"u": false,
"v": true,
"w": [ 0,1,2,3,4 ],
"x": null,
"y": 0
}

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

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub