How to curry a function across an unknown number of parameters

后端 未结 5 2113
灰色年华
灰色年华 2020-12-06 18:40

Say I have a function called multiplyDivide

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

5条回答
  •  天涯浪人
    2020-12-06 19:11

    Do you mean something like this?

    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));
    

提交回复
热议问题