Home > Mobile >  Unable to inject parameters properly?
Unable to inject parameters properly?

Time:09-28

const curryByBind = (fn) => fn.length === 0 ? fn() : (arg) => fn.bind(null, arg);
const sum = (x,y) => x y;
const sumMany = curryByBind(sum);

console.log(sumMany(1)(2)())

I'm trying to learn curry and attempting to inject arguments into function via bind, I was thinking sumMany(1) should return a function that can take (2) which eventually at last execute fn(), but I cant figure out which part gone wrong, it's saying sumMany(...)(...) is not a function?

CodePudding user response:

Your curryByBind is working fine: the only problem is that you ran out of arguments.

sum(x, y) takes two arguments. After calling sumMany with one argument (sum's x), it returns a function expecting a single argument (sum's y).

After you call that function with one argument, you have supplied values for all of sum's arguments, so it returns 3. (Which is not a function, hence the error when you try calling it.)

console.log(sumMany(1)(2)) logs 3, as expected.

  • Related