closures

Is there any way to return from a function from inside a closure?

喜欢而已 提交于 2020-01-10 04:58:07
问题 I have the following simplified code: fn f() -> i32 { let a = some_result.unwrap_or_else(|_| { return 1; // want to return this value from f <------------- }); } I want to return the value 1 from the whole function f in this specific error case but I can't figure out how to do it from within a closure. If I instead use a match expression, it works fine as follows: fn f() -> i32 { let a = match some_result { Ok(result) => result, Err(_) => { return 1; }, }; } However, this makes the code

How to inspect bound closure variables in javascript?

浪尽此生 提交于 2020-01-09 12:52:10
问题 Suppose we do something like this (as part of the construction of a Javascript object): var foo = 3; this.method = function () { alert(foo); }; Now a closure will be generated to make sure foo is retained and available for use in method . Is there a way to do introspection on the current closure? What I'm looking for is a way to enumerate all the variables that are available inside method , which should include foo . Debug code like this would greatly help in debugging the binding of closures

How are closures implemented?

时光毁灭记忆、已成空白 提交于 2020-01-09 12:51:45
问题 "Learning Python, 4th Ed." mentions that: the enclosing scope variable is looked up when the nested functions are later called.. However, I thought that when a function exits, all of its local references disappear. def makeActions(): acts = [] for i in range(5): # Tries to remember each i acts.append(lambda x: i ** x) # All remember same last i! return acts makeActions()[n] is the same for every n because the variable i is somehow looked up at call time. How does Python look up this variable?

Closures in Java 7 [duplicate]

帅比萌擦擦* 提交于 2020-01-09 06:20:52
问题 This question already has answers here : Closure in Java 7 [closed] (7 answers) Closed 3 years ago . I have heard that closures could be introduced in the next Java standard that is scheduled to be released somewhere around next summer. What would this syntax look like? I read somewhere that introducing closures in java is a bigger change than generic was in java 5. Is this true? pros and cons? (By now we definitely know that closures not will be included in the next Java release) OR edit:

Closures in Java 7 [duplicate]

℡╲_俬逩灬. 提交于 2020-01-09 06:20:21
问题 This question already has answers here : Closure in Java 7 [closed] (7 answers) Closed 3 years ago . I have heard that closures could be introduced in the next Java standard that is scheduled to be released somewhere around next summer. What would this syntax look like? I read somewhere that introducing closures in java is a bigger change than generic was in java 5. Is this true? pros and cons? (By now we definitely know that closures not will be included in the next Java release) OR edit:

Closure in JavaScript - whats wrong?

核能气质少年 提交于 2020-01-08 13:25:12
问题 I trying to make next with closure: function func(number) { var result = number; var res = function(num) { return result + num; }; return res; } var result = func(2)(3)(4)(5)(3); console.log(result); // 17 I need to receive 2 + 3 + 4 + 5 + 3 = 17 But I got an error: Uncaught TypeError: number is not a function 回答1: You're misusing your functions. func(2) returns the res function. Calling that function with (3) returns the number 5 (via return result + num ). 5 is not a function, so (4) gives

How do I pass the value (not the reference) of a JS variable to a function? [duplicate]

自古美人都是妖i 提交于 2020-01-08 09:29:42
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 4 years ago . Here is a simplified version of something I'm trying to run: for (var i = 0; i < results.length; i++) { marker = results[i]; google.maps.event.addListener(marker, 'click', function() { change_selection(i); }); } but I'm finding that every listener uses the value of results.length (the value when the for loop terminates). How can I add listeners such that each

Add a return value to a closure [duplicate]

孤街醉人 提交于 2020-01-07 09:37:29
问题 This question already has answers here : Run code only after asynchronous function finishes executing (2 answers) Closed 2 years ago . I'm not very familiar with closure. I'm using this function to download a JSON file from a remote server requestJson(){ // Asynchronous Http call to your api url, using NSURLSession: NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: "http://api.site.com/json")!, completionHandler: { (data, response, error) -> Void in // Check if data was received

javascript closure in loop

时光总嘲笑我的痴心妄想 提交于 2020-01-07 07:10:27
问题 Following code is given: var a = [ ], i = 0, j = 0; for (i = 0; i < 5; i += 1) { (function(c) { a.push(function () { console.log(c); }); })(i); }; for (j = 0; j < 5; j += 1) { a[j](); } Why does i always get bigger by 1 instead of staying 5 ? Hasn't the foor loop already been passed, so the i parameter given to the anonymous function should be 5 ? 回答1: If you referenced i from the inner closure then yes, you would see the result being 5 in all cases. However, you pass i by value to the outer

What happens with values in closure? JavaScript

你说的曾经没有我的故事 提交于 2020-01-07 05:33:42
问题 I've seen a really cool explanation of closure. So in my view closure is a way to store data somewhere. Let's see examples of closure and example without closure: Without closure: function F1(x,y) { var z=5; function F2(){ console.log('x='+x+'y='+y+'z='+z); } F2(); } F1(1,2) With closure: function F1(x,y) { var z=5; function F2(){ console.log('x='+x+'y='+y+'z='+z); } return F2(); } var p=F1(1,2) p(); I've understood that in closure when F1() finishes its work, then F1() clears connection to