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.

learn more… | top users | synonyms

0
votes
1answer
95 views

what does it mean when a nested function isn't assigned to a var?

I'm referencing this link about closures: http://stackoverflow.com/questions/111102/how-do-javascript-closures-work This is specifically about javascript. In the second answer with 956 votes, The ...
4
votes
2answers
211 views

Using a closure to avoid code duplication in Python

Sometimes I find myself wanting to run the same code from a few different spots in the same function. Say I have some function func1, and I want to do the same thing from a few different spots in ...
2
votes
2answers
214 views

Is there a point in using closures for non-fully functional programming languages? [duplicate]

Maybe I don't understand closures fully but take for example C#, why would I use closures when I can use classes? Am I missing something? Note that I read this one already but my question is specific ...
3
votes
1answer
255 views

Utilising Promises, Closures and Recursion in Node.js

I have employed a recursive call to a closure in order to mitigate against some race conditions I am getting (That's what I think it is). I'd like to know if (and why) this is a good solution or a bad ...
5
votes
3answers
279 views

Interface at the class or function level?

I have been falling into a pattern lately where I have been defining routines that rely on an interface defined by a function that is specified as a parameter to the routine. (The language is C#, but ...
3
votes
5answers
491 views

Are closures sufficient to characterize functional programming?

All functional programming languages that I know of (e.g. Haskell, Scala, Scheme, Clojure, SML, OCaml, ...) support a notion of closures. Also, I often read that a language X can be considered ...
0
votes
1answer
802 views

Javascript idioms this that vs this self [closed]

I've been writing JavaScript for a bit and have now seen code using the idiom var that = this; and var self = this; Used to gain access to this through closure scope. At this point var self = ...
0
votes
4answers
1k views

What's so useful about closures (in JS)? [duplicate]

In my quest to understand closures in the context of JS, I find myself asking why do you even need to use closures? What's so great about having an inner function be able to access the parent ...
1
vote
1answer
332 views

Make a flowchart to demonstrate closure behavior

I saw below test question the other day in which the authors used a flowchart to represent the logic of loops, and I got to thinking it would be interesting to do this with some more complex logic. ...
10
votes
4answers
3k views

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 they a lambda expression is ...
12
votes
4answers
1k views

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 ...
2
votes
1answer
1k views

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?
6
votes
3answers
587 views

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 ...
2
votes
1answer
620 views

Using captured non-local variables in C++ closures

On this wikipedia page I have found the following sentence regarding closures in C++11: C++11 closures can capture non-local variables by copy or by reference, but without extending their ...
3
votes
1answer
218 views

Can methods in an OOP language be considered as some kind of closure?

Consider a method m of a class A in an object-oriented language like Java or C++. In the body of the method m it is possible to reference the member variables of the object on which the method is ...
3
votes
1answer
207 views

What is the proper jargon to refer to a variable wrapped inside a function closure?

In JavaScript, there is no such thing as a "private" variable. In order to achieve encapsulation and information hiding in JavaScript, I can wrap a variable inside a function closure, like so: var ...
3
votes
2answers
318 views

Why to keep wrapping closure in each js-file if it can be added after concatenating?

At the moment every javasrcipt file in our project includes closure wrapper. Before deploying all files are concatenated and minimized. And i'm asking myself, do we really need all that closures OR ...
7
votes
2answers
222 views

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 = ...
2
votes
2answers
162 views

Trimming script size by using array notation for frequently accessed properties

I noticed some redundancy in a script I ran through Google Closure Compiler. (function(){function g(a){var k;if(a){if(a.call)a.prototype=j,a.prototype[e]={}}else a= {};var ...
7
votes
3answers
893 views

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 ...
16
votes
1answer
361 views

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 ...
27
votes
3answers
1k views

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 ...
2
votes
3answers
767 views

difference between passing a value vs passing a function as a parameter in scala

I often hear that scala has a ability to pass function as a parameter to another function. I would like to know the difference between passing a value as a parameter vs passing function itself as the ...
9
votes
3answers
762 views

Why is there so much buzz about closures? [closed]

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 ...
14
votes
7answers
2k views

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 ...
2
votes
3answers
338 views

How to avoid shooting yourself in the foot when using closure in JavaScript

The closure feature is widely used in JavaScript and make it quite difficult for programmer who was used to program in a no-closure language. So what is the best practice about using closure and how ...
11
votes
4answers
567 views

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 ...
4
votes
1answer
859 views

what are the benefits of closure, primarily for PHP?

I am beginning the process of moving code over to PHP 5.3 and one of the most highly touted features of PHP 5.3 is the ability to use closures. My understanding of closures is that they allow ...
47
votes
9answers
14k views

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?