难道仅仅是避免继承的一种方法吗? [英] Is currying just a way to avoid inheritance?
问题描述
因此,我对curring(基于SO问题)的理解是,它允许您部分设置函数的参数并作为结果返回截断的"函数.
So my understanding of currying (based on SO questions) is that it lets you partially set parameters of a function and return a "truncated" function as a result.
如果您的毛发函数很大,则需要10个参数,看起来像
If you have a big hairy function takes 10 parameters and looks like
function (location, type, gender, jumpShot%, SSN, vegetarian, salary) {
//weird stuff
}
,并且您想要一个子集"功能,该功能可以让您处理除jumpShot%
以外的所有其他内容的预设,您是否不应该打破从原始功能继承的类呢?
and you want a "subset" function that will let you deal with presets for all but the jumpShot%
, shouldn't you just break out a class that inherits from the original function?
我想我要寻找的是这种模式的用例.谢谢!
I suppose what I'm looking for is a use case for this pattern. Thanks!
推荐答案
在javascript中,我确实使用了回调函数(因为在调用它们(从调用者那里)后,它们无法传递任何参数
In javascript I do currying on callback functions (because they cannot be passed any parameters after they are called (from the caller)
所以像这样:
...
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
}
我不确定它是否足够详细或连贯...但是通过currying基本上可以让您预填充"参数并返回一个已经填充了数据的裸函数调用(而不是要求您在某些地方填充该信息)另一点)
I'm not sure if that was detailed or coherent enough... but currying basically lets you 'prefill' the parameters and return a bare function call that already has data filled (instead of requiring you to fill that info at some other point)
这篇关于难道仅仅是避免继承的一种方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!