closures

Binding click event handlers in a loop causing problems in jQuery

血红的双手。 提交于 2019-12-01 20:50:43
I am trying to run the following code: I pass parameter to a function, but it always has the value of the last object run through the loop. I read some articles about it on stackoverflow, but I couldn't find out how to make it run in my solution. The object is a JSON object returned from the server. All it's values are correct. for(var i = 0;i<parents.length;i++){ var row = $(document.createElement("tr")); var colName = $(document.createElement("td")); var aDisplayCol = $(document.createElement("a")); var parentId = parents[i]['PARENT_ID']; aDisplayCol.attr("href","#").html(parents[i]['NAME'])

How to return an outer function inside an asynchronous inner function in swift?

为君一笑 提交于 2019-12-01 20:46:13
问题 I am creating a swift class which contain of a function which validate if the user is true. However, the userVerifyResult will always return "false" even if the result is true due to dataTaskWithRequest function is executed asynchronously. How can I get around with that? class userLibService: NSObject{ func VerifyUser() -> String{ var userVerifyResult = "false" var url = NSURL(string: "http://www.example.com/test.php")! var request = NSMutableURLRequest(URL: url) request.HTTPMethod = "POST"

Undefined Behavior with the C++0x Closure: I

為{幸葍}努か 提交于 2019-12-01 20:09:19
Consider the example: #include <iostream> #include <functional> // std::function #include <vector> // std::vector #include <algorithm> // std::for_each int main(){ auto adder = [](int x) { return [&](int y) { return x+=y; }; }; std::vector < std::function<int(int)> > vec; vec.push_back(adder(1)); vec.push_back(adder(10)); std::for_each(vec.begin(), vec.end(), [](std::function<int(int)> f){std::cout << f(33) << " ";}); std::cout << std::endl; } One expects the integers 34 and 43 43 and 76 , but instead gcc 4.6.0 produces "internal compiler error: Segmentation fault". What is wrong with the code

swift maximum consecutive positive numbers

左心房为你撑大大i 提交于 2019-12-01 19:53:21
How to count maximum consecutive positive numbers using closures? var numbers = [1,3,4,-1,-2,5,2,-2,-3,-4,5] //in this case it should be 3 print(numbers.reduce(0, { $1 > 0 ? $0 + 1 : $0 } ))//this counts total positive numbers Update: Simpler solution: Split the array into slices of positive elements, and determine the maximal slice length: let numbers = [1,3,4,-1,-2,5,2,-2,-3,-4,5] let maxConsecutive = numbers.split(whereSeparator: { $0 <= 0 }).map { $0.count }.max()! print(maxConsecutive) // 3 Old answer: ) Using the ideas from Swift running sum : let numbers = [1,3,4,-1,-2,5,2,-2,-3,-4,5]

modified closure warning in ReSharper

隐身守侯 提交于 2019-12-01 19:36:31
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 could go wrong. ReSharper tries to fix the problem by making a second variable that seems pointless to

Trouble with non-escaping closures in Swift 3

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 19:28:44
I have an extension Array in the form of: extension Array { private func someFunction(someClosure: (() -> Int)?) { // Do Something } func someOtherFunction(someOtherClosure: () -> Int) { someFunction(someClosure: someOtherClosure) } } But I'm getting the error: Passing non-escaping parameter 'someOtherClosure' to function expecting an @escaping closure . Both closures are indeed non-escaping (by default), and explicitly adding @noescape to someFunction yields a warning indicating that this is the default in Swift 3.1. Any idea why I'm getting this error? -- UPDATE -- Screenshot attached:

RelayCommand from lambda with constructor parameters

吃可爱长大的小学妹 提交于 2019-12-01 19:13:08
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) { field = p1; Command = new RelayCommand(() => DoIt(field)); } private DoIt(Foo p) { p.DoSomething(); }

How to pass additional variables in to underscores templates

女生的网名这么多〃 提交于 2019-12-01 19:10:25
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>' + this.model.get('searchTerm') + '</b>') Or if I could set searchTerm as a local variable in my render

Function pointers working as closures in C++

匆匆过客 提交于 2019-12-01 19:08:18
Is there a way in C++ to effectively create a closure which will be a function pointer? I am using the Gnu Scientific Library and I have to create a gsl_function . This function needs to effectively "close" a couple of parameters available when I create it. Is there a nice trick to create a closure so that I don't have to pass all of them as params in the gsl_function structure? If not, should I just pass in a pointer to an array containing these parameters? EDIT I have tried to use boost::bind like this: #include <gsl/gsl_integration.h> #include <boost/bind.hpp> #include "bondpricecalculator

Why is the value moved into the closure here rather than borrowed?

一笑奈何 提交于 2019-12-01 19:04:18
The Error Handling chapter of the Rust Book contains an example on how to use the combinators of Option and Result . A file is read and through application of a series of combinators the contents are parsed as an i32 and returned in a Result<i32, String> . Now, I got confused when I looked at the code. There, in one closure to an and_then a local String value is created an subsequently passed as a return value to another combinator. Here is the code example: use std::fs::File; use std::io::Read; use std::path::Path; fn file_double<P: AsRef<Path>>(file_path: P) -> Result<i32, String> { File: