Questions tagged [closures]

A function (often anonymous) bound to the referencing environment in its original lexical scope in such a way that it will still have access to that environment (its variables and other references) if executed outside that scope.

A function (often anonymous) bound to the referencing environment in its original lexical scope in such a way that it will still have access to that environment (its variables and other references) if executed outside that scope.

It is the binding between the function and its original scope which distinguishes a closure from a simple anonymous function. Many imperative languages (e.g. Pascal and C) support function pointers, but when the referenced function is executed, it will only have access to its internal variables and to the referencing environment of the scope in which it is executed.

Closures have been closely associated with functional programming since the invention of the Scheme language (in which all lambda functions are closures). While it is possible to construct a functional language without closures (the first versions of LISP did not have them), all modern functional languages currently support them.

Closures are found much less frequently in imperative languages (while Smalltalk had them, Java still does not - though they are promised in version 8). Dynamic languages (e.g Perl, Ruby, JavaScript) have historically been much more forward in this area than their non-dynamic cousins.

49 questions
161
votes
9 answers

What is a closure?

Every now and then I see "closures" being mentioned, and I tried looking it up but Wiki doesn't give an explanation that I understand. Could someone help me out here?
gablin
  • 17,525
41
votes
5 answers

Is a lambda expression something more than an anonymous inner class with a single method?

There is a new hype with the long awaited lambda expressions in Java 8; every 3 day another article appears with them about how cool they are. As far as I have understood a lambda expression is nothing more than an anonymous inner class with a…
Random42
  • 10,520
  • 10
  • 52
  • 65
31
votes
3 answers

What are the benefits and disadvantages in the approaches of C#, Java and Scala to Closures/Lambdas/...?

I wonder what the technical implementation differences between C# and Scala are and how both solutions compare to the implementation ideas and concerns voiced in the email Peek Past lambda by Brian Goetz, sent to the mailing list of Project Lambda…
soc
  • 381
17
votes
6 answers

Why is closure important for JavaScript?

C#'s lambda expression also has closures but is rarely discussed by the C# communities or books. I see far more JavaScript people and books talk about its closures than they do in the C# world. Why is that?
TomCaps
  • 311
15
votes
4 answers

Is garbage collection needed for implementing safe closures?

I recently attended an online course on programming languages in which, among other concepts, closures were presented. I write down two examples inspired by this course to give some context before asking my question. The first example is an SML…
Giorgio
  • 19,764
15
votes
1 answer

Does Groovy follow Tennent's Correspondence Principle?

Here's an interesting discussion of Tennent's Correspondence Principle, and a brief description from Neal Gafter: The principle dictates that an expression or statement, when wrapped in a closure and then immediately invoked, ought to have the same…
Armand
  • 6,528
11
votes
4 answers

How will closures in Java impact the Java Community?

It is one of the most talked about features planned for Java: Closures. Many of us have been longing for them. Some of us (including I) have grown a bit impatient and have turned to scripting languages to fill the void. But, once closures have…
10
votes
3 answers

Are closures with side-effects considered "functional style"?

Many modern programming languages support some concept of closure, i.e. of a piece of code (a block or a function) that Can be treated as a value, and therefore stored in a variable, passed around to different parts of the code, be defined in one…
Giorgio
  • 19,764
9
votes
3 answers

Why is there so much buzz about closures?

Why is there so much buzz about closures among developers? In my career I never intentionally used them, though don't clearly understand what they are. UPD: just to clarify. The question is about why the closure concept became so talky these days.
user1449
8
votes
3 answers

What common programming problems are best solved by using prototypes and closures?

As much as I understand both concepts, I can't see how can I take advantage of JavaScript's closures and prototypes aside from using them for creating instantiable and/or encapsulated class-like blocks (which seems more of a workaround than an asset…
deprecated
  • 3,297
7
votes
4 answers

Are a class's methods a type of closure?

Per MDN A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consists of any local variables that were in-scope at the time that the closure was created.…
tt9
  • 631
7
votes
1 answer

What is the most generic way to provide a variable amount of outputs from a Rust function?

I am currently writing an API for machine learning algorithms in Rust and I would like for a single genetic algorithm, artificial neural network, or Bayesian network to provide multiple outputs so that for instances in which there may be redundancy,…
vadix
  • 81
7
votes
2 answers

Every function is a closure?

Wikipedia says, that closure - is a function, which has an access to variables, declared outside of the function. There is even an example: function startAt(x) function incrementBy(y) return x + y return incrementBy variable closure1 =…
7
votes
1 answer

What is a closure and how is it implemented in Ruby?

In the context of the Ruby programming language, what is a closure and when do you use one? What are the uses for it in Rails?
7
votes
2 answers

When we talk about 'a closure' do we refer to a single variable, or do we refer to all 'closed' variables?

Let's take the following (JavaScript) code that returns a function that closes over variables x and y to illustrate: function test() { var x = Math.random(); var y = Math.random(); var f = function() { console.log(x, y); }; return…
1
2 3 4