closures

Count functions calls with JavaScript

早过忘川 提交于 2019-12-20 03:07:20
问题 For example: I have a lot of functions and use them many times. I need to count calls for each function. What is the best practice to make it? At first i thought i need closures, but I can't implement it in a right way. 回答1: In the simplest case, you can decorate each function with a profiling wrapper: _calls = {} profile = function(fn) { return function() { _calls[fn.name] = (_calls[fn.name] || 0) + 1; return fn.apply(this, arguments); } } function foo() { bar() bar() } function bar() { }

Groovy subclass called superclass method that accesses closure

ⅰ亾dé卋堺 提交于 2019-12-20 02:24:46
问题 I have a groovy superclass that looks like: class AGroovyClass { private String str = "hello" void printString(int nTimes) { nTimes.times { println str } } } and subclass class AGroovySubclass extends AGroovyClass { // some other subclass methods } My client code calls: new AGroovySubclass().printString(5) And this actually breaks because it says that that there is no such property "str" for AGroovySubclass I would have thought since the printString method is in AGroovyClass, it should have

Is LINQifying my code worth accessing a foreach variable in a closure?

牧云@^-^@ 提交于 2019-12-20 02:11:53
问题 Reminiscent of the title of a bootleg live Rolling Stones recording of yesteryear, Resharper is sharper than I'll ever be; as I had it inspect my code, it told me this regarding closures: 1) "Loop: foreach (var item in PlatypiIds) { var query = db.Table<Locations>().Where(l => l.PlatypusId == item). Where(l=> l.SentTimeUTC >= EarliestToShow). Where(l=> l.SentTimeUTC <= LatestToShow). OrderBy(l => l.SentTimeUTC); if (query != null) { foreach (var q in query) { listLocs.Add(q); } } } ...can be

Inline if statement mutating inout parameter in a void return closure, weird error (Error: type 'Int1' does not conform to protocol 'BooleanType')

心不动则不痛 提交于 2019-12-20 01:46:19
问题 I've run into a somewhat weird (compile time) error that I cannot make any sense out of. The error is given for the following snippet: /* error: type 'Int1' does not conform to protocol 'BooleanType' */ let closure1 : (inout foo: Int) -> () = { foo -> () in (foo<0 ? (foo = -1) : (foo = 1)) } Error: type 'Int1' does not conform to protocol 'BooleanType' Note that Int 1 is not a typo here. Question 1: Why am I not allowed to use a single inline if statement (with result '()' ) as the implicit

Inline if statement mutating inout parameter in a void return closure, weird error (Error: type 'Int1' does not conform to protocol 'BooleanType')

妖精的绣舞 提交于 2019-12-20 01:46:05
问题 I've run into a somewhat weird (compile time) error that I cannot make any sense out of. The error is given for the following snippet: /* error: type 'Int1' does not conform to protocol 'BooleanType' */ let closure1 : (inout foo: Int) -> () = { foo -> () in (foo<0 ? (foo = -1) : (foo = 1)) } Error: type 'Int1' does not conform to protocol 'BooleanType' Note that Int 1 is not a typo here. Question 1: Why am I not allowed to use a single inline if statement (with result '()' ) as the implicit

Why does printing the owner of a closure in a string cause infinite recursion?

不想你离开。 提交于 2019-12-20 01:43:44
问题 I'm playing with closures and seeing this odd behavior that I can't quite explain: groovy:000> ({ println owner })() groovysh_evaluate@200b6145 ===> null groovy:000> ({ println "${owner}" })() groovysh_evaluate@2bf75a70 ===> null groovy:000> ({ ({ println owner })() })() groovysh_evaluate$_run_closure1@10f67a01 ===> null groovy:000> ({ ({ println "${owner}" })() })() ERROR java.lang.StackOverflowError: null at groovysh_evaluate$_run_closure1_closure2.doCall (groovysh_evaluate:2) at groovysh

modified closure warning in ReSharper

时光总嘲笑我的痴心妄想 提交于 2019-12-20 01:38:28
问题 I was hoping someone could explain to me what bad thing could happen in this code, which causes ReSharper to give an 'Access to modified closure' warning: bool result = true; foreach (string key in keys.TakeWhile(key => result)) { result = result && ContainsKey(key); } return result; Even if the code above seems safe, what bad things could happen in other 'modified closure' instances? I often see this warning as a result of using LINQ queries, and I tend to ignore it because I don't know what

Any better way to combine multiple callbacks?

≡放荡痞女 提交于 2019-12-20 01:09:16
问题 I need to call an async function (with loop) for multiple values and wait for those results. Right now I'm using the following code: (function(){ var when_done = function(r){ alert("Completed. Sum of lengths is: [" + r + "]"); }; // call when ready var datain = ['google','facebook','youtube','twitter']; // the data to be parsed var response = {pending:0, fordone:false, data:0}; // control object, "data" holds summed response lengths response.cb = function(){ // if there are pending requests,

How to pass additional variables in to underscores templates

大城市里の小女人 提交于 2019-12-19 21:52:26
问题 I've a backbone view that renders a search result in a underscore template. As I want to highlight the search term in the result I have the following print method in the template: print(someKey.replace(searchTerm, '<b>' + searchTerm + '</b>') It works as aspected, but I have to set the searchTerm variable in the global namespace to get this work. I wonder if there is a way to access my views model in the print method, so I could write it like this: print(someKey.replace(searchTerm, '<b>' +

RelayCommand from lambda with constructor parameters

时光总嘲笑我的痴心妄想 提交于 2019-12-19 21:48:32
问题 If, in a XAML file, I bind a Button to "Command" from the following class, then clicking the Button does not cause DoIt to be executed: class Thing() { public Thing(Foo p1) { Command = new RelayCommand(() => DoIt(p1)); } private DoIt(Foo p) { p.DoSomething(); } public ICommand Command { get; private set; } } However, it does work if I initialize a field from p1 and pass the field as a parameter to the method call inside the lambda: class Thing() { private Foo field; public Thing(Foo p1) {