Questions tagged [lexical-closures]

Lexical closures are functions, often anonymous, that capture, or close over, their lexical environment. Lexical closures are used extensively in functional programming techniques that involve higher-order functions.

53 questions
106
votes
9 answers

Scope of lambda functions and their parameters?

I need a callback function that is almost exactly the same for a series of gui events. The function will behave slightly differently depending on which event has called it. Seems like a simple case to me, but I cannot figure out this weird behavior…
agartland
  • 1,654
  • 2
  • 15
  • 19
63
votes
7 answers

Do we have closures in C++?

I was reading about closures on the net. I was wondering if C++ has a built-in facility for closures or if there is any way we can implement closures in C++?
Parvinder Singh
  • 1,737
  • 2
  • 12
  • 17
26
votes
1 answer

Getting arguments passed to an ES6 arrow function using arguments variable

I understand how arrow functions work in ES6, and the lexical this, but I was wondering if anyone knows of a way to get the arguments passed to an arrow function? In ES5, you can simply do: function foo( bar, baz ){ console.log('Args:',…
Justin
  • 1,959
  • 5
  • 22
  • 40
15
votes
2 answers

Accessing VUE JS's data from Axios

I have a Vue JS (Vuetify) App that makes an ajax request that I would like to populate a div's content with the response, however I am having difficulties accessing the instance's data. All examples I have seen use this to point to the data object,…
Haddad
  • 369
  • 1
  • 2
  • 9
8
votes
4 answers

javascript currying

I'm trying to create curry function that can be applied to any function and return another, with 1 of the arguments applied. Properties that I want to have: If function has only one argument curry function should return value: f(a); curry(f,x) =…
qnikst
  • 302
  • 1
  • 9
8
votes
2 answers

Prevent JavaScript closure from inheriting scope

I am looking for a fancy way to prevent a closure from inheriting surrounding scrope. For example: let foo = function(t){ let x = 'y'; t.bar = function(){ console.log(x); // => 'y' }); }; there are only two ways I know of preventing…
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
8
votes
4 answers

Lexical scope/closures in javaScript

I understand functions in 'js' have lexical scope (i.e. functions create their environment (scope) when they are defined not when they are executed.) function f1() { var a = 1; f2(); } function f2() { return a; } f1(); // a is not…
Antonio Pavicevac-Ortiz
  • 7,239
  • 17
  • 68
  • 141
7
votes
1 answer

Using NSUndoManager, how to register undos using Swift closures

I am trying to grok how to use NSLayoutManager using Swift closures. I can successfully register an undo as follows: doThing(); undoManager?.registerUndoWithTarget(self, handler: { _ in undoThing(); } undoManager?.setActionName("do thing") Of…
wcochran
  • 10,089
  • 6
  • 61
  • 69
7
votes
4 answers

Emacs lisp: why does this sexp cause an invalid-function error?

The sexp in question is (((lambda (b) (lambda (a) (+ b a))) 3) 5) which, to me, looks like it should evaluate to 8, and in other lisps (e.g. Racket) it does, but in elisp it instead throws this error: Debugger entered--Lisp error:…
James Porter
  • 1,853
  • 2
  • 17
  • 30
6
votes
2 answers

Recursive closure in JavaScript

function buildList( list ) { var i = 0; var first = function () { console.log( "in" ) console.log( i ); } var Second = function () { console.log( "out" ) first(); } return Second; } var a = buildList( [1, 2, 3]…
6
votes
1 answer

What does "lexical" mean in the statement "C# implements lexical closure"?

I'm reading about C# and closure, various articles, Jon Skeet's awesome "C# in Depth" and I see statements like "C# and ruby implement lexical closure". Why do the authors include the word "lexical"? Why not just say they "implement closure"? What…
richard
  • 12,263
  • 23
  • 95
  • 151
5
votes
1 answer

Lexical closures over macrolet?

Is there a way to do something like lexical closures using macrolet? What I want to do is make the following macro a local recursive helper that calls a function on each combination instead of generating a list as it does now calling the macro in…
asm
  • 8,758
  • 3
  • 27
  • 48
5
votes
2 answers

Clojure closure efficiency?

Quite often, I swap! an atom value using an anonymous function that uses one or more external values in calculating the new value. There are two ways to do this, one with what I understand is a closure and one not, and my question is which is the…
4
votes
4 answers

How does the JS scope of these blocks work?

Can anyone explain why the following produces 1,2 and the other produces 5? Should they not both produce 5? //produces 1,2 (function () { var a = [5]; function bar() { if (!a) { var a = [1, 2]; } …
Harvester316
  • 381
  • 1
  • 8
  • 17
4
votes
1 answer

Closures get parent function name

Bash is “kind of function programming language” it dose not have classes. I managed to use encapsulation with Closures, but I want also to do some introspection to find also docker_ parent/super/base function (If you know add comments to define this…
Andrei.Danciuc
  • 1,000
  • 10
  • 24
1
2 3 4