closures

Please explain the use of JavaScript closures in loops

I have read a number of explanations about closures and closures inside loops. I have a hard time understanding the concept. I have this code: Is there a way to reduce the code as much as possible so the concept of closure can be made clearer. I am having a hard time understanding the part in which the i is inside two parenthesis. Thanks...

Adding an rails activerecord association within a loop

I want to add a has_many through association to a activerecord model class for each symbol in an array. for example PeopleOrganisation::ROLES.each do |role| has_many role.to_s.pluralize.to_sym, :through => :people_organisations, :source => :person, :conditions => "people_organisations.role = '#{role.to_s}'" do def << (ob...

PHP providing a closure to determine uniqueness of an item in an array

Quick question: is there a way to provide a closure in PHP to some equivalent function to the array_unique function so that you can specify your own comparison closure to be used when comparing two items in the array? I have an array of class instances (which may contain duplicates) and want to tell PHP to use particular logic to determi...

How do I use closures to access twitters @Anywhere javascript api?

I want to do something that in a classical object oriented language like Java, C# etc. is very easy to do. I simply want to access a property of an instantiated object. The object is globally scoped in the browser's window object, and provided by the twitter @anywhere API. For my code examples, assume you have already logged the user ...

string functions (subStr) not found in XMLHttpRequest event handler

I am have a problem with creating a server push network object. Because Firefox and chrome handle server push differently, (Firefox get onload events, chrome uses onprogress /broken ). For chrome I need to capture the onprogress events and then mask off the previous data from the responseText. I want to pass just the new data to chrome. ...

php closure (Anonymous function)

A php closure or anonymous function is used to create function without specifying its name. Is it possible to call them without assigning to identifier as we do in JavaScript ? e.g. (function(){ echo('anonymous function'); })(); What is the correct use of use construct while defining anonymous function and what is the status of a...

why is a captured variable not holding a reference to the instance of an object

As the code shows below, I'm creating a thread in a foreach loop and running them at a later time, however when I run the thread I get the "object reference not set to an instance of an object" error. I suspect this is a closure problem, but it seems like i'm doing everything i should be to avoid that here by creating a local copy of th...

C++ closure hack

is there any problem with such closure implementation (stolen from python hack) void function(int value) { struct closure { closure(int v = value) : value_(value) {} private: int value_; }; closure c; } upon further investigation, it appears in member functions, local variables can not be used as default va...

using _gaq asynch in closures

When using var _gaq = _gaq || []; within a script tag what would be needed to support this in a closure to add analytics async requests. i.e. experiment = (function(){ var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXX-X']); _gaq.push(['_trackPageview']); var nobody_knows_this_var_...

How do I use an anonymous function within a class method in Python (closure) ?

class Test: def somemethod(self): def write(): print 'hello' write() x = Test() x.somemethod() write() is a function that will be used several times through somemethod(). somemethod() is the only function within the class that will require it's use so it seems silly to define it outside of somemethod(...

closures handled differently on desktop browser and mobile safari?

Here's a simple javascript program: var $d = {}; var AudioPlayer = function(filename, timeUpdateCallback, playbackDone){ // An HTML5 audio player is defined here. } $d.AudioPlayer = AudioPlayer; var AudioManager = function(chapterId){ var audioPlayer; var me=this; this.next = function(){ ... ...

jQuery: Is there any reason not to store DOM elements in a variable instead of attaching them to the DOM?

I've got a plug-in I'm working on which fetches a portion of a remote page, and saves that portion to be used in a jQuery UI dialog widget; originally I was attaching the HTML to the DOM and hiding it with display:none until it was called with $.dialog but I realized I could just leave the DOM node in a variable. Is there any reason no...

JavaScript scoping w/ closure: help me understand

Running the following code: for (var i=0; i<3; i++) { setTimeout( function() { console.log(i); } , 500 ); } Outputs "3" three times. It's outputting the final value of i as opposed to the value of i when the inner function is created. If I want the output to be 1, 2, and 3, how would I write this code? How can I get it to use th...

Closures and dynamic scope?

I think I understand why there is a danger in allowing closures in a language using dynamic scope. That is, it seems you will be able to close the variable OK, but when trying to read it you will only get the value at the top of global stack. This might be dangerous if other functions use same name in the interim. Have I missed some o...

How to execute functions inside jquery document ready function closure, from a global context

Hi, I want to execute a function that is inside the jquery document ready closure. But I want to execute it from a global context. e.g. $("document").ready(function () { function myFunction() { alert("test"); } }); Is there some sort of "path" syntax I can write, to get inside the doc ready closure? e.g. Here is som...

Groovy - Closures and bind, why this code doesn't work ?

I think this is a newbie question but why it fails on the last assertion? I was thinking that the closure binds the value on it, so changing it from the closure will change the value outside the closure. def value = 5 def foo(n){ return { ++n } } def test = foo(value) assert test() == 6 assert test() == 7 assert value == ...

[Ruby] How to access the parent collection in a each closure?

I'm looking from something that to replace the *parent*. %w[apple apples].each do |w| next if *parent*.includes? w + "s" puts w end # output: apples ...

puzzle: value in clousure is changed.

I use http://tile.cloudmade.com/wml/latest/web-maps-lite.js to geocode. There is a address array containing around 20 addresess addresses[n] = {where:where,who:who,contact:contact,note:note,type:type}; Then I loop the array to geocode for (var i = 0; i < addresses.length; i++) { geocoder2.getLocations(addresses[i].where, functio...

very simple groovy, map and closure question, but what I'm doing wrong.....

Hi, This is OK def variables=[ ['var1':'test1'], ['var2':'test2'], ['var3':'test3'] ] println "${variables.size()}" variables.each{entry -> println "${entry} " } I got: 3 [var1:test1] [var2:test2] [var3:test3] but this caused problems def variables=[ ...

Accessing private variables from within a closure.

I'm trying to reference a private variable of an object from within a closure. The code below would seem to work, but it complains Fatal error: Cannot access self:: when no class scope is active in test.php on line 12 and Fatal error: Using $this when not in object context in test.php on line 20. Any ideas how to accomplish the same res...