closures

Is this object-lifetime-extending-closure a C# compiler bug?

ε祈祈猫儿з 提交于 2019-12-17 21:38:46
问题 I was answering a question about the possibility of closures (legitimately) extending object-lifetimes when I ran into some extremely curious code-gen on the part of the C# compiler (4.0 if that matters). The shortest repro I can find is the following: Create a lambda that captures a local while calling a static method of the containing type. Assign the generated delegate-reference to an instance field of the containing object. Result: The compiler creates a closure-object that references the

Coding clarity of closure for load event

心已入冬 提交于 2019-12-17 21:17:20
问题 Is there are clear way to write this closure for the load event on line #4: for i,item of m # add item once image loaded new_item = $("<img src='#{util.image_url(item, 'large')}' />") new_item.on 'load', ((item) => (=> @add_item(item)))(item) $("#preload-area").append(new_item) 回答1: You want a do loop: When using a JavaScript loop to generate functions, it's common to insert a closure wrapper in order to ensure that loop variables are closed over, and all the generated functions don't just

How to create a function that returns an existing promise instead of new promise?

自古美人都是妖i 提交于 2019-12-17 20:58:10
问题 JavaScript/Promise experts, I hope you can help me, because I don't understand how I can create a function that returns an existing promise instead of a new promise. I have done a lot of research, but all examples return a new promise every time the function is called. Assume I have a function that takes some time to process and generates a different result every time it is being called. When I use the new promise method then I still need to wait to complete every time and I don't get the

Javascript closure?

自闭症网瘾萝莉.ら 提交于 2019-12-17 20:33:37
问题 Method # 1 function transform(ar) { var alStr = []; for(var i=0; i<ar.length; i++) { alStr[i] = (function(v) { return (function() { return v; }); }(ar[i])); } return alStr; } var a = ["a", 24, { foo: "bar" }]; var b = transform(a); a[1]; b[1](); Method # 2 function transform(ar) { var alStr = []; for(var a in ar) { var O = function() { return a; } alStr.push(O); } return alStr; } var a = ["a", 24, { foo: "bar" }]; var b = transform(a); a[1]; b[1](); The above mentioned methods are used to

If the “with” statement in Javascript creates a new scope, why does this closure doesn't contain the new “x” in new scope each time?

不羁的心 提交于 2019-12-17 19:36:48
问题 If the with statement in Javascript creates a new scope, shouldn't clicking on the links show a different x which are in different scopes? It doesn't. <a href="#" id="link1">ha link 1</a> <a href="#" id="link2">ha link 2</a> <a href="#" id="link3">ha link 3</a> <a href="#" id="link4">ha link 4</a> <a href="#" id="link5">ha link 5</a> <script type="text/javascript"> for (i = 1; i <= 5; i++) { with({foo:"bar"}) { var x = i; document.getElementById('link' + i).onclick = function() { alert(x);

What does $0 represent in closures in Swift?

只谈情不闲聊 提交于 2019-12-17 19:06:50
问题 I have seen closures in swift have $0 inside of it and sometimes they use $1. What exactly is $0 and what are other $x can you use? Here are examples of it in use. applyMutliplication(2, {$0 * 3}) array.map({$0 + 1}) Thanks! 回答1: It's a shorthand argument name. From the Swift Book: “Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0, $1, $2, and so on.” — Apple Inc. “The Swift

In Python 2, how do I write to variable in the parent scope?

不羁的心 提交于 2019-12-17 17:33:22
问题 I have the following code inside a function: stored_blocks = {} def replace_blocks(m): block = m.group(0) block_hash = sha1(block) stored_blocks[block_hash] = block return '{{{%s}}}' % block_hash num_converted = 0 def convert_variables(m): name = m.group(1) num_converted += 1 return '<%%= %s %%>' % name fixed = MATCH_DECLARE_NEW.sub('', template) fixed = MATCH_PYTHON_BLOCK.sub(replace_blocks, fixed) fixed = MATCH_FORMAT.sub(convert_variables, fixed) Adding elements to stored_blocks works fine

Closure vs Anonymous function (difference?) [duplicate]

大兔子大兔子 提交于 2019-12-17 17:32:00
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: What is Closures/Lambda in PHP or Javascript in layman terms? What is the difference between a 'closure' and a 'lambda'? Hi, I have been unable to find a definition that clearly explains the differences between a closure and an anonymous function. Most references I have seen clearly specify that they are distinct "things" yet I can't seem to get my head around why. Could someone please simplify it for me? What

Javascript Reflection

笑着哭i 提交于 2019-12-17 17:30:30
问题 Is there a way to get all methods (private, privileged, or public) of a javascript object from within? Here's the sample object: var Test = function() { // private methods function testOne() {} function testTwo() {} function testThree() {} // public methods function getMethods() { for (i in this) { alert(i); // shows getMethods, but not private methods } } return { getMethods : getMethods } }(); // should return ['testOne', 'testTwo', 'testThree', 'getMethods'] Test.getMethods(); The current

Register multiple routes using range for loop slices/map

∥☆過路亽.° 提交于 2019-12-17 17:11:49
问题 Consider I have slice of string paths: paths := []string{"/path0", "/path1", "/path2" /*... "/path-n"*/ } // where n is the last path Using package net/http , I want to register handler for this path using for loop with range clause. This is how I do this: for _, path := range paths { http.HandleFunc(path, handler) } // in this case every handler is print the path to the console or to the browser EDIT: Basically the asker used this code: for _, path := range paths { http.HandleFunc(path, func