"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.)
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>
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.
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".
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>
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.