1

I have the below code wherein, I have wrapped functions to achieve the above behaviour. But unfortunately it returns expected result only when the no of parameters equals to 2.

function baseCurry (func) {
    return function (...args) {
      if (args.length >= func.length) {
        return func.call(this, ...args)
      } else {
        return baseCurry(func.bind(this, ...args))
      }
    }
  }
  
  function addHelper (x, y) {
    return x + y;
  }
  
  const superCurry = baseCurry(addHelper);

Test Cases:

  console.log(superCurry(1, 5)); // 6
  console.log(superCurry(1)(5)); // 6
  console.log(superCurry(1)); // [Function (anonymous)]
  console.log(superCurry(1)(2)(3)); // Error
  console.log(superCurry(1,2)(3)); // Error

I need to change it in such a way that it gives expected result for all n >= 1, where 'n' is the number of parameters

Note:

The params can be passed in any combinations like

console.log(superCurry(1,2)(3)(4))
console.log(superCurry(1,2,3)(5,7)(4)(8,9))

Thanks in advance

Amaarockz
  • 4,348
  • 2
  • 9
  • 27
  • 2
    A function result cannot be a function and a number primitive at the same time. You'll need to rely on `valueOf`, but `console.log` will not make its argument a primitive on its own. – trincot Apr 12 '22 at 16:46
  • Iam ok with some minimal changes in the above code to make it work? – Amaarockz Apr 12 '22 at 16:47
  • is it possible? – Amaarockz Apr 12 '22 at 16:48
  • 1
    Your function addHelper only takes 2 arguments, so it's not clear how you expect it to work with 3 or more. Sure you probably "want" it to add up an arbitrary number of arguments, but it's not going to just do that on its own when it's defined to work on only 2 arguments. – Robin Zigmond Apr 12 '22 at 16:48
  • It is only possible if you change something in the `console.log` lines. – trincot Apr 12 '22 at 16:48
  • @RobinZigmond Exactly, but I dont know how to modify my helper for that – Amaarockz Apr 12 '22 at 16:49
  • So are you saying `n` is the number of arguments that the helper takes? Is that the definition of `n`? This is not clear from your driver code where you want it to work with that helper while providing 3 values. – trincot Apr 12 '22 at 16:51
  • The actual problem is that in order to make it support 'n' arguments, I dont know if I should get the arguments inside helper or baseCurry! – Amaarockz Apr 12 '22 at 16:52
  • As the code is written, it derives `n` from the number of arguments that the helper takes. So if you want it to work with a different value of `n` you need a different helper, **and** create a new `superCurry` function with it. – trincot Apr 12 '22 at 16:55
  • @trincot I realize, any idea on how to implement? – Amaarockz Apr 12 '22 at 16:56
  • I don't understand. You would define the helper to take `x, y, z`, and just do what you already did, but making sure you always pass three values, so `(1)(2)(3)` or `(1, 2)(3)`, ...etc. It is not intended to vary the number of values with the *same* helper. – trincot Apr 12 '22 at 17:00
  • instead of `x,y,z`, lets say I recieve it like `...arguments`, then how can I take this foward? – Amaarockz Apr 12 '22 at 17:02
  • 1
    well you can just use .reduce on the arguments array to add them all up. That won't work in curried form though. You're back to the problem @trincot mentioned at the start - that `superCurry(1, 2)` cannot be both the number 3 and a function that adds 3 to any subsequent arguments. – Robin Zigmond Apr 12 '22 at 17:03
  • 1
    @Amaarrockz "*is it possible?*" - No. – Bergi Apr 12 '22 at 18:13

1 Answers1

1

I could do something similar to your expectation, but I do need an extra pair of () at the end of the call chain so that the function will know when a function is to be returned and when the value.

function baseCurry (func, value) {
    this.value = value ?? 0;
    return (...args) => {
      if (args.length === 0) {
          let val = this.value;
          this.value = 0;
          return val;
      }
      for (let index = 0; index < args.length; index += func.length - 1) {
          this.value = func(this.value, ...args.slice(index, (index + func.length - 1 <= args.length) ? index + func.length - 1 : undefined));
      }
      return baseCurry(func, this.value);
    }
  }
  
  function addHelper (x, y) {
    return x + y;
  }
  
  const superCurry = baseCurry(addHelper);

  console.log(superCurry(1, 5)());
  console.log(superCurry(1)(5)());
  console.log(superCurry(1)());
  console.log(superCurry(1)(2)(3)());
  console.log(superCurry(1,2)(3)());
  console.log(superCurry(1,2,3)());
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175