TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Closures – A Simple Explanation (Using Ruby)

54 点作者 bleakgadfly超过 13 年前

9 条评论

colanderman超过 13 年前
"the most popular languages that are in use right now don't support closures (<i>Java</i>, C++)"<p>That is blatantly wrong. Closures, that is, the implementation structure which stores along with a code pointer the references or values to which local variables referenced by said code are bound, have been in Java since version <i>1.0</i>. Otherwise you could not write code such as:<p>class foo { final static int bar = 6; static class baz { int bla() { return bar; } } }<p>and expect "(new foo.baz()).bla()" to return 6.<p>What Java <i>is</i> missing (and I suspect what the author is conflating closures with) are <i>anonymous functions</i>, which require closures to function properly for the same reason inner classes require closures.<p><i>Closures are not anonymous functions.</i> Closures are an <i>implementation detail</i> which allow anonymous functions (among other structures) to act as expected in statically-scoped languages.<p>Scheme has anonymous functions and closures.<p>Java does not have anonymous functions but <i>does</i> have closures.<p>Emacs Lisp has anonymous functions but does <i>not</i> have closures (and thus does not exhibit static scope).<p>BASIC has neither anonymous functions nor closures.<p>(It is worthwhile to note that there are other methods for implementing static scope such as alpha-renaming but they are usually much less efficient than closures.)
评论 #2919231 未加载
评论 #2919642 未加载
评论 #2919396 未加载
评论 #2919410 未加载
评论 #2919494 未加载
enneff超过 13 年前
From the succinct description of closures:<p><pre><code> It remembers the values of all the variables that were in scope when the function was created. It is then able to access those variables when it is called even though they may no longer be in scope. </code></pre> No! It doesn't remember the <i>values</i>. It binds the <i>variables</i>. This is a major misstep in describing closures. The most important point: when the values of the bound variables change <i>the closure sees the new values</i>.<p>Please, fix this!<p>A better simple description of closures:<p>* You can pass a closure around as a value (to be called later).<p>* When you create a closure it is bound to the variables in its current scope. When you call a closure, it sees the <i>current</i> value of the bound variables (not their values at the time the closure was created).<p>Follow this with a simple example, like:<p><pre><code> i := 0 inc := func() { i++ } println(i) // prints "0" inc() println(i) // prints "1" inc() println(i) // prints "2"</code></pre>
评论 #2919406 未加载
malkia超过 13 年前
For me one of the coolest ways of using closures was to implement undo system. It's not very efficient, but easy to write and compose.<p>Basically every editor operation can return a function that does the opposite. For example (some lua-like language)<p>function DeleteLine(lines, line_number)<p><pre><code> local deletedLine = lines[line_number] -- do the actual deletion - move elements up return function() InsertLine(lines, line_number, deletedLine) end end function InsertLine(lines, line_number, deletedLine) -- do the actual insertion return function() DeleteLine(lines, line_number) end end </code></pre> Again, not very efficient - but easy to grasp. Without closures would be hard to compose - you would have to take care of a lot of extra parameters where they need to be stored, etc.
评论 #2919855 未加载
ttyS0超过 13 年前
To save you time, you can skip the initial rant about how closures are not taught in universities and start reading from the heading "A Concept Explained Simply".
dmbass超过 13 年前
This explanation did not add anything to help me better understand closures and anonymous functions. I already understand that closures allow you to pass an anonymous function around and it maintains the variables that were in its scope when it was created (whether by copy or reference).<p>The problem, at least for me, is finding a non-trivial example of using an anonymous function. Does anybody have other examples like malkia's[1] that demonstrate why anonymous functions and closures are useful? Or maybe just a good explanation of functional programming?<p>1. <a href="http://news.ycombinator.com/item?id=2919149" rel="nofollow">http://news.ycombinator.com/item?id=2919149</a>
评论 #2919387 未加载
评论 #2919373 未加载
评论 #2919455 未加载
评论 #2919331 未加载
rgrove超过 13 年前
Back to the Future II is the best explanation of closures I've ever seen.
评论 #2919639 未加载
lispm超过 13 年前
Closures are functions plus their bindings for its free variables. The references to the bindings have lexical scope and indefinite extent.
jrvarela56超过 13 年前
I dont understand the difference between a closure and an object's state. Is an object one example of a closure? You can pass it around and it contains instance variables.<p>What are lambdas for in this context? Simply calling the object's attribute would return the value set at creation - even if modified afterwards.
评论 #2919815 未加载
jabo超过 13 年前
Wow, I've seen functions like these in Erlang, but I didn't realize their usefulness until reading this.