闭包和柯里化有什么区别和可能的相似之处?
我已经阅读了这里关于闭包和柯里化的一些帖子,但我觉得我没有找到答案。 那么闭包和柯里化有什么区别和可能的相似之处呢? 谢谢您的帮助 :)
I've read through some of the post on here about closures and currying but I feel like I didn't find the answer. So what's the differences and possibly the similarities of closures and currying? Thanks for the help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
柯里化首先实际上是一个数学概念。 这是公正的观察,对于任何n-ary函数f:S0×...Sn → R,您可以定义一个带有 n-1 个参数的新函数 fprime (刚刚发现了一个 markdown bug!),其中第一个参数被常量替换。 因此,如果您有一个函数
add(a,b)
,您可以定义一个新函数add1(b)
为add1(b) ::= add (1, b)
...将“::=”读作“被定义为”。
闭包更多的是一个编程概念。 (当然,编程中的一切也是一个数学概念,但是闭包因为编程而变得有趣。)当你构造一个闭包时,你绑定一个或多个变量; 您正在创建一段代码,其中包含一些与之相关的变量。
关系在于,您可以使用闭包来实现柯里化:您可以通过创建一个将第一个参数绑定到 1 的闭包来构建上面的
add1
函数。Currying is really a mathematical concept first and foremost. It's the just observation that for any n-ary function f: S0×...Sn → R, you can define a new function fprime (just found a markdown bug!) with n-1 parameters where that first parameter is replaced by a constant. So, if you have a function
add(a,b)
, you can define a new functionadd1(b)
asadd1(b) ::= add(1, b)
...reading "::=" as "is defined to be."
A closure is more of a programming concept. (Of course, everything in programming is a mathematical concept as well, but closures became interesting because of programming.) When you construct a closure, you bind one or more variables; you're creating a chunk of code that has some variables tied to it.
The relationship is that you can use a closure in order to implement currying: you could build your
add1
function above by making a closure in which that first parameter is bound to 1.