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

python closure weird behavior

I am trying a piece of code from the question in Lexical closures in Python flist = [] for i in xrange(3): def func(x): return x*i flist.append(func) for f in flist: print f.func_closure The output is: None None None Shouldn't it…
jdavid_1385
  • 107
  • 6
1
vote
2 answers

React setstate not merging the old state into the new state

according to many examples, this should work: const [_timeseries, $timeseries] = useState({hi:'lol'}) useEffect(() => { socket.on('plot', e => { let keyname = Object.keys(e)[0] $timeseries({..._timeseries, [keyname] : value)}) } },…
user11789318
1
vote
1 answer

access outer function variable in inner functions

function Outer(){ var a=10; function Inner(){ var a = 20; console.log(a); } Inner(); } Outer(); In this codek I want the inner function to print the value of outer function's a(i.e 10). How do I achieve this?
1
vote
1 answer

Bind method to object runtime late binding

I am aware about late bindings in loop in python, but I cant find way to solve this . def bind_method(object, methods): for method in methods: def my_method(): result = method() return result …
Nilesh
  • 20,521
  • 16
  • 92
  • 148
1
vote
0 answers

Which closure variables does javascript allocate?

i had yet another dive into javascript memory leaks. my question arose while reading Ilya Kantor's article. i am aware of ie's memory-leak caused by circular references between DOM-/COM-objects and javascript objects as Ilya points out. i am also…
espretto
  • 234
  • 2
  • 11
1
vote
1 answer

Parallellize Independent Function Calls that Each Modify Function's Parent Environment

I'd like to find a way to parallelize repeated independent function calls in which each call modifies the function's parent environment. Each execution of the function is independent, however, for various reasons I am unable to consider any other…
1
vote
4 answers

How do I make a function only callable from another function?

//function declerations void func_A(); void func_B(); void func_SubA(); //main int main(){ ... } //function definitions void func_A(){ ... } void func_B(){ ... } void func_SubA(){ ... } What is the best way of ensuring that func_SubA() can only…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
0
votes
2 answers

Variable in wrong scope (maybe needs a closure?)

I have the following code that is in need of a closure: var numItems = document.getElementsByClassName('l').length; for (var i = 0; i < numItems; i++) { document.getElementsByClassName('l')[i].onclick = function (e){ preview(this.href, i); …
Devin Rhode
  • 23,026
  • 8
  • 58
  • 72
0
votes
2 answers

Functions has access to it own lexical as well outer environment

Functions has access to it own lexical as well outer environment but it's not working or maybe I don't understand why it's working like that. Here is the code function makeArmy() { let shooters = []; let i = 0; while (i < 10) { …
Akhil Rana
  • 145
  • 2
  • 12
0
votes
1 answer

ECMASCRIPT Closures - What is Evaluation block in JavaScript?

https://tc39.es/ecma262/multipage/ecmascript-language-statements-and-declarations.html#sec-block-runtime-semantics-evaluation 14.2.2 Runtime Semantics: Evaluation Block : { } Return empty. Block : { StatementList } Let oldEnv be the running…
user19551894
0
votes
0 answers

I can't understand why is the output is -96 of this code

I actually expect that output should be 101 during the first iteration and 3 during the second iteration but it's actually -96 for both iterations. Here is the code const arr = []; for (var i = 100; i > 0; i = i - 99) { arr.push(function () { …
Volodya
  • 3
  • 1
0
votes
0 answers

how lexical scoping happening in closures?

var x = 10; function foo() { var y = x + 5; return y; } function bar() { var x = 2; return foo(); } console.log(bar()); // 15 In my view, output should be 7 since foo()'s lexical envt. is bar() and hence value of x should be taken as 2…
0
votes
1 answer

What version of Perl introduced lexical my subroutines?

According to this bug report one of my modules is generating this error on older Perls, Experimental "my" subs not enabled at ... What should I set my minimum perl version too?
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
0
votes
1 answer

Javascript closure is not incrementing value

function setupCounter(val){ console.log(val); return function counter(){ console.log('counter func ', val); return val++; } } debugger let counter1 = setupCounter(0); console.log(counter1());…
Mekkush
  • 5
  • 4
0
votes
0 answers

Closure/Object Composition Investigation: Why is the first breakpoint hit only once while the breakpoint in closure scope is hit repeatedly?

Could use some feedback on a Saturday afternoon object composition/closure investigation (is it accurate, is it missing any key points, non-key points, can you help answer remaining questions/provide resources to do so). Thanks in advance :) run…