We have a functions "module" that contains a number of functions. We want to add a sumCubes function. Start by looking at the code for functions.sum and functions.sumSquares. Go take a look at functions.sumSquares(). The code for sumCubes will be very similar but instead of summing the raw values (temp += v
), or the square of the values (temp += v * v
), this will sum the cube (temp += v * v * v
). Note: temp += v
is short hand for temp = temp + v
.
Lumenize.functions are designed to incrementally calculate the results from prior results. Did you notice how that works? The trick is to understand that in CoffeeScript it is perfectly legal to call a function with less than the full list of parameters. So, if you call functions.sum([1, 2, 3, 4])
(only using the fist values
parameter), the answer will be 10. However, let's say you previously calculated the sum for [1, 2, 3] = 6 and you wanted to take advantage of that prior calculation to speed up the calculation of sum of