closures

Passing parameters to function closure

淺唱寂寞╮ 提交于 2019-12-03 10:32:57
I'm trying to understand the difference in Go between creating an anonymous function which takes a parameter, versus having that function act as a closure. Here is an example of the difference. With parameter: func main() { done := make(chan bool, 1) go func(c chan bool) { time.Sleep(50 * time.Millisecond) c <- true }(done) <-done } As closure: func main() { done := make(chan bool, 1) go func() { time.Sleep(50 * time.Millisecond) done <- true }() <-done } My question is, when is the first form better than the second? Would you ever use a parameter for this kind of thing? The only time I can

perl closures and $_

倖福魔咒の 提交于 2019-12-03 10:18:31
One of the first things I try to learn in an unfamiliar programming language is how it handles closures. Their semantics are often intertwined with how the language handles scopes and various other tricky bits so understanding them reveals several other aspects of the language. Plus, closures are a really powerful construct and often times cut down on the amount of boilerplate I have to type. So I was messing around with perl closures and I stumbled upon a little gotcha: my @closures; foreach (1..3) { # create some closures push @closures, sub { say "I will remember $_"; }; } foreach (

Please explain closures, or binding the loop counter to the function scope

坚强是说给别人听的谎言 提交于 2019-12-03 09:55:51
问题 I've seen programmers assign events listeners inside loops, using the counter. I believe this is the syntax: for(var i=0; i < someArray.length; i++){ someArray[i].onclick = (function(i){/* Some code using i */})(i); } Could someone please explain the logic behind this, and this weird syntax, I've never seen this: (function(i))(i); Many thanks for your time and patience. 回答1: This is done because JavaScript only has function scope , not block scope. Hence, every variable you declare in a loop

When actually is a closure created?

我是研究僧i 提交于 2019-12-03 09:11:08
问题 Is it true that a closure is created in the following cases for foo , but not for bar ? Case 1: <script type="text/javascript"> function foo() { } </script> foo is a closure with a scope chain with only the global scope. Case 2: <script type="text/javascript"> var i = 1; function foo() { return i; } </script> same as Case 1. Case 3: <script type="text/javascript"> function Circle(r) { this.r = r; } Circle.prototype.foo = function() { return 3.1415 * this.r * this.r } </script> in this case,

JavaScript - How do I learn about “closures” usage?

£可爱£侵袭症+ 提交于 2019-12-03 08:51:46
问题 From Wikipedia, the free encyclopedia: Closure (computer science) In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables. The explicit use of closures is associated with functional programming and with languages such as ML and Lisp. Constructs such as objects in other languages can also be modeled with closures. To use this inside of JavaScript, can someone point me to an

Wait until an asynchronous api call is completed - Swift/IOS

谁说我不能喝 提交于 2019-12-03 08:20:30
I'm working on an ios app where in my appDelegate I have: func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool { self.api.signInWithToken(emailstring, token: authtokenstring) { (object: AnyObject?, error:String?) in if(object != nil){ self.user = object as? User // go straight to the home view if auth succeeded var rootViewController = self.window!.rootViewController as UINavigationController let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) var homeViewController = mainStoryboard

What is the difference between $.proxy() and bind()?

旧街凉风 提交于 2019-12-03 08:20:10
问题 In 2009, ECMAScript 5 added a built-in bind() function which takes an object as a parameter and returns an identical function in which this will always refer to the object you passed it. (I couldn't find anything that looked like a canonical documentation link.) How is this different from jQuery's $.proxy() function? Did $.proxy() come first before ECMAScript 5 was released? Is there a particular reason to favor $.proxy(function(){}, this) over function(){}.bind(this) ? 回答1: proxy came first

How are environments, (en)closures, and frames related?

六月ゝ 毕业季﹏ 提交于 2019-12-03 08:13:07
问题 I want to better understand how environments, closures, and frames are related. I understand function closures contain an environment, environments contain a frame and an enclosure, and frames contain variables, but I'm a bit fuzzy on how they interact with one another. Perhaps an example of what's going on during a function call would help? Or maybe a diagram? 回答1: UPDATE R-lang defines an environment as having a frame . I tend to think about frames as stack frames , not as mapping from name

Pass method as parameter in Groovy

依然范特西╮ 提交于 2019-12-03 08:11:01
问题 Is there a way to pass a method as a parameter in Groovy without wrapping it in a closure? It seems to work with functions, but not methods. For instance, given the following: def foo(Closure c) { c(arg1: "baz", arg2:"qux") } def bar(Map args) { println('arg1: ' + args['arg1']) println('arg2: ' + args['arg2']) } This works: foo(bar) But if bar is a method in a class: class Quux { def foo(Closure c) { c(arg1: "baz", arg2:"qux") } def bar(Map args) { println('arg1: ' + args['arg1']) println(

Why can't I roll a loop in Javascript?

…衆ロ難τιáo~ 提交于 2019-12-03 07:41:25
I am working on a web page that uses dojo and has a number (6 in my test case, but variable in general) of project widgets on it. I'm invoking dojo.addOnLoad(init), and in my init() function I have these lines: dojo.connect(dijit.byId("project" + 0).InputNode, "onChange", function() {makeMatch(0);}); dojo.connect(dijit.byId("project" + 1).InputNode, "onChange", function() {makeMatch(1);}); dojo.connect(dijit.byId("project" + 2).InputNode, "onChange", function() {makeMatch(2);}); dojo.connect(dijit.byId("project" + 3).InputNode, "onChange", function() {makeMatch(3);}); dojo.connect(dijit.byId(