Skip to main content
gersond0001
New Participant
Feb 4, 2022 5:28 AM UTC
Solved

What is the "context" parameter in reduce()

Can someone explain with an example the reduce( function, initial, list, context ) function's "context " parameter. I didn't find an example for it in the docs.

harshitb6843
Brainy
Feb 4, 2022 5:48 AM UTC

Hi there,

I tried implementing various cases of it below for my understanding. 

If we examine the pattern below, it looks like the context is being passed in every evaluation as an extra value along with the initial and respective item from the list. 
Ex. In the expression on line 4, you can see that the result has been increased by 3. This means that the value in 'context' was passed in the function along with the other 2 values, every time the reduce did an iteration. You can find a similar pattern in other expressions written in the above image. 

2 replies

harshitb6843
Brainy
Feb 4, 2022 5:48 AM UTC

Hi there,

I tried implementing various cases of it below for my understanding. 

If we examine the pattern below, it looks like the context is being passed in every evaluation as an extra value along with the initial and respective item from the list. 
Ex. In the expression on line 4, you can see that the result has been increased by 3. This means that the value in 'context' was passed in the function along with the other 2 values, every time the reduce did an iteration. You can find a similar pattern in other expressions written in the above image. 

stefanhelzle0001
Brainy
Feb 4, 2022 6:44 AM UTC

The documentation has to say: "Variable number of parameters passed directly into each function evaluation." I think this is pretty clear.