柯里化只是避免继承的一种方法吗?
所以我对柯里化的理解(基于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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
柯里化有很多用途。从简单地为经常使用的函数指定默认参数到返回服务于特定目的的专用函数。
但让我给你举这个例子:
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:
在javascript中,我对回调函数进行柯里化(因为它们在被调用后(从调用者)无法传递任何参数
所以类似:
我不确定这是否足够详细或连贯......但柯里化基本上可以让你'预填充“参数并返回已填充数据的裸函数调用(而不是要求您在其他点填充该信息)
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:
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)
当以函数式风格编程时,您经常绑定参数以从旧函数生成新函数(在本例中为谓词)。伪代码:
可能相当于:
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:
might be equivalent to:
where
{x : x > 5}
is an anonymous function definition. That is, it constructs a list of all values fromsome_list
which are greater than 5.在许多情况下,要省略的参数在编译时并不知道,而是在运行时知道。此外,对于给定函数可能存在的柯里化委托的数量没有限制。以下内容改编自真实世界的程序。
我有一个系统,在其中我向远程计算机发送命令数据包并接收返回的响应数据包。每个命令包都有一个索引号,每个回复都带有它所响应的命令的索引号。一个典型的命令翻译成英语可能是“从地址 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.