closures

Closures & async node.js functions

ぐ巨炮叔叔 提交于 2019-12-11 08:22:35
问题 All, Trying to get my head around closures in a node.js context (async calls). I have the following code: timer = setInterval(pollOID, 1000); function pollOID() { for (channel in channels) { session.get({ oid: channels[channel].oid }, function (varbinds) { console.log("The " + channels[channel].name + " is " + varbinds); }); } } The code polls a router for SNMP data each second using a loop in the setInterval callback to query the router for several SNMP entities. The session.get function has

how to update a list of html items based on a list of json data

微笑、不失礼 提交于 2019-12-11 08:18:35
问题 I have a list of checkboxes in my html page, like so: <ul id="myList"> <li class="checkboxItem"> <input type="checkbox" name="item1" value="001" id="item-1"/> <label for="item-1" class="checkboxLabel">Display 1</label> </li> <li class="checkboxItem"> <input type="checkbox" name="item2" value="042" id="item-2"/> <label for="item-2" class="checkboxLabel">Display 42</label> </li> </ul> now I make a call to get some json data, which comes back like so: [{"name":"002","title":"Display 1"}] what I

Type-hint closure parameters

守給你的承諾、 提交于 2019-12-11 08:03:20
问题 With type-hinting in PHP is it possible to type-hint the parameters of a closure? for example function some_function(\Closure<int> $closure) { $closure(3); } // This would throw an exception some_function(function(string $value) { echo $value; }); // This would work. some_function(function(int $value) { echo $value; }); 回答1: Not natively. You would need to manually make use of reflection. <?php function some_function(\Closure $closure) { $reflection = new ReflectionFunction($closure);

Create environment with variables in Scheme

梦想的初衷 提交于 2019-12-11 07:36:27
问题 I was looking to create something similar to an object or an environment kind of like in OOP This is what I was thinking: (define env (variable 'x 10 env)) Where I define an env and create a variable of value 10 in that environment. I also want to be able to call on the values in that environment. For example (get-value env 'x) > 10 The most I can understand is that it involves closures but i'm not sure where to start from there 回答1: The easiest way of achieving this is using alist. The above

mySQL transitive closure table

淺唱寂寞╮ 提交于 2019-12-11 07:26:12
问题 I have some code I've been using in SQL Server to generate a closure table from another table that has just the direct parent/child relationships, I can run very simple queries against this to determine lineage. Now I am needing to do all this in mySQL, but I am having trouble with the recursive querying to generate the closure table... My original SQL server query is WHILE @@ROWCOUNT>0 INSERT INTO [ClosureTable] ([Ancestor], [Descendent]) SELECT distinct [Parent],[tc].[Descendent] FROM

Why does this defun closure not behave the same as the defparameter closure?

有些话、适合烂在心里 提交于 2019-12-11 07:26:07
问题 Consider these two: (defparameter *lfn* (let ((count 0)) #'(lambda () (incf count)))) (defun testclosure () (let ((count 0)) #'(lambda () (incf count)))) Why do they behave differently: CL-USER> (funcall (testclosure)) 1 CL-USER> (funcall (testclosure)) 1 CL-USER> (funcall *lfn*) 1 CL-USER> (funcall *lfn*) 2 count is closed over in the defparameter version but not in the defun version. Why is this? 回答1: Sylwester's answer explains this very well, but in a case an example with more explicit

Javascript benchmarks in testing different emulations of a “Class”

自作多情 提交于 2019-12-11 07:14:47
问题 i have read articles that say: using the prototype will be fastest since functions declared are shared. more detail was explained in this article where tapping JS native prototype will increase performance as compared to using 'improvisions'. closures should perform worse since each creation of it returns a separate copy of a set of functions and variables. objects (functions) are sort of closures but with this . has access control (public/private). they're supposed to be better than closures

Python Tkinter OptionMenu add a command to multiple OptionMenus

感情迁移 提交于 2019-12-11 06:55:56
问题 Basically I have a series of OptionMenus that are created in a loop, but is currently empty: option_menu = [] for ii in range(jj): option_menu.append([]) for ll in range(kk): option_menu[ii].append(OptionMenu(frame,tkinter_text_var[ii][ll],'')) Then elsewhere I use a checkbox to set the values along the lines of: for ii in range(jj): for ll in range(kk): option_menu[ii][ll]["menu"].add_command(label = name_from_box.get(), command = lambda: tkinter_text_var[ii][ll].set(name_from_box.get()))

passing array value using closure

被刻印的时光 ゝ 提交于 2019-12-11 06:39:23
问题 Whats the best way of getting multiple local arrays from another function. I am doing something similar to this. And as far as the variable names attached to a function, "arr1=f1()", do these need to be declared as local ,"var arr1=f1()". <script> function ArrValues(arr){ var array=arr; function f1(){ var ID=[]; ID=['grapes','peaches','plums']; return ID }; function f2(){ var Nam=[]; Nam=['car','motorcycle','tree']; return Nam }; function f3(){ var Num=[]; Num=['200','1000','350']; return Num

How can I pass a FnMut closure to a function using a reference in Rust?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 06:32:45
问题 I've learned how to pass a closure argument to a function so I can call closure twice: let closure = || println!("hello"); fn call<F>(f: &F) where F: Fn(), { f(); } call(&closure); call(&closure); When I use FnMut : let mut string: String = "hello".to_owned(); let change_string = || string.push_str(" world"); fn call<F>(mut f: &mut F) where F: FnMut(), { f(); } call(&change_string); call(&change_string); It will turn out an error: error[E0308]: mismatched types --> src/main.rs:10:10 | 10 |