闭包和柯里化有什么区别和可能的相似之处?

发布于 2024-07-10 16:01:45 字数 77 浏览 5 评论 0原文

我已经阅读了这里关于闭包和柯里化的一些帖子,但我觉得我没有找到答案。 那么闭包和柯里化有什么区别和可能的相似之处呢? 谢谢您的帮助 :)

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 技术交流群。

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

发布评论

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

评论(1

若能看破又如何 2024-07-17 16:01:45

柯里化首先实际上是一个数学概念。 这是公正的观察,对于任何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 function add1(b) as

add1(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.

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