Initializing reduce
Tasks such as adding integers or finding maximum values have a common thread: the input values and the accumulated values are of the same type. When two numbers are added, the result is a number; when a maximum or a minimum is chosen between two numbers, the result is still a number. When we use reduce
to add numbers together, the running total is a number just like all the other inputs. In the examples so far, the first function call that reduce
makes takes the first two items in the sequence. We can break a reduce
call into its successive function calls:
(reduce + [1 2 3 5 7]) (+ 1 2) (+ 3 3) (+ 6 5) (+ 11 7)
We actually don't need the anonymous function that we used in the previous examples, because +
takes numbers as arguments, and returns a number:
In each of our examples so far, three different things are all of the same type:
- The values in the...