0

I need to add the function n number of arguments. Example : add(3)(8)(6)(10).

if it is only 2 arguments we can add the code like this. add(4)(5)

function add(x){
    return function(y){
       return x+y;
    }
}
add(4)(5)

if it is n number of arguments of how can we do this?

4
  • 3
    You want add(3)(8)(6)(10) === 27, for any number of terms? That’s impossible. You can have something that acts like a number, but it won’t really be one (and that would be a terrible API). Commented Apr 3, 2018 at 3:46
  • Do you know n a priori? Commented Apr 3, 2018 at 3:47
  • stackoverflow.com/questions/39187590/… Commented Apr 3, 2018 at 4:17
  • Or even Variadic curried sum function. Commented Apr 3, 2018 at 4:19

3 Answers 3

3

The closer I can get to what you asked is this;

function add(x){
    var next = function(y){
       return add(x + y)
    }

    next.result = x
    return next
}

console.log(add(4)(5).result)
console.log(add(4)(5)(1)(5).result)
console.log(add(3).result)

Here is a slightly different approach using objects, and IMO it is a lot more readable then add(1)(2)(3) since it is clear what operation you are performing in the subsequent steps. Also, this approach allows for extending with more operations, like minus for example.

class Add {
    constructor (value) {
        this.value = value
    }

    add (anotherValue) {
        return new Add(this.value + anotherValue)
    }

    result () {
        return this.value
    }
}

function add (value) {
    return new Add(value)
}

var result = add(3).add(5).add(10).result()
console.log(result) // 18

Sign up to request clarification or add additional context in comments.

5 Comments

The asker wants partial applications of functions, not method chaining.
Yeah, but what he wants is impossible (see Ryan comment). This is a much better way to achieve the same result, IMHO.
IMHO don't start the chain with add(). Probably doing something like calculator.startWith(3).add(5) is more Readable and Understandable right off the bat. That being said, this is still pretty cool.
Great suggestion, @ErikPhilips except for those uppercase letters haha :)
I'm primarily program in C#, I do it without thinking ;)
0

If n is not known beforehand, I can say categorically it's not possible. If n is fixed, you can do something like (say, n is 4):

const add = first => second => third => fourth => first + second + third + fourth.

If you want to let n be flexible, your best bet is this

const add = (...additives) => {
  let result = 0;
  for (const additive of additives) {
    result += additive;
  }
  return result;
}

Comments

0

As Ryan suggested in his comment : add(3)(8)(6)(10) is impossible.

Try this work around :

function add() {
  var sum = 0;
  for (var i = 0; i < arguments.length; i++) {
    sum = sum+arguments[i];
  }
  return sum;
}

var res = add(3, 8, 6, 10);
console.log(res);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.