Variable Length Curring


// Given an array of numbers, if the index is even, add.
// If the index is odd, subtract.
const addSubtractReducer = (total, current, index) =>
  (index % 2) === 0 ?
    total + current :
    total - current;
const addSubtract = x => {
  const nums = [ ];
  // Recursive function that accumulates numbers for the operation.
  const f = y => {
    nums.push(y);
    return f;
  };
  // When the recursive function is type cast to a number,
  // reduce the accumulated numbers.
  f.valueOf = () => {
    return nums.reduce(addSubtractReducer, x);
  };
  // Return the recursive function, having added the first digit.
  return f;
};