Shape of Input/Output

Then, for better understanding of reduce let’s have a concept of input/output shape. We all know a function takes input(s) and generate output. In a pure function, there is no side effects so we can say, when we call the pure function, it’s only transforming the input to output.

For example, the following function takes two numbers and output one number. It looks like: [], number -> [] .

function sum(a: number[], b: number): number[] {

return [...a, b];

}

You can kind of visualizing it of putting one rock on top of a stack of rocks:

Now, with that shape concept in mind, we can categorize the usage of reduce into two case: