How to execute a function by an unknown number of parameters

Say I have a function called multiplyDivide

If I were to call multiplyDivide(2)(3)(4)(6), it would be equivalent 2 * 3 / 4 * 6.

Update: Is it possible to write such a function if I do not know in advance how many parameters I will accept? For example, I could have multiplyDivide(1)(2)ormultiplyDivide(1)(2)(3)(4)...(n-1)(n)

+4
source share
4 answers

Why should it be curry? Wouldn’t it be much simpler to simply “overload” the function by providing arguments n?

function multiplyDivide() {
  var result = arguments[0];
  for (var i = 1; i < arguments.length; i++) {
    result = (i % 2) ? (result * arguments[i]) : (result / arguments[i]);
  }
  return result;
}

console.log(multiplyDivide(2, 3, 4, 6)); //9
console.log(multiplyDivide(5, 4, 2, 3, 3, 5, 1)); //50
Run codeHide result
+3
source

, , , . , .

. - , :

// Using add instead of multiplyDivide to simplify example:

function add (num) {
    function adder (n) {
        if (n !== undefined) {
            num += n;
            return adder;
        }
        else { // terminate
            return num;
        }
    }
    return adder;
}

:

var sum = add(1)(2)(3)(4)();

, :

var x = add(1)(2)(3)(4);
x = x(5)(6)(7);
x = x(8)(9)(10);

var sum = x();

js- , getter . , "API" :

function add (num) {
    function adder (n) {
        num += n;
        return adder;
    }
    adder.value = function(){
        return num
    };
    return adder;
}

:

var sum = add(1)(2)(3)(4).value();

, .valueOf() .toString():

function add (num) {
    function adder (n) {
        num += n;
        return adder;
    }
    adder.valueOf = function(){
        return num
    };
    adder.toString = function(){
        return '' + num
    };
    return adder;
}

:

var sum = add(1)(2)(3)(4) + 5; // results in 15
var txt = add(1)(2)(3)(4) + "hello"; // results in "10hello"

, .

+2

- ?

var multiplyDivide = function(first){
  return function(second){
    return function(third){
      return function(forth){
        return first * second / third * forth;
      }
    }
  }
}
//should be 6
console.log(multiplyDivide(2)(4)(4)(3));
0

...

Currying - , . JS, , .

, , , .

,

function multiplyDivide (n,m,o,p){
  return n * m / o * p;
}

curry :

function curry(fn){
  return fn.length > 1 ? function(...a){
                           return a.length >= fn.length ? fn(...a)
                                                        : curry(fn.bind(fn,...a));
                         }
                       : fn;
}

,

function multiplyDivide (n,m,o,p){
      return n * m / o * p;
    }

function curry(fn){
  return fn.length > 1 ? function(...a){
                           return a.length >= fn.length ? fn(...a)
                                                        : curry(fn.bind(fn,...a));
                         }
                       : fn;
}

var f = curry(multiplyDivide),
 res1 = f(4,5,2,10),
 res2 = f(4)(5,2,10),
 res3 = f(4)(5)(2,10),
 res4 = f(4)(5)(2)(10),
 res5 = f(4,5)(2,10),
 res6 = f(4,5)(2)(10),
 res7 = f(4,5,2)(10),
 res8 = f(4,5)(2,10);
console.log(res1,res2,res3,res4,res5,res6,res7,res8);
Hide result

There may be simpler ways, but this is what I could come up with.

0
source

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


All Articles