closures

Array with multiple Objects with expire-timers fails

百般思念 提交于 2019-12-03 21:06:42
I am making a game in HTML5, and now I just got a disturbing problem. In my game I have an Array with all the particles, all the particles have expire-timers, with different random generated delays. When the expire-timers expire they delete there own Object with the Array.splice() function, that causes trouble, since the Array.splice() function will mess up the order of the Array. First I had the function like this: (n = Blood particles to spawn, and x and y = starting point.) (All particles is spread random directions from the middle, and then they are decelerated by friction) function

How to copy a variable in JavaScript?

一世执手 提交于 2019-12-03 20:43:16
I have this JavaScript code: for (var idx in data) { var row = $("<tr></tr>"); row.click(function() { alert(idx); }); table.append(row); } So I'm looking through an array, dynamically creating rows (the part where I create the cells is omitted as it's not important). Important is that I create a new function which encloses the idx variable. However, idx is only a reference, so at the end of the loop, all rows have the same function and all alert the same value. One way I solve this at the moment is by doing this: function GetRowClickFunction(idx){ return function() { alert(idx); } } and in the

Writing my first DSL in C# and getting hung up on func<T> & Action

心已入冬 提交于 2019-12-03 19:34:50
问题 I'm taking a crack at writing my first DSL for a simple tool at work. I'm using the builder pattern to setup the complex parent object but am running into brick walls for building out the child collections of the parent object. Here's a sample: Use: var myMorningCoffee = Coffee.Make.WithCream().WithOuncesToServe(16); Sample with closure (I think that's what they're called): var myMorningCoffee = Coffee.Make.WithCream().PourIn( x => { x.ShotOfExpresso.AtTemperature(100); x.ShotOfExpresso

PHP closure scope problem

非 Y 不嫁゛ 提交于 2019-12-03 19:13:22
问题 Apparently $pid is out of scope here. Shouldn't it be "closed" in with the function? I'm fairly sure that is how closures work in javascript for example. According to some articles php closures are broken, so I cannot access this ? So how can $pid be accessed from this closure function? class MyClass { static function getHdvdsCol($pid) { $col = new PointColumn(); $col->key = $pid; $col->parser = function($row) { print $pid; // Undefined variable: pid }; return $col; } } $func = MyClass:

What are the use cases for closures/callback functions in JavaScript?

蹲街弑〆低调 提交于 2019-12-03 18:20:25
问题 I was listening to Crockford's talk on JavaScript closures and am convinced of the benefit of information hiding, but I do not have a firm understanding of when to use callback functions. It is mostly a true statement that a person could accomplish the same functionality with or without callbacks. As someone who is writing code, what heuristics or cues should I keep in mind when determining when to use callbacks/closures? I am not looking for the blanket statement 'Closures make more secure

.call() / .apply() with NO parameters VS simply calling a function with () parenthesis

删除回忆录丶 提交于 2019-12-03 18:04:10
问题 I've seen it done differently in code out there, but is there any benefit or reason to doing a (blank params) .call / .apply over a regular () function execution. This of course is an over-simplified example var func = function () { /* do whatever */ }; func.call(); func.apply(); VERSUS just the simple parenthesis. func(); Haven't seen any information on this anywhere, I know why call/apply are used when params are passed. 回答1: When you call a method with func(); , this variable inside the

Adding HTML to Drupal closure?

情到浓时终转凉″ 提交于 2019-12-03 16:52:56
To add javascript, you can use: drupal_add_js And similar for css: drupal_add_css But what if I just want to add html at the end of my page. I.e. Add a div with some text in it at the end of the page? A lot of suggestions here work if you want your alteration to be theme-based, but if you want this to come from a module, using a block region, page template or page prepocess won't cut it, because you're tying the alteration to the theme. From a modular standpoint, the following options are best: hook_footer() -- http://api.drupal.org/api/function/hook_footer/6 :: my_module_footer() should

Is it possible to create function-local closures pre-C++11?

烈酒焚心 提交于 2019-12-03 16:46:59
With C++11, we get lambdas, and the possibility to create functions/functors/closures on-the-fly where we actually need them, not somewhere where they don't really belong. In C++98/03, a nice way to make function-local functors/closures would've been the following: struct{ void operator()(int& item){ ++item; } }foo_functor; some_templated_func(some_args, foo_functor); Sadly, you can't use local types for templates (Visual Studio allows this with language extensions enabled). My train of though then went the following way: struct X{ static void functor(int& item){ ++item; } }; some_templated

Compose example in Paul Graham's ANSI Common Lisp

北慕城南 提交于 2019-12-03 16:27:01
问题 Can anybody explain an example in Paul Graham's ANSI Common Lisp page 110? The example try to explain the use &rest and lambda to create functional programming facilities. One of them is a function to compose functional arguments. I cannot find anything explaining how it worked. The code is as follows: (defun compose (&rest fns) (destructuring-bind (fn1 . rest) (reverse fns) #'(lambda (&rest args) (reduce #'(lambda (v f) (funcall f v)) rest :initial-value (apply fn1 args))))) The usage is:

Can't make weak reference to closure in Swift

怎甘沉沦 提交于 2019-12-03 16:25:17
问题 Update: I tried writing it without making it weak, and there doesn't seem to be a leak. So maybe the question is no longer necessary. In Objective-C ARC, when you want to have a closure be able to use itself inside of the closure, the block cannot capture a strong reference to itself, or it will be a retain cycle, so instead you can make the closure capture a weak reference to itself, like so: // This is a simplified example, but there are real uses of recursive closures int (^fib)(int); _