JS function with two brackets and two pairs

I am trying to understand how a function that runs with two brackets and two parameters works. For instance:

add(10)(10); // returns 20 

I know how to write one that takes two parameters like this:

 function add(a, b) { return a + b; } add(10,10); // returns 20 

How can I change this function so that it can be run with one set of parameters or two, and get the same result?

Any help is appreciated. Literally scratching his head over it.

Thanks in advance!

+6
source share
4 answers

How can I change this function so that it can be run with one set of parameters or two, and get the same result?

You can almost do it, but I'm trying my best to think of a good reason.

Here's how: you discover how many arguments your function received, and if it received only one, you return the function instead of the number - and add this function to the second number if it is called:

 function add(a,b) { if (arguments.length === 1) { return function(b2) { // You could call this arg `b` as well if you like, return a + b2; // it would shadow (hide, supercede) the one above }; } return a + b; } console.log(add(10, 10)); // 20 console.log(add(10)(10)); // 20 

I said โ€œalmostโ€ above, because only because the add function received only one argument, which does not guarantee that the caller will call the result. They could write:

 var x = add(10); 

... and never call the function that x now refers to.

+15
source

Welcome to the wonderful world of first-order functions.

In JavaScript, a function can return a function because the function is just another object. A simple implementation is something like:

 function add(x){ return function addOther(y){ return x + y; }; } 

This is possible due to closures and first-order functions.

It also allows you to run a partial application, libraries such as Ramda use it to a large extent.

 var addThree = add(3) addThree(5); // 8 
+8
source

To extend the fact that both TJ Crowder and Benjamin Gruenbaum , libraries such as Ramda (disclosure: I am one of the authors), you can convert a simple function as follows:

 function add(a, b) { return a + b; } 

in the style under discussion, wrapping it in a call to the curry function:

 var add = R.curry(function add(a, b) { return a + b; }); add(3, 5); //=> 8 add(3)(5); //=> 8 var add3 = add(3); add3(5); //=> 8 

The best article I know on this subject is Hugh Jackson Why Curry Helps . I wrote more information in Approve Curry.


Update

The curry version is a bit simpler than the Ramda version. He would do it higher and a little more, but he doesnโ€™t do some of the things Ramda does with placeholder values:

 // here is a function that takes a function and returns a curried version // of it, that is, a version that performs the sort of partial application // you describe. var curry = function(fn) { // first, we detect how many arguments the function has. var fnArity = fn.length; var partialApply = function(args) { // now, let create a function that curried return function () { // collect the previous args as the partial, and add the new // ones you just received var newArgs = (args || []).concat([].slice.call(arguments, 0)); // if we have "enough" arguments, we don't need any more partial // application and we can call the function. if (newArgs.length >= fnArity) { return fn.apply(this, newArgs); } else { // else we return a partially applied version return partialApply(newArgs); } }; }; return partialApply([]); // a function is itself partially applied with 0 args }; 
+4
source
 function add() { var sum = 0; for (var i = 0; i < arguments.length; i++) { sum += arguments[i]; } function total() { for (var i = 0; i < arguments.length; i++) { sum += arguments[i]; } return total; } total.toString = function () { return sum }; return total; } 

This will work for any arguments and parentheses.

https://medium.com/@imdebasispanda/super-function-with-closure-86a58a9a980b

0
source

Source: https://habr.com/ru/post/985771/


All Articles