closures

Closure in python?

浪子不回头ぞ 提交于 2019-12-04 03:47:41
问题 When I run this code, I get this result: 15 15 I expect the output should be 15 17 but it is not. The question is: why? def make_adder_and_setter(x): def setter(n): x = n return (lambda y: x + y, setter) myadder, mysetter = make_adder_and_setter(5) print myadder(10) mysetter(7) print myadder(10) 回答1: Python 2.x has a syntax limitation that doesn't allow to capture a variable in read/write. The reason is that if a variable is assigned in a function there are only two possibilities: the

Borrow-check error with variable not living long enough in nested lambda

妖精的绣舞 提交于 2019-12-04 03:38:48
I am getting an error inside a nested lambda. let rows = vec![ vec![3, 6, 2, 8, 9, 0], vec![0, 0, 1, 4, 5, 1], ]; let pair_sums = rows.iter() .flat_map(|row| { (0 ..= row.len()).map(|i| row[i] + row[i + 1]) }) .collect::<Vec<_>>(); println!("{:?}", pair_sums); error[E0597]: `row` does not live long enough --> src/main.rs:9:40 | 9 | (0..row.len() - 1).map(|i| row[i] + row[i + 1]) | --- ^^^ does not live long enough | | | capture occurs here 10 | }) | - borrowed value only lives until here 11 | .collect::<Vec<_>>(); | - borrowed value needs to live until here I can sort of see why this is

Passing parameters into a closure for setTimeout

删除回忆录丶 提交于 2019-12-04 03:28:31
问题 I've run into an issue where my app lives in an iframe and it's being called from an external domain. IE9 won't fire the load event when the iframe loads properly so I think I'm stuck using setTimeout to poll the page. Anyway, I want to see what duration is generally needed for my setTimeout to complete, so I wanted to be able to log the delay the setTimeout fires from my callback, but I'm not sure how to pass that context into it so I can log it. App.readyIE9 = function() { var timings = [1

Scala closures compared to Java innerclasses -> final VS var

南楼画角 提交于 2019-12-04 03:12:51
问题 I've first asked this question about the use of final with anonymous inner classes in Java: Why do we use final keyword with anonymous inner classes? I'm actually reading the Scala book of Martin Odersky. It seems Scala simplifies a lot of Java code, but for Scala closures I could notice a significant difference. While in Java we "simulate" closures with an anonymous inner class, capturing a final variable (which will be copied to live on the heap instead of the stack) , it seems in Scala we

How to removeEventListener that was added using closure?

穿精又带淫゛_ 提交于 2019-12-04 03:03:33
问题 This is basically a followup question to this: Can't pass event to addEventListener: closure issue. I have read almost every related question and can't find the answer to this. The function below is executed within a loop where the parameters are pulled from an array of data. With this function I am able to pass different/new parameters to each instance of an event listener. The outerfunction allows for the values of the parameters to be encapsulated within closure so that the actual values

odd lambda behavior

时光怂恿深爱的人放手 提交于 2019-12-04 02:54:17
I stumbled across this article and found it very interesting, so I ran some tests on my own: Test One: List<Action> actions = new List<Action>(); for (int i = 0; i < 5; ++i) actions.Add(() => Console.WriteLine(i)); foreach (Action action in actions) action(); Outputs: 5 5 5 5 5 Test Two: List<Action> actions = new List<Action>(); for (int i = 0; i < 5; ++i) { int j = i; actions.Add(() => Console.WriteLine(j)); } foreach (Action action in actions) action(); Outputs: 0 1 2 3 4 According to the article, in Test One all of the lambdas contain a reference to i which causes them to all output 5.

JS: What's the difference between a ! closure and () closure? [duplicate]

大城市里の小女人 提交于 2019-12-04 02:16:01
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: What does the exclamation mark do before the function? So I was going back and looking over some of my own code as well as some other javascript code and I realized that a while back when I started writing javascript libraries I was using closures that looked something like this: (function( window, document, undefined ) { // code })( window, document ); But then I saw some bootstrap code and I changed to this

Over how much of its enclosing scope does a (javascript) closure close?

ぐ巨炮叔叔 提交于 2019-12-04 02:15:48
问题 When I have some function that uses variables from its enclosing scope(s) and use that function outside of this scope (these scopes), then this is called a closure. Is there any specification about over "how much" of its enclosing scope(s) a function has to close? (Or, put differently, over "how less" it absolutely needs to close?) Consider: function Outer() { var bigGuy = createSomethingHuge(); var tinyNumber = 42; return (function () { /* CONTENTS */ }); } Or even: function Layer1() { var

setTimeout() - in for loop with random delay [duplicate]

╄→гoц情女王★ 提交于 2019-12-04 02:12:48
This question already has answers here : Closed 7 years ago . Possible Duplicate: Javascript closure inside loops - simple practical example Seen many posts talking about setTimeout and closures but I'm still not able to pass in a simple for loop counter. for (i = 0; i < 5; i++) { setTimeout(function () { console.log(i); }, Math.floor(Math.random() * 1000)); } Gives 5 5 5 5 5 Would like to have 0 1 2 3 4 What's wrong ? Please don't flame, I thought I have understood the setTimeout() tale but apparently not. You can use a closure to keep a reference to the current value of i within the loop:

Javascript object literal, how to solve context?

家住魔仙堡 提交于 2019-12-04 01:55:45
问题 I would like to start organizing my code properly, so I want to use object literals. In the following case, I'm doing a pseudo class. I would like that init() could work as a constructor, but unfortunately, I'm not seeing how to set attributes based on object context. var car = { context : this, wheels : 0, color : '', speed : 0, init : (function(x){ console.log(x); x.wheels = 4; x.color = 'red'; x.speed = 120; })(context) }; console.log(car.color); 回答1: You can't immediately run a function