Skip to main content
Share Your Experience: Take the 2024 Developer Survey

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.

Filter by
Sorted by
Tagged with
4 votes
3 answers
879 views

Is this extensive usage of closure a known (anti-)pattern? And does it have a name?

I often use function closures for storing data (e.g. database URL), which doesn't change between function calls. Is this an (anti-)pattern? Does it have a name? While developing apps, which recieve ...
Sebastian DonnerWolfBach's user avatar
2 votes
2 answers
262 views

Is there a name for a closure which just returns a value?

I have a PHP closure defined as function () use ($contents) { return $contents; }. It's like a value object except it's a closure. Is there a name for this?
chx's user avatar
  • 373
-2 votes
1 answer
240 views

Best way to deal with lots of nested closures in Swift

I have some external SDK library that makes IO calls (either networking or database) in the form of blocks, like so: SomeClass.doWork(success: {}, failure: {}) Now I need to chain about 60 different ...
zaitsman's user avatar
  • 384
0 votes
2 answers
3k views

Purpose of Closure Style in Laravel/PHP?

Many of Laravel's build-in framework libraries use closures as arguments. What is the primary purpose of this coding style? Is it just to gain more control over the configuration of the function, to ...
Charlie Dalsass's user avatar
3 votes
3 answers
4k views

Are nested closures a good thing?

I've been doing Swift development for a while now with Firebase, and I find myself making a lot of code like this: Auth.auth().signIn(with: credential) { (user, error) in if let error = error { ...
Forest Kunecke's user avatar
2 votes
0 answers
1k views

Should I use a closure instead of a class when I only have one method?

Lets say I have a JavaScript class that takes a whole lot of settings in the constructor, and then only have one method that the user of the class might want to call multiple times. Here's an example: ...
Anders's user avatar
  • 1,351
2 votes
1 answer
947 views

Is it possible to have nested functions, without having closures, in a lexically scoped language which has first class functions?

A closure is a record storing a function, together with an environment: A mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope)...
Utku's user avatar
  • 1,922
6 votes
4 answers
718 views

javascript closure = c# properties?

For anyone who is expert in both Javascript and C#: Can we think of JS closures the way we think about Private and Public Properties in c#? A closure refers to an inner function (public property) ...
nanonerd's user avatar
  • 189
2 votes
4 answers
328 views

Understanding closures

I'm reading the JavaScript guide on MDN and trying to understand the usefulness of closures. This sentence helped me the most, "In other words, the functions defined in the closure 'remember' the ...
ScrollingMarquees's user avatar
4 votes
1 answer
1k views

Why closure declaration syntax in Swift is different from function declaration

let closure = { (args) -> ReturnT in ... } vs func function(args) -> ReturnT { ... } Why didn't Apple follow the principle of Occam's razor and make closure and function declarations equal? ...
kelin's user avatar
  • 153
6 votes
3 answers
728 views

What is the difference between explicit parameters and closures

In F#, a tail recursive List.filter function can be implemented like: let filter f = let rec filterImpl f acc = function | [] -> List.rev acc | h :: t -> filterImpl f (if f ...
rexcfnghk's user avatar
  • 183
-1 votes
1 answer
101 views

Javascript Closures to Separate Class [closed]

PROBLEM: In Javascript I use closures to encapsulated methods inside a class. It helps building hierarchy inside and minimize amount of methods at root, but they become lengthy. RESEARCH: Applied to ...
Zon's user avatar
  • 187
4 votes
1 answer
746 views

A JavaScript IIFE prevents leaks to the global space as a closure? Is that the correct concept?

I see in the code or sometimes people talk about it, for some JavaScript code: (function() { var something; function someFunction() { // some code here } // do something }(...
nonopolarity's user avatar
  • 1,827
1 vote
2 answers
2k views

What is the use case for shadowing variables?

One of the things the coffeescript programming language is criticized for is its treatment of variable declarations and scope (example). The answers to this question (and the blog I linked to above) ...
Jared Smith's user avatar
  • 1,925
7 votes
4 answers
1k views

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 ...
tt9's user avatar
  • 621
6 votes
1 answer
535 views

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 ...
vadix's user avatar
  • 71
2 votes
1 answer
88 views

Where is the closure argument initialized?

In this Wikipedia article about Closures function startAt(x) function incrementBy(y) return x + y return incrementBy variable closure1 = startAt(1) variable ...
zuzuleinen's user avatar
-3 votes
1 answer
214 views

Describe closures to a non programmer [closed]

I will have a presentation about closures, and a non programmer friend of mine asked, what closures are. With what analogy would you describe that? I mean "take a look at the lamba calculus" wouldn't ...
Dániel Nagy's user avatar
1 vote
1 answer
1k views

What are the benefits of using new over closures

I've been programming in JS for over a year now, mainly in angularJS. And I can honestly say I've never made a function use prototypical inheritance. When ever i need a class like object that has ...
user133688's user avatar
7 votes
2 answers
4k views

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) ...
ozahorulia's user avatar
3 votes
0 answers
384 views

JS closures - Passing a function to a child, how should the shared object be accessed

I have a design and am wondering what the appropriate way to access variables is. I'll demonstrate with this example since I can't seem to describe it better than the title. Term is an object ...
DanielST's user avatar
  • 347
0 votes
1 answer
156 views

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

I'm referencing this link about closures: https://stackoverflow.com/questions/111102/how-do-javascript-closures-work This is specifically about javascript. In the second answer with 956 votes, The ...
user3029001's user avatar
5 votes
3 answers
4k 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 ...
kuzzooroo's user avatar
  • 595
3 votes
1 answer
4k 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 ...
Stevo's user avatar
  • 453
5 votes
3 answers
518 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 ...
Jeremyx's user avatar
  • 151
5 votes
5 answers
2k 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 ...
Giorgio's user avatar
  • 19.7k
1 vote
1 answer
3k 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 = ...
john-charles's user avatar
4 votes
4 answers
19k 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 function'...
Mark Bubel's user avatar
0 votes
1 answer
693 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. ...
1252748's user avatar
  • 133
41 votes
5 answers
24k 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 a lambda expression is ...
Random42's user avatar
  • 10.5k
15 votes
4 answers
3k 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 ...
Giorgio's user avatar
  • 19.7k
7 votes
1 answer
8k 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?
Michael Durrant's user avatar
10 votes
3 answers
3k 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 to ...
Giorgio's user avatar
  • 19.7k
4 votes
1 answer
3k 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 lifetime. ...
Giorgio's user avatar
  • 19.7k
5 votes
1 answer
1k 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 ...
Giorgio's user avatar
  • 19.7k
3 votes
1 answer
304 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 ...
Vivian River's user avatar
  • 2,397
3 votes
2 answers
928 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 ...
sdespolit's user avatar
  • 133
7 votes
2 answers
317 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 = ...
Michiel van Oosterhout's user avatar
2 votes
2 answers
191 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 c=a,b,c=(a=c.call?c:null)?...
Hey's user avatar
  • 832
8 votes
3 answers
2k 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 ...
deprecated's user avatar
  • 3,297
15 votes
1 answer
432 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 ...
Armand's user avatar
  • 6,518
31 votes
3 answers
3k 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 ...
soc's user avatar
  • 381
2 votes
3 answers
2k 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 ...
prasonscala's user avatar
  • 1,631
9 votes
3 answers
938 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 ...
user avatar
17 votes
6 answers
6k 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 ...
TomCaps's user avatar
  • 311
3 votes
3 answers
484 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 ...
xiao's user avatar
  • 995
11 votes
4 answers
666 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 ...
Ryan Delucchi's user avatar
6 votes
3 answers
3k 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 ...
Patrick's user avatar
  • 2,922
161 votes
9 answers
73k 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?
gablin's user avatar
  • 17.4k