Home > database >  How to implement infinite currying sum values with a callback function and result use TypeScript?
How to implement infinite currying sum values with a callback function and result use TypeScript?

Time:10-27

How to declare a currying sum function type in typescript? I need a sum function , it can sum numbers by currying like this

console.log(sum(100, 200)(300)()); // 600
console.log(sum(100, 200)()); // 300
console.log(sum()); // 0

I can implementation with javascript but I don't konw how declare type in typescript

const sum =  function sum(...allNumbers: any[]):any {
   if (allNumbers.length === 0) return 0;
   const sumValue = (numbers: number[]) =>
      numbers.reduce((sum, num) => (sum  = num), 0);
   return function memoFunc(...nums: any[]): any {
      allNumbers = allNumbers.concat(nums);
      if (nums.length === 0) {
         return sumValue(allNumbers);
      }
      return memoFunc;
   };
}

console.log(sum(100, 200)(300)()); // 600
console.log(sum(100, 200)()); // 300
console.log(sum()); // 0

This is my code, I dont kown how to declare the type , so I use any to replace. How can I delare it?

CodePudding user response:

// It's easier to use a `type` as it's recursive
type ChainedAdd = ((...numbers: number[]) => ChainedAdd) & (() => number)

function chainedAdd(base: number): ChainedAdd {
    // the below definition is overloading definition of ChainedAdd
    function add(): number;
    function add(...numbers: number[]): ChainedAdd;
    function add(...numbers: number[]) {
        if (numbers.length > 0) {
            // this is sum reducer, replace with whatever you like
            let newBase = numbers.reduce((v, e) => v   e, base);
            return chainedAdd(newBase);
        } else {
            return base;
        }
    }
    return add
}

const sum = chainedAdd(0);

console.log(sum(100, 200)(300)()); // 600
console.log(sum(100, 200)()); // 300
console.log(sum()); // 0

CodePudding user response:

A possibility would be

function sum(): number;
function sum( ...nums: number[]): typeof sum;

function sum(...allNumbers: any): number | typeof sum{
   if (allNumbers.length === 0) return 0;
   const sumValue = (numbers: number[]): number => numbers.reduce((sum, num) => (sum  = num), 0);
   const memoFunc = function(...nums: number[]): number | typeof sum{
      allNumbers = allNumbers.concat(nums);
      if (nums.length === 0) {
         return sumValue(allNumbers);
      }
      return memoFunc as typeof sum;
   };
   return memoFunc as typeof sum;
}

//console.log(sum()(300)); // compiler error
//const x: number = sum(200, 100); // compiler error
console.log(sum(100, 200)(300)()); // 600
console.log(sum(100)(200)(300)()); // 600
console.log(sum(100, 200)()); // 300
console.log(sum()); // 0

playground

I see that it's pretty close in underling conception to previous answer by Dimava -- I'd leave it on for now just because it follows closely the original version - might be useful to the OP.

  • Related