Write a function
add which when called
add(1)(2)(3)...(n) should yield the same result.
Our objective is to create a single reusable function called
add which can take
n number of arguments or can return
n number of times a function which takes one (or list of )argument each time and finally solve the problem.
closure, currying etc.
Let us start by breaking the problem and trying to create a function that will add two numbers in different ways.
In the above code, as you can see, first we are checking if the second parameter exists. If present, then we just add the first and second parameters and return it. But if it is not present, we assume that it will be provided later by the user, so we return an
anonymous function. The returned function takes one parameter and then computes the result of the previous as well as the new parameter. (What the Hell ???)
Well if you’re new to the concept of
currying you might find the second way of calling
add(1)(2) a bit confusing. In the first approach
add with just one parameter and assign it to
addAgain variable. Now
addAgain is a function that takes one parameter. As it might surprise you, we are just passing one parameter and it is still returning us the result. This is because we have already passed the value of
addAgain(2) it is fairly obvious what is going to happen.
In the above example, we not just created
closure but also
curried the functionality.
Curryingis a process of reducing a function which takes multiple arguments into a function which takes only one argument at a time and returns another function which takes second argument, third argument and so on until all arguments are exhausted.
So by now you must have understood how easy it is to create a
currying function if we know the size of arguments beforehand. But in our case (the original problem), we need to write a function which takes an infinite number of arguments and adds them.
NOTE: the idea here is to return a function as well as a computed value each time, so that if it is called again, the returned function will handle it, and if its not called, the computed value is printed. easy peasy.
Let’s create a function that adds n number of parameters by using
arguments property of the function.
We are assigning the value of
args using the spread operator. You can find more about the spread operator here. In this case, it basically converts collection of arguments to an array. Next, we reduce the array and compute the total and return it. find more about the reduce method here.
The problem in the first case is solved. Now how do we solve the second case? By now we know that to solve the second case, we must return a function which again will return a function and so on until it’s not called anymore, in which case, the final value is computed. Let’s write a code that will return a function
n number of times which will in turn create a recursion.
Here, this function will work for any number of
curried arguments. But as you can check in the console, the final value is still not what we expected. It will always be a returned function and that's not what we want. So we do a little tweak here, in each loop:
- We have to return a function
addReturnthat will have reference to
- Before returning
addReturn, we compute the sum of the values in the argument list.
- We store the sum as a property of the
addReturncan be called as a normal function, which will in turn call
addfunction or we can print the value via its
Now let’s modify our code:
This is our final working code. Look carefully for what we have done here, we are creating a new member variable called
addReturn function and assigning it the computed total value.
Have a careful look at how we call the function after modification. We can either call the returned function by passing additional parameter and if we have nothing to call anymore, we print the value.
This way u can write a reusable function for any computation.
If you’re preparing for an interview, I suggest you have a look at key points to consider for cracking the interview.
Hope this helps you. Please feel free to comment any suggestions or questions.
If you find this article useful, you can show your appreciation by clicking on the clap button. As the saying goes, ‘When we give cheerfully and accept gratefully, everyone is blessed’.