closures

Problem with assigning delegates in for-loop

Hi. I have an application that is capable of plugins (MEF). The plugins are WPF UserControls that import services. The user can select the wanted plugin from the main menu of the application. To do this, i use the following loop: foreach(IToolPlugin Plugin in ToolPlugins) { Plugin.Init(); MenuItem PluginMenuItem = Plugin.MenuI...

What is a full powered closure?

I was at a Java conference on Scala the other day and the speaker referred to 'full powered closures'. I am having a hard time nailing down a definition that makes sense to me. I have read the wiki page on closures but it really didn't answer it for me. Can someone help me with a clear cut definition? Maybe even include a simple examp...

Javascript closures - variable scope question

I'm reading the Mozilla developer's site on closures, and I noticed in their example for common mistakes, they had this code: <p id="help">Helpful notes will appear here</p> <p>E-mail: <input type="text" id="email" name="email"></p> <p>Name: <input type="text" id="name" name="name"></p> <p>Age: <input type="text" id="age" name="ag...

Why aren't these two bits of JavaScript equivalent?

in jquery 1.4.2, ff 3.6.6: The following code produces three divs, which write messages to the firebug console as you would expect. However, if you uncomment out the loop and comment out the 3 lines doing it manually, it doesn't work - mousing over any of the divs results in "three" being written to the console. Why are these two me...

Why do we have closures in JavaScript?

Am wrapping my head around JavaScript closures and am at a point where things are falling in place; i.e a closure is the local variables for a function - kept alive after the function has returned, or a closure is a stack-frame which is not deallocated when the function returns. Am starting to understand this concept, but the more i und...

javascript timer or intervals created in loop using closure

I'm using jQuery to setup a timer or interval loop on a few elements to check them every couple seconds. I've tried setting a timer and checking if I should restart it, or setting and interval and checking if I should stop it. Although simplified, this is basically what I need: var mytimers = new Array(); $('div.items').each(function(...

Closure Definition and Example

Hello to all, what is closure and its correspondence example ? I have research a lot and could not understand. Please explain in generic programming language concept aspect and specific programming language aspect. Please help. Thanks. ...

Google Maps v3 opens the last infoWindow when a marker is clicked

I have a shared infoWindow for all my markers. It works good if I use jquery's $().each(function(){}), but if I change it to JavaScrips's native for or while loop, it's not working as expected. Whenever I click a marker, it will open the last populated marker's infoWindow, instead of the clicked marker's infoWindow. This works: $(sto...

Google Closure Editor/WYSIWYG

Does anyone have experience with Google Closure Editor/WYSIWYG? I'm thinking of moving from CKEDITOR to Google Closure Editor/WYSIWYG. Ideally I'd love to use the etherpad editor but it doesn't appear that anyone has separated the editor from all the app. Anyhow, for Google Closure Editor/WYSIWYG, does anyone know, does it support the r...

Why doesn't this closure have access to the 'this' keyword? - jQuery

I'm a beginner to closures (and Javscript in general), and I can't find a satisfactory explanation as to what's going on in this code: function myObject(){ this.myHello = "hello"; this.myMethod = do_stuff; } function do_stuff(){ var myThis = this; $.get('http://example.com', function(){ alert(this.myHello); ...

How can I use PHP 5.3 Closures like We use Blocks in Ruby

How can I use PHP 5.3 Closures like We use Blocks in Ruby. I never used 'for' Loop in Ruby due to using Blocks with 'each' 'find_all' 'inject' Methods. How can I use PHP 5.3 Closures like Ruby Blocks and say bye-bye to 'for' Loops :) Like Between { and } is a Closure(or Block or Anonymous Function) fruit = %w[apple banana orange] frui...

Emulate ruby's inject() behavior in PHP

From this question here, I was writing an enum wrapper to have some methods that can be used with lambdas to somewhat emulate ruby's usage of blocks in enums. class enum { public $arr; function __construct($array) { $this->arr = $array; } function each($lambda) { array_walk($this->arr, $lambda); } ...

Why does one Javascript closure work and the other doesn't?

There are two versions, supposedly when the user click the first link, it will alert "1", and the second link, "2", etc: Version 1: <a href="#" id="link1">click me</a> <a href="#" id="link2">click me</a> <a href="#" id="link3">click me</a> <a href="#" id="link4">click me</a> <a href="#" id="link5">click me</a> <script type="text/javas...

jquery: context with closures?

Hi there! I got a simple question about jQuery but rather javascript approaches in general... Is it ok, to do this? : this._checkedTexts.length = 0; // <- array belonging to "me" var contextCheckedTexts = this._checkedTexts; $('#valueContainer input:checked').each(function() { contextCheckedTexts.push($(this).text()); }); Since '...

What's the difference between closures and traditional classes?

What are the pros and cons of closures against classes, and viceversa? Edit: As user Faisal put it, both closures and classes can be used to "describe an entity that maintains and manipulates state", so closures provide a way to program in an object oriented way using functional languages. Like most programmers, I'm more familiar with ...

How to import a closure from groovy to java?

I'm trying to extract a closure from a groovy script. I define the closure as def printMe = {str ->println str} in my groovy file, and then try to use it by grabbing it from the binding as follows: GroovyScriptEngine gse = new GroovyScriptEngine(new String[] { "scripts" }); Binding binding = new Binding(); gse.run("test.groovy", bin...

(Python) Closure created when it wasn't expected

I got an unexpected closure when creating a nested class. I suspect that this is something related to metaclasses, super, or both. It is definitely related to how closures get created. I am using python2.7. Here are five simplified examples that demonstrate the same problem that I am seeing (they all build off the first): EXAMPLE ...

What is the search order for free variables in Python?

Specifically, how are free variables bound at definition for methods of a class? It is probably something like this: enclosing function (temporary) scope => generate closure global (permanent) scope => generate no closure (just look it up when the method body executes) raise UnboundLocalError() Here are two examples: globalname = 0...

Finding a function's parameters in Python

I want to be able to ask a class's __init__ method what it's parameters are. The straightforward approach is the following: cls.__init__.__func__.__code__.co_varnames[:code.co_argcount] However, that won't work if the class has any decorators. It will give the parameter list for the function returned by the decorator. I want to get...

Can someone please explain this bit of Python code?

I started working in Python just recently and haven't fully learned all the nuts and bolts of it, but recently I came across this post that explains why python has closures, in there, there is a sample code that goes like this: y = 0 def foo(): x = [0] def bar(): print x[0], y def change(z): global y ...