closures

How can I capture variables by anonymous method when using it in OTL?

二次信任 提交于 2020-03-16 05:22:40
问题 What I want to do: I have a few objects in a genric list. I want to capture each of this object in anonymous method and execute this method as a separate OTL Task. This is a simplified example: program Project51; {$APPTYPE CONSOLE} uses SysUtils, Generics.Collections, OtlTaskControl, OtlTask; type TProc = reference to procedure; type TMyObject = class(TObject) public ID: Integer; constructor Create(AID: Integer); end; constructor TMyObject.Create(AID: Integer); begin ID := AID; end; var

Why does a setTimeout delay of 0 still run after all other synchronous code in a for loop?

假如想象 提交于 2020-03-05 05:06:12
问题 I've know versions of this question has been discussed, and I think this is unique. Why does a delay of 0, still causes the below behavior. for(var i = 0; i <3; i ++) { console.log(i, 'started'); setTimeout(()=> { console.log(i); },0) console.log(i, 'done'); } console.log('loop over'); // 0 started // 0 done // 1 started // 1 done // 2 started // 2 done // loop over // 3 3 3 Here is what I think I know so far: Quoted from MDN in respect to setTimeouts position on the stack: This is because

Why does a setTimeout delay of 0 still run after all other synchronous code in a for loop?

 ̄綄美尐妖づ 提交于 2020-03-05 05:05:45
问题 I've know versions of this question has been discussed, and I think this is unique. Why does a delay of 0, still causes the below behavior. for(var i = 0; i <3; i ++) { console.log(i, 'started'); setTimeout(()=> { console.log(i); },0) console.log(i, 'done'); } console.log('loop over'); // 0 started // 0 done // 1 started // 1 done // 2 started // 2 done // loop over // 3 3 3 Here is what I think I know so far: Quoted from MDN in respect to setTimeouts position on the stack: This is because

Protractors, promises, parameters, and closures

牧云@^-^@ 提交于 2020-02-25 05:58:46
问题 I'm now tearing out what little remains of my hair. And I think I've lost vision in my left knee. I have two functions that work. Each one takes a parameter correctly. I would dearly, so very dearly, love one function to take both parameters. I have a super simple data structure: var stuff = [ { name: "Stone", id: "cc45" }, { name: "Hanley", id: "cc78" } ]; I wish to loop through the structure and run a super simple test on each. Please observe: for (var ii = 0; ii < stuff.length; ii++) { var

Closure allocations in C#

不想你离开。 提交于 2020-02-19 09:46:41
问题 I've installed the Clr Heap Allocation Analyzer extension and in a project I see something that I quite don't understand, I've got a method with a signature public Task<int> ExecuteAsync(string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { param = SetModificationValuesForGlobalRing(param); return _sqlPolicy.ExecuteAsync(async () => { int result; using (var connection = new SqlConnection(_connectionString)) { await

swift 3 completion handler to return string

五迷三道 提交于 2020-02-08 10:33:30
问题 I am new at swift and got problem with getting string from function, I am trying to use completion handler, but something is wrong, could you help me? After adding [String : String] to func, I cant get rezult, I want to get response and print it. Error: Cannot convert return expresion of type () to return type [String : String] Requests: public func login(userName: String, password: String) -> [String : String]{ let loginrequest = JsonRequests.loginRequest(userName: userName, password:

How do I kill a setInterval()/setTimout() if I lose the calling object?

。_饼干妹妹 提交于 2020-02-08 03:09:44
问题 I'm working on a rather large app, I need to call a function over an over again while a key is being pressed, at a specific interval. There's parts of the app that I can't edit, but it's replacing my .onkeyup() listeners and sometimes the interval is just left there forever. What I really want is for the interval to stop when the object gets destroyed, reassigned, etc... After setInterval(), bindings, closures, I made this to try something out and now I am even more confused: function

lambda's captured variable is reset

此生再无相见时 提交于 2020-02-05 02:51:11
问题 I'm trying to use lambda s inside a project but I think I'm missing something about the closure's scope. I tested this piece of code which in some way is a simplification of my problem. #include <iostream> #include <functional> using namespace std; void tester_wrapper(std::function<int(void)> cb, int counter){ if (counter == 10) return; else{ cout << cb() << endl; tester_wrapper(cb, counter + 1); } } void tester(std::function<int(void)> cb){ tester_wrapper(cb, 0); } int main() { auto getNum =

Tracking number of function calls + closures (à la SICP) in Python

三世轮回 提交于 2020-02-03 21:50:59
问题 This is a question about scope and closures in Python , motivated by an exercise in SICP. Much thanks for your time if you read this! A question (3.2) in SICP asks one to create a procedure "make-monitored", that takes in a function f (of one parameter) as input and returns a procedure that keeps track of how many times f has been called. (If the input to this new procedure is "num-calls" it returns the number of times f has been called, if it is "reset" it resets counter to 0 and anything

Tracking number of function calls + closures (à la SICP) in Python

爷,独闯天下 提交于 2020-02-03 21:48:06
问题 This is a question about scope and closures in Python , motivated by an exercise in SICP. Much thanks for your time if you read this! A question (3.2) in SICP asks one to create a procedure "make-monitored", that takes in a function f (of one parameter) as input and returns a procedure that keeps track of how many times f has been called. (If the input to this new procedure is "num-calls" it returns the number of times f has been called, if it is "reset" it resets counter to 0 and anything