1

In a recent interview, I was asked to write a function that adds numbers and accepts parameters like this:

add(1)(2)(3) // result is 6
add(1,2)(3,4)(5) // result is 15

The number of parameters is not fixed, and the arguments can be either passed in sets or individually.

How can I implement this add function?

darKnight
  • 5,651
  • 13
  • 47
  • 87
  • 3
    So you want to implement currying a variadic function? I'd argue that's not possible, but there are many related questions on SO. E.g. https://stackoverflow.com/q/38638644/218196 – Felix Kling May 10 '18 at 18:31
  • I'm sure you did some attempt in your presumed interview, why didn't you make any here? Also it's very close to [this kata](https://www.codewars.com/kata/55ba24f1cb367c48ac0000a2) from codewars, you can look at solutions by opting out of the related kata. Also note that the description of your function is not proper - is the result returned after exactly three calls every time or what? – ASDFGerte May 10 '18 at 18:31
  • Possible duplicate of [Variadic curried sum function](https://stackoverflow.com/q/5832891/218196) (found via Google) – Felix Kling May 10 '18 at 18:33
  • @ASDFGerte: I am not sure what you mean by 'opting out of the related kata'. I don't see any solution mentioned there, just the option of taking that challenge! – darKnight May 10 '18 at 18:46
  • @FelixKling: It's slightly different from my question. The solutions there append a `+` before function invocation, or an empty `()` after the end of function invocation, neither of which match my case exactly. They also don't handle the case when multiple parameters are passed in one go. – darKnight May 10 '18 at 18:49
  • 4
    I know. What you want is simply not possible, if you really want to to curry a variadic function. Think about it: Currying means to return a function of lower arity if not all arguments are passed, eventually returning the result if all arguments have been passed. But since a variadic function accepts an unlimited number of arguments, you don't know when it is done. Hence the use of "hacks" in the other question. – Felix Kling May 10 '18 at 18:53

4 Answers4

2

Given your examples, the number of parameters is fixed in some ways.

As @ASDFGerte pointed out, your examples seem to return the result after three invocations. In this case a simple implementation without introducing terms like variadic and currying could be

function add(...args1){
  return function(...args2){
    return function(...args3){
      return args1.concat(args2).concat(args3).reduce((a,b)=>a+b)}}}
                
console.log(add(1)(2)(3))
console.log(add(1,2)(3,4)(5))

Every invocation accepts a variable number of parameters.

However it would be nice to generalize the construction of this nested functions structure and you can accomplish that with currying.

But if you want to allow an arbitrary number of invocations, when you should stop returning a new function and return the result? There is no way to know, and this is a simple, unaccurate and partial explanation to give you the idea of why they said you cannot accomplish what they asked you.

So the ultimate question is: is it possible that you misunderstood the question? Or maybe it was just a trick to test you

Edit

Another option would be to actually invoke the function when no arguments are passed in, change the call to add(1)(2)(3)()

Here an example recursive implementation

function sum (...args) {
  let s = args.reduce((a,b)=>a+b)

   return function (...x) {
     return x.length == 0 ? s : sum(s, ...x)
    };
}
console.log(sum(1,2)(2,3,4)(2)())

At every invocation computes the sum of current parameters and then return a new function that:

  • if is invoked without parameters just return the current sum
  • if other numbers are passed in, invokes recursively sum passing the actual sum and the new numbers
Francesco
  • 4,052
  • 2
  • 21
  • 29
  • We can check if the function argument is `undefined` and if it is, we stop returning a new function. So maybe a recursive function here which checks this condition should work? – darKnight May 11 '18 at 09:11
  • it could but instead of `add(1)(2)(3)` you should do `add(1)(2)(3)()` – Francesco May 11 '18 at 09:18
  • Ok. But I am not able to wrap my head around how this will work. Can you give a more detailed explanation? – darKnight May 11 '18 at 09:27
1

I'm a bit late to the party, but something like this would work (a bit hacky though in my opinion):

const add = (a, ...restA) => {
  const fn = (b, ...restB) => {
    return add([a, ...restA].reduce((x, y) => x + y) + [b, ...restB].reduce((x, y) => x + y))
  };
  fn.valueOf = () => {
    return [a, ...restA].reduce((x, y) => x + y)
  };
  return fn;
}

This function returns a function with a value of the sum. The tests below are outputing the coerced values instead of the actual functions.

console.log(+add(1,2)(3,4)(5)); // 15
console.log(+add(1)) // 1
console.log(+add(1)(2)) // 3
console.log(+add(1)(2)(3)) // 6
console.log(+add(1)(2)(3)(4)) // 10

Since it's a currying function, it will always return another function so you can do something like this:

const addTwo = add(2);
console.log(+addTwo(5)); // 7
Kam
  • 36
  • 2
0

using reduce and spread it can be done as below

 function calc(...args1){
    
    return function (...args2){
    
    return function (...args3){
    
    let merge = [...args1, ...args2, ...args3]
    
    return merge.reduce((x ,y)=> x + y)  ;
    
    }
    }
    } 

let sum = calc(10)(1)(4);

console.log("sum",sum);
Nischith
  • 166
  • 1
  • 8
0

They probably wanted to know how comfortable you were with "javascript internals", such as how and when methods like Function#toString and Function#valueOf, Function#[Symbol.toPrimitive] are called under the hood.

const add = (...numbers) => {  
  const cadd = (...args) => add(...args, ...numbers);
  
  cadd[Symbol.toPrimitive] = () => numbers.reduce((a, b) => a + b);
  
  return cadd;
}

console.log(
  `add(1,2)(3,4)(5) =>`, add(1,2)(3,4)(5),
); // result is 15

console.log(
  `add(1,2) =>`, add(1,2),
); // result is 3

console.log(
  `add(1,2)(5)(1,2)(5)(1,2)(5)(1,2)(5) =>`, add(1,2)(5)(1,2)(5)(1,2)(5)(1,2)(5),
); // result is 32
Hitmands
  • 13,491
  • 4
  • 34
  • 69