Reduce Function

 

Reduce function is used to do any computation while iterating on an array, computation result is not lost while iteration. it’s a very helpful function where you have to do computation on some array element.

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

or

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

Here

  • Array – is the array on which we want to apply reduce function
  • Item
    • item in the input array
    • Refer as $
    • Takes 1st array item value in case acc is defined
    • Takes 2nd array item value in case acc is not defined
  • Accumulator (acc)
    • Store the results of lambda expression after each iteration in reduce function
    • Refer as $$
    • We can explicitly define the initial value like [2,3,3] reduce ((item, acc = “2”) → acc * item)
    • If initial value is not defined then it will take the value of first item of array.
  • Iteration
    • no of item in array minus 1 in case acc is not initialized
    • no of item in array in case acc is initialized

Lets see the use case where

acc value is not initialized

Dataweave:

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

Here

  • [2,3] – is the input array
  • acc -> $$ will take the 1st item value = 2 (as acc is not initialized)
  • $ will take the 2nd item value = 3 (as acc is not initialized)
  • Loop count = no of item in array minus 1 (as acc is not initialized) = 2 – 1 = 1
    • Acc = ($=3 + $$=2) = 5

So result will be 5

acc value is initialized

Dataweave:

%dw 2.0
output application/json
---
[2,3] reduce ((item, acc = 4) -> acc + item)

Here

  • [2,3] – is the input array
  • acc will take the initialized value = 4
  • item will take 1st item value = 2 (as acc is initialized)
  • Loop count = no of item in array (as acc is initialized) = 2
    • Acc = acc + item -> 4 + 2 -> 6
    • Acc = acc + item -> 6 + 3 -> 9

So result will be 9

more details

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Rizwan
Rizwan
4 years ago

Hello Admin,
while concatenating using reduce function getting the output as “54321”
—-
if acc value is not initialized
acc -> $$ will take the 1st item value 
$ will take the 2nd item value

[1,2,3,4,5] reduce ((item,acc) -> item ++ acc)
suzi
suzi
3 years ago

I/P

a = [1,2,3,4,5,6]
b = [3,4,5,6]

i want to print duplicates using reduce function

O/P

C = [3,4,5,6]

Niranjan Koduru
Niranjan Koduru
2 years ago
Reply to  suzi

[(a ++ b reduce ((item, acc) -> acc ++ item) splitBy “”) distinctBy ($) joinBy “”]

Last edited 2 years ago by Niranjan Koduru
Saravanan Devadass
Saravanan Devadass
1 year ago

Answer is incorrect. It shows like [
“123456”
]

Saravanan Devadass
Saravanan Devadass
1 year ago

The answer is wrong. Please provide the screenshot if you feel it’s correct.

Anshul
Anshul
1 year ago

The correct answer is:

Dataweave:
%dw 2.0
output application/json
var a = [1,2,3,5,6,7]
var b = [2,4,6,7]

a filter (b contains $)

Output: [2,6,7]

Last edited 1 year ago by Anshul
Anshul
Anshul
1 year ago
Reply to  suzi

The correct answer to this is: %dw 2.0 output application/json var a = [1,2,3,4,5,6,1,7] var b = [3,4,5,7,8] var commonData = b map ((item, index) -> if((a contains item) == true) item else “”) filter $ != “” var record = a ++ b var distinctVal = record distinctBy $ — { CombinedArray : record, distincValues : distinctVal, duplicatesAre : commonData } Output will be: { “CombinedArray”: [ 1, 2, 3, 4, 5, 6, 1, 7, 3, 4, 5, 7, 8 ], “distincValues”: [ 1, 2, 3, 4, 5, 6, 7, 8 ], “duplicatesAre”: [ 3, 4, 5, 7 ]… Read more »

Saravanan Devadass
Saravanan Devadass
1 year ago

%dw 2.0
output application/json
var arr1 = [1,2,3,4,5]
var arr2 = [3,4,5,6]

fun getDupData(data, findData) = if(sizeOf(data map $ == findData filter $)>1) findData else null

fun getDuplicateData(arr1, arr2) = do {
var allData = arr1 ++ arr2

allData distinctBy $ map ((item, index) -> {
DuplicateData: getDupData(allData, item),
}) filter $.DuplicateData != null
}

{
dupValues1: getDuplicateData(arr1, arr2).DuplicateData,
}