closures

Self-modifying code in Ruby

左心房为你撑大大i 提交于 2019-12-24 15:11:40
问题 I am concerned about writing self-modifying code in Ruby. And by self-modifying, I mean being able to write functions that take a code block as an input value, and output another code block based on this. (I am not asking about basics such as redefining methods at runtime.) What I might want to do is, for example, having the following block, _x_ = lambda { |a, b, c, d| b + c } one can notice that arguments a and d are not used in the body at all, so I would like a function eg. #strip to

Understanding Closures [duplicate]

江枫思渺然 提交于 2019-12-24 12:22:24
问题 This question already has answers here : How do JavaScript closures work? (86 answers) Closed 4 years ago . I was going through an Advanced JavaScript text and came across these codes, I think the writer was trying to demonstrate closures but didn't explain the codes. I've been staring at these sets of codes for hours and still can't figure out why they will produce different output. for (var i = 0; i <= 2000; i += 1000) { setTimeout(function () { console.log('i value in closure is ' + i); },

unexpected non-void return value in void function

筅森魡賤 提交于 2019-12-24 12:18:56
问题 Below is my code. I'm learning closures. I'm getting an error that my function would not return a value. Can someone help? func operationOnNumbers(_ a: Int, _ b: Int, operation: (Int, Int) -> Int) { let result = operation(a, b) print(result) return result } let addClosure = {(a: Int, b: Int) in a + b } operationOnNumbers(5, 7, operation: addClosure) 回答1: Use this modified code as you have missed return type in the function ( -> Int) func operationOnNumbers(_ a: Int, _ b: Int, operation: (Int,

setTimeout only sees last value in array? [duplicate]

女生的网名这么多〃 提交于 2019-12-24 12:17:38
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Javascript closure inside loops - simple practical example How do I pass the value (not the reference) of a JS variable to a function? Why always the last reference to the object is used in loop? I have an array of ids which I loop over and want to use in a function called by setTimeout, however when "func" below is executed it only seems to see the last id stored in the array. I have been trying to uses

How to refactor duplication out of closures that are an argument to a method call in Groovy?

社会主义新天地 提交于 2019-12-24 12:01:57
问题 Consider this real world example, which is code written to make 'GET' and 'POST' calls to the same REST service endpoint via the Rest Client Builder (Grails plugin). I dislike the duplication in that the header and content type setup is identical, but am not sure how to refactor the common pieces out given that they are calling methods on the closure that is being passed to the get() or post() method call. Please provide a concrete example of a good refactoring out of the duplication in your

Overriding document.write within a specified scope?

。_饼干妹妹 提交于 2019-12-24 10:57:53
问题 I need to override document.write in order to render an ad that comes back wrapped in that function from google's doubleclick. Since the standard document.write replaces the page content entirely, i overrode it to append the contents of the ad to an existing element. However, I want to make sure that this override only occurs during the ad render and that any other document.writes that may be on the page are unaffected. Can this be scoped or accomplished via closure? I'm not very familiar

Why use closure in that simple case?

泪湿孤枕 提交于 2019-12-24 10:47:49
问题 In this article http://howtonode.org/why-use-closure an example is given : function greeter(name, age) { var message = name + ", who is " + age + " years old, says hi!"; return function greet() { console.log(message); }; } // Generate the closure var bobGreeter = greeter("Bob", 47); // Use the closure bobGreeter(); Why would it be more worth than function greeter(name, age) { var message = name + ", who is " + age + " years old, says hi!"; console.log(message); } greeter("Bob", 47); which is

'Cannot convert the expression's type' with optional in closure body

北慕城南 提交于 2019-12-24 10:47:46
问题 I have come across (most definitely) a feature that I don't quite understand. I have a closure, that takes in a Float and has no return value, aka (number: Float) -> Void . In that closure, I perform a call on optional value, not forcing its unwrapping, because I like the behavior since the optional is a delegate (= if it's nil, don't do anything). But when I do that, I get an error: Cannot convert the expression's type '...' to type Float. Interestingly enough, when I simply add println() ,

Variable outside of callback in NodeJS

徘徊边缘 提交于 2019-12-24 10:36:45
问题 I am fairly new to NodeJS and to JavaScript in general. Here is my script: var data = []; client.query( 'SELECT * FROM cds', function selectCb(err, results, fields) { if (err) { throw err; } console.log(results); console.log(fields); data.push(results); } ); console.log(data); How can I get access to the results (or data ) var outside of the callback? I don't want to have to write a ton of callbacks inside of each other when running queries. 回答1: What you're asking for is synchronous (or

Releasing closures

狂风中的少年 提交于 2019-12-24 08:22:40
问题 On a nodemcu, I'm using a closure to send a file over a socket, like this: function sendfile(sock, name) local fd = file.open(name, "r") function sendchunk() local data = fd:read() if data then sock:send(data) else fd:close() sock:close() end end sock:on("sent", sendchunk) sendchunk() end After transferring a few files, the interpreter panics due to "not enough memory". I can imagine this may be due to closure still hanging around. It would be difficult for the garbage collector to determine