closures

Python closure function losing outer variable access

与世无争的帅哥 提交于 2019-12-21 07:28:31
问题 I just learned python @ decorator, it's cool, but soon I found my modified code coming out weird problems. def with_wrapper(param1): def dummy_wrapper(fn): print param1 param1 = 'new' fn(param1) return dummy_wrapper def dummy(): @with_wrapper('param1') def implementation(param2): print param2 dummy() I debug it, it throws out exception at print param1 UnboundLocalError: local variable 'param1' referenced before assignment If I remove param1 = 'new' this line, without any modify operation

How can I use PHP 5.3 Closures like We use Blocks in Ruby

放肆的年华 提交于 2019-12-21 05:01:07
问题 How can I use PHP 5.3 Closures like We use Blocks in Ruby. I never used 'for' Loop in Ruby due to using Blocks with 'each' 'find_all' 'inject' Methods. How can I use PHP 5.3 Closures like Ruby Blocks and say bye-bye to 'for' Loops :) Like Between { and } is a Closure(or Block or Anonymous Function) fruit = %w[apple banana orange] fruit.each { |f| print "#{f}, " } I do it in PHP this way, $fruit = array('apple', 'banana', 'orange'); foreach ($fruit as $f) { print "$f, "; } Is there a way to do

Swift closure as values in Dictionary

五迷三道 提交于 2019-12-21 04:15:56
问题 I'm trying to use an Objective-C library which expects a NSDictionary as its return type. Within the NSDictionary , I can return values of any type, including blocks. I cannot figure out if there is a way to write an analogous swift method that returns a Dictionary with a closure or a string as a possible value type. I can't use AnyObject as the value type for the dictionary so this doesn't work: Dictionary<String,AnyObject> = ["Key":{(value:AnyObject) -> String in return value.description] I

Swift closure as values in Dictionary

最后都变了- 提交于 2019-12-21 04:15:01
问题 I'm trying to use an Objective-C library which expects a NSDictionary as its return type. Within the NSDictionary , I can return values of any type, including blocks. I cannot figure out if there is a way to write an analogous swift method that returns a Dictionary with a closure or a string as a possible value type. I can't use AnyObject as the value type for the dictionary so this doesn't work: Dictionary<String,AnyObject> = ["Key":{(value:AnyObject) -> String in return value.description] I

Groovy - closures vs methods - the difference

流过昼夜 提交于 2019-12-21 03:52:51
问题 If you look very carefully at the picture included, you will notice that you can refactor Groovy code using the Eclipse IDE and convert a method to a closure and vice versa. So, what exactly is a closure again and how is it different than a method? Can someone give a good example of using a closure as well as why it's a useful feature? Anonymous inner classes weren't good enough? 回答1: Closure is a Closure class instance , that implements Call logic. It may be passed as argument or assigned to

What does event binding mean?

北慕城南 提交于 2019-12-21 03:32:26
问题 What does event binding mean? I always come across this word whenever I search around the internet and whatever I try to look for the meaning, it's still vague to me @_@ A while ago, while reading some blogs regarding JavaScript I see people using this sacred word that I cannot grasp. 回答1: Event binding refers to telling the browser that a particular function should be called whenever some 'event' occurs. Events mostly relate to user input, such as clicks. An example of binding to an event in

Passing parameters to function closure

百般思念 提交于 2019-12-21 03:30:42
问题 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

How to make block local variables the default in ruby 1.9?

久未见 提交于 2019-12-21 02:50:09
问题 Ruby 1.9 gives the ability to define variables that are just local to a block and do not close over variables of the same name in an outer scope: x = 10 proc { |;x| x = 20 }.call x #=> 10 I would like to have this behaviour as default for some blocks I define - without having to use the |;x, y, z| syntax (note the semicolon). I do not think Ruby allows this natively but is it possible to hack this functionality? I have one solution currently but it's quite ugly as it requires checking to see

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

試著忘記壹切 提交于 2019-12-21 02:46:45
问题 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 =

Powershell: Use a variable to reference a property of $_ in a script block

与世无争的帅哥 提交于 2019-12-21 02:46:18
问题 $var =@( @{id="1"; name="abc"; age="1"; }, @{id="2"; name="def"; age="2"; } ); $properties = @("ID","Name","Age") ; $format = @(); foreach ($p in $properties) { $format += @{label=$p ; Expression = {$_.$p}} #$_.$p is not working! } $var |% { [PSCustomObject]$_ } | ft $format In the above example, I want to access each object's property through a variable name. But it cannot work as expected. So in my case, how to make Expression = {$_.$p} working? 回答1: The OP's code and this answer use PSv3+