Does curry function in javascript uses principle of closure? [closed]

自古美人都是妖i 提交于 2019-11-30 12:55:15

问题


It would be very helpful, if someone explains the working of a curry function. I have read many examples, but not able to grasp it properly. Is it anyhow related to closure.


回答1:


Currying is just technique, that can make use of any language feature (e.g. closures) to achieve the desired result, but it is not defined what language feature has to be used. As of that currying does not require to make use of closures (but in most of the cases closures will be used)

Here a little example of the usage of currying, with and without the usage of closure.

With the use closure:

function addition(x,y) {
  if (typeof y === "undefined" ) {
    return function (y) {
      return x + y;
    }
  }
  return x + y;
}

var additionRemaining = addition(3); // Currying
additionRemaining(5);//add 5 to 3

With the use of new Function instead of closure (partial evaluation):

function addition(x,y) {
  if (typeof y === "undefined" ) {
    return new Function('y','return '+x+' + y;');
  }
  return x + y;
}

var additionRemaining = addition(3); // Currying
additionRemaining(5);//add 5 to 3


来源:https://stackoverflow.com/questions/28912097/does-curry-function-in-javascript-uses-principle-of-closure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!