柯里化只是避免继承的一种方法吗?

发布于 2024-08-30 04:19:15 字数 327 浏览 4 评论 0原文

所以我对柯里化的理解(基于SO问题)是它可以让你部分设置函数的参数并返回一个“截断的”函数作为结果。

如果您有一个需要 10 个参数的大函数,并且看起来像这样,

function (location, type, gender, jumpShot%, SSN, vegetarian, salary) {
    //weird stuff
}

并且您想要一个“子集”函数,它可以让您处理除 jumpShot% 之外的所有预设,那么您不应该直接分解一个继承自原始函数的类?

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

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.

If you have a big hairy function takes 10 parameters and looks like

function (location, type, gender, jumpShot%, SSN, vegetarian, salary) {
    //weird stuff
}

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一江春梦 2024-09-06 04:19:15

柯里化有很多用途。从简单地为经常使用的函数指定默认参数到返回服务于特定目的的专用函数。

但让我给你举这个例子:

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

Currying has many uses. From simply specifying default parameters for functions you use often to returning specialized functions that serve a specific purpose.

But let me give you this example:

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')
悲凉≈ 2024-09-06 04:19:15

在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 
}

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

In javascript I do currying on callback functions (because they cannot be passed any parameters after they are called (from the caller)

So something like:

...
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 
}

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)

渔村楼浪 2024-09-06 04:19:15

当以函数式风格编程时,您经常绑定参数以从旧函数生成新函数(在本例中为谓词)。伪代码:

filter(bind_second(greater_than, 5), some_list)

可能相当于:

filter({x : x > 5}, some_list)

where {x : x > 5} 是一个匿名函数定义。也就是说,它构建了 some_list 中所有大于 5 的值的列表。

When programming in a functional style, you often bind arguments to generate new functions (in this example, predicates) from old. Pseudo-code:

filter(bind_second(greater_than, 5), some_list)

might be equivalent to:

filter({x : x > 5}, some_list)

where {x : x > 5} is an anonymous function definition. That is, it constructs a list of all values from some_list which are greater than 5.

寄居者 2024-09-06 04:19:15

在许多情况下,要省略的参数在编译时并不知道,而是在运行时知道。此外,对于给定函数可能存在的柯里化委托的数量没有限制。以下内容改编自真实世界的程序。

我有一个系统,在其中我向远程计算机发送命令数据包并接收返回的响应数据包。每个命令包都有一个索引号,每个回复都带有它所响应的命令的索引号。一个典型的命令翻译成英语可能是“从地址 0x12300 开始给我 128 个字节”。典型的响应可能是“成功”。以及 128 字节的数据。

为了处理通信,我有一个例程,它接受多个命令数据包,每个命令数据包都有一个委托。收到每个响应后,相应的委托将在收到的数据上运行。与上述命令关联的委托类似于“确认我获得了 128 字节数据的‘成功’,如果是这样,请将它们存储到地址 0x12300 处的缓冲区中”。请注意,在任何给定时间可能有多个数据包未完成;柯里化的地址参数对于例程知道传入数据应该去哪里是必要的。即使我想编写一个不需要地址参数的“将数据存储到缓冲区”例程,它也无法知道传入数据应该去哪里。

In many cases, the parameters to be omitted will not be known at compile time, but rather at run time. Further, there's no limit to the number of curried delegates that may exist for a given function. The following is adapted from a real-world program.

I have a system in which I send out command packets to a remote machine and receive back response packets. Every command packet has an index number, and each reply bears the index number of the command to which it is a response. A typical command, translated into English, might be "give me 128 bytes starting at address 0x12300". A typical response might be "Successful." along with 128 bytes of data.

To handle communication, I have a routine which accepts a number of command packets, each with a delegate. As each response is received, the corresponding delegate will be run on the received data. The delegate associated with the command above would be something like "Confirm that I got a 'success' with 128 bytes of data, and if so, store them into my buffer at address 0x12300". Note that multiple packets may be outstanding at any given time; the curried address parameter is necessary for the routine to know where the incoming data should go. Even if I wanted to write a "store data to buffer" routine which didn't require an address parameter, it would have no way of knowing where the incoming data should go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文