currying只是一种避免继承的方法吗?

Ale*_*Mcp 9 language-agnostic currying

所以我对currying的理解(基于SO问题)是它允许你部分设置函数的参数并返回一个"截断"函数作为结果.

如果你有一个很大的毛发功能需要10个参数,看起来像

function (location, type, gender, jumpShot%, SSN, vegetarian, salary) {
    //weird stuff
}
Run Code Online (Sandbox Code Playgroud)

并且你想要一个"子集"函数,它可以让你处理除了之外的所有预设jumpShot%,你不应该打破一个继承原始函数的类吗?

我想我正在寻找的是这种模式的用例.谢谢!

Wol*_*lph 5

Currying有很多用途.从简单地指定您经常使用的函数的默认参数到返回用于特定目的的专用函数.

但是,让我举个例子:

function log_message(log_level, message){}
log_error = curry(log_message, ERROR)
log_warning = curry(log_message, WARNING)

log_message(WARNING, 'This would be a warning')
log_warning('This would also be a warning')
Run Code Online (Sandbox Code Playgroud)


Mit*_*sey 1

在javascript中,我对回调函数进行柯里化(因为它们在被调用后无法传递任何参数(来自调用者)

所以像这样:

...
var test = "something specifically set in this function";
onSuccess: this.returnCallback.curry(test).bind(this),

// This will fail (because this would pass the var and possibly change it should the function be run elsewhere
onSuccess: this.returnCallback.bind(this,test),
...

// this has 2 params, but in the ajax callback, only the 'ajaxResponse' is passed, so I have to use curry
returnCallback: function(thePassedVar, ajaxResponse){
   // now in here i can have 'thePassedVar', if 
}
Run Code Online (Sandbox Code Playgroud)

我不确定这是否足够详细或连贯......但是柯里化基本上可以让你“预填充”参数并返回一个已经填充了数据的裸函数调用(而不是要求你在其他点填充该信息)