closures

Access a copied integer variable in javascript anonymous method

安稳与你 提交于 2019-12-08 17:30:39
问题 I am a C# developer and used to the way closures work in C#. Currently I have to work with anonymous javascript functions and experience a problem with the following snippet: function ClosureTest() { var funcArray = new Array(); var i = 0; while (i < 2) { var contextCopy = i; funcArray[i] = function() { alert(contextCopy); return false; }; i++; } funcArray[0](); funcArray[1](); } I expect the first funcArray() call to say 0 and the second to say 1 . However, they both say 1 . How is that

Adding Swift Closures as values to Swift dictionary

半腔热情 提交于 2019-12-08 17:19:05
问题 I want to create a Swift dictionary that holds String type as its keys and Closures as its values. Following is the code that I have but it gives me the error: '@lvalue is not identical to '(String, () -> Void)' class CommandResolver { private var commandDict:[String : () -> Void]! init() { self.setUpCommandDict(); } func setUpCommandDict() { self.commandDict["OpenAssessment_1"] = { println("I am inside closure"); } } } I tried looking at other question on StackOverflow regarding closures in

Is promise a closure?

六月ゝ 毕业季﹏ 提交于 2019-12-08 15:47:19
问题 In closure tag wiki page, it reads "jQuery itself is one big closure." But is promise a closure as well? Could you please explain why or why not? This is how I understand closure: assign a function to a variable and reuse it with different environments. Promise does that with $.ajax() , but I could not find anywhere in stackoverflow where promise is introduced as a closure. Maybe because there are other features of promise like $.Deferred() , resolve() , and fail() to expand its functionality

JavaScript: default arguments in automatic getters and setters in closures without eval?

那年仲夏 提交于 2019-12-08 15:42:39
Note : this question is a follow up of the recently asked question JavaScript: automatic getters and setters in closures without eval? . The gist of that question was as follows: "How can one automatically provide getters and setters for scoped variables in a closure - without the use of the eval statement". There the poster, provided code demonstrating how to do so with eval and the user gave the following answer which does not require eval : function myClosure() { var instance = {}; var args = Array.prototype.slice.call(arguments); args.forEach(function(arg) { instance[arg] = function(d) {

Anonymous closure can not be used inside a closure that has explicit arguments

痞子三分冷 提交于 2019-12-08 15:40:13
问题 Can smb explain what is the problem, how should I modify my code? I need to filter CKRecord s returned from CloudKit . override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { let defaultContainer = CKContainer.defaultContainer() let publicDatabase = defaultContainer.publicCloudDatabase let myfunc2 = myfunc(names, { (records: [CKRecord], error: NSError) in if error == nil { let records2 = records.filter(

PHP closure as an optional function argument

徘徊边缘 提交于 2019-12-08 14:56:03
问题 Would be possible to specify a default argument value when argument is a PHP closure? Like: public function getCollection($filter = function($e) { return $e; }) { // Stuff } Am i missing something (maybe a different syntax?) or it's not possible at all? Of course i know i can do: public function getCollection($filter = null) { $filter = is_callable($filter) ? $filter : function($e) { return $e; }; // Stuff } ( NOTE: I didn't test the above code) 回答1: Default arguments can only be "scalar

How can you capture multiple arguments weakly in a Swift closure?

南楼画角 提交于 2019-12-08 14:30:19
问题 Is there a way to capture multiple arguments weakly in a swift closure? I know this is the syntax to capture one argument weakly: { [weak arg] arg.doSomething() } How can I do this for two objects that I wish to capture weakly? 回答1: From Expressions in "The Swift Programming Language" (emphasis added): Closure Expression ... A closure expression can explicitly specify the values that it captures from the surrounding scope using a capture list. A capture list is written as a comma separated

Swift 3.0 closure expression: what if the variadic parameters not at the last place in the parameters list?

对着背影说爱祢 提交于 2019-12-08 14:16:25
Update at 2016.09.19 There is a tricky, indirect way to use variadic parameters before some other parameters in closure expression parameters list, haha let testClosure = { (scores: Int...) -> (_ name: String) -> String in return { name in return "Happy" } } let k = testClosure(1, 2, 3)("John") And I found some related issues in bugs.swift.org: SR-2475 SR-494 Original Post According to the document of Swift 3.0 , for a closure expression, "variadic parameters can be used if you name the variadic parameter"(see Closure Expresssion Syntax part). But for Swift 2.x, the description is "Variadic

How to use an Swift object method as a closure?

∥☆過路亽.° 提交于 2019-12-08 12:52:47
问题 I'd like to use an object method as a closure because I need to reuse the same closure multiple times in different places in an object. Let's say I have the following: class A { func launch(code: Int) -> Bool { return false } } And I need a closure that is of type Int -> Bool in the same object. How would I be able to use the launch method as the closure? I'd rather not do something like { self.launch($0) } if I can just directly reference the method. 回答1: Instance methods are curried

C++0x closures / lambdas example

别等时光非礼了梦想. 提交于 2019-12-08 11:03:57
问题 I am attempting to leverage C++0x closures to make the control flow between a custom lexer and parser more straightforward. Without closures, I have the following arrangement: //-------- // lexer.h class Lexer { public: struct Token { int type; QString lexeme; } struct Callback { virtual int processToken(const Token &token) = 0; }; Lexer(); int tokenize(const QList<Token> &patterns, QTextStream &stream, Callback *callback); }; //------------- // foo_parser.h class FooParser: public Lexer: