Loading article...
Here are two different implementations of a curried sum function, one using state within the function and the other using recursion.
function sum(a) { // Create a new function that maintains its own state function innerSum(b) { // Track the running total as a property of the function itself if (b === undefined) { return innerSum.total; } // Add the new value to our running total innerSum.total += b; // Return this function again to allow for chaining return innerSum; } // Initialize the total innerSum.total = a; // Add a valueOf method to make the function evaluate to the total when used in comparisons innerSum.valueOf = function() { return innerSum.total; }; // Add a toString method to make the function convert to the total when used as a string innerSum.toString = function() { return innerSum.total.toString(); }; return innerSum; } // Test cases const sum1 = sum(1); console.log(sum1(2) == 3); // true console.log(sum1(3) == 6); // true console.log(sum(1)(2)(3) == 6); // true console.log(sum(5)(-1)(2) == 6); // true
innerSum.total property is used to store the running total.innerSum) to allow for chaining additional calls.valueOf and toString : These methods are added to ensure that the function behaves like a number in comparisons and string conversions.function sum(a) { // Base case value const currentSum = a; // Return a new function that will handle the next value function nextSum(b) { // When called with a new value, create a new sum starting // with the current accumulated value plus the new value return sum(currentSum + b); } // Add valueOf to allow for comparison with primitives nextSum.valueOf = function() { return currentSum; }; // Add toString for string conversions nextSum.toString = function() { return currentSum.toString(); }; return nextSum; } // Test cases const sum1 = sum(1); console.log(sum1(2) == 3); // true console.log(sum1(3) == 4); // true console.log(sum(1)(2)(3) == 6); // true console.log(sum(5)(-1)(2) == 6); // true
sum(currentSum + b)) to accumulate the sum as more values are provided.valueOf and toString : These methods are used to convert the recursive function to a primitive value for comparisons and string conversions.innerSum.total) to track the running total.sum(currentSum + b).Both implementations allow you to chain function calls to accumulate the sum, with the key distinction being whether you manage state via a variable (total) or via recursive function calls.
Test your understanding with 3 quick questions