closures

Reference Instance Variables in Javascript Constructor

I'm trying to maintain state on an object by doing something like this: obj = function() { this.foo = undefined; this.changeState = function () { (function () { this.foo = "bar" })(); // This is contrived, but same idea. }; }; I want to set the instance variable foo to "bar" when I call the changeState method. ...

Invoke method as closure

Hi, My understanding of the Groovy operator .& is that it converts a method call to a closure. Therefore it seems like the following code (which can be run in the Groovy console) should work: class Foo { def method(def param) { param + 10 } } def invokeClosure = {Closure closure -> return closure.call() } def f = new Foo()...

javascript closure immediate evaluation

Hi, Consider the following Javascript code: var a = []; var f = function() { for (var i = 0; i < 3; i++) { a.push(function(){alert(i)}); } for (var j = 0; j < 3; j++) { a[j](); } }; The alerts print out '3' all three times. I want a different behaviour - in each iteration of the loop generate a funct...

Lua Closures in implementing a DSL

Lua has a really nice no-parenthesis call syntax that coupled with function closures allow me to write the following local tag = 1 function test(obj) return function(str) return function (tbl) tbl.objtag = tag tbl.objname = str return tbl end end end test (tag) "def" { } test tag ...

How different programming languages use closures?

To my knowledge, combined with the knowledge of others, among the mainstream languages Objective C C# VB.net Java Python Ruby Javascript Lisp Perl have closures and anonymous functions. Plain C/C++ doesn't have either of those. Do closures in these languages have the same semantics? How important are they for everyday programming? ...

Why can't a Python class definition assign a closure variable to itself?

Why doesn't the following work in Python? def make_class(a): class A(object): a=a return A ...

C++ closures and templates

We all know you can simulate closures in C++98 by defining local structs/classes inside a function. But is there some reason that locally defined structs can't be used to instantiate templates outside of the local scope? For example, it would be really useful to be able to do things like this: void work(std::vector<Foo>& foo_array) { ...

Erlang: Why does this fail with a 'badarith' exception?

Is it possible to implement a closure in Erlang? For example, how would I translate this snippet from Scheme? (define (make-adder n) (lamdba (x) (+ x n))) I've tried the following, but I'm clearly missing something. make_adder(n) -> fun (x) -> x + n end. Compiling this gives the error Warning: this expression will fail with...

Javascript infamous Loop problem?

Hi All: I've got the following code snippet. function addLinks () { for (var i=0, link; i<5; i++) { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function () { alert(i); }; document.body.appendChild(link); } } The above code is for generating 5 links and bind each link with an alert event to...

Problem with JavaScript closure

Hey guys, I am stuck at the following code. At first I'll describe the use-case: The function "addPreset" gets called with an instance of ColorGradient. When calling this.listController.addItem(...) a callback function named onSelect ist supplied, which gets called everytime the onSelect-event on the listController-item is triggered. Wha...

How to store local variables in jQuery click functions?

I'm trying to figure out how to store external variable values in the functions created during jQuery's click() event. Here's a sample of the code I'm working with now. for(var i=0; i<3; i++){ $('#tmpid'+i).click(function(){ var gid = i; alert(gid); }); } <div id="tmpid0">1al</div> <div id="tmpid1">asd</div> <div id="tmpid2">qwe<...

Example of a circular reference in Javascript?

Hi, I was wondering if anyone has a good, working example of a circular reference in javascript? I know this is incredibly easy to do with closures, but have had a hard time wrapping my brain around this. An example that I can dissect in Firebug would be most appreciated. Thanks ...

PHP Import Foreign Class' Method into MyClass

Wondering if this is possible in PHP Land: Let's say I have a class as follows: class myClass{ var $myVar; ... myMethod(){ $this->myVar = 10; } } and another class: class anotherClass { ... addFive(){ $this->myVar += 5; } } The 'anotherClass' is 3500 lines long and I just want the single 'addFive' m...

Emacs Actionscript 3 indentation for functions defined inline in an arglist

I'm using the actionscript-mode-connors.el for indenting Actionscript 3 code in emacs. I have most things figured out, but one thing bothering me is when I use an inline closure as a function argument, the indentation of the interior of the function is screwed up. For example: var foo:int = some_function( bar, baz, function():vo...

Variables in Anonymous Functions -- Can someone explain the following?

I've been trying to assign a function to onclick event of a dynamically created "a" tag in JavaScript. All of the tags are created in a loop as follows: for ( var i = 0; i < 4; i++ ) { var a = document.createElement( "a" ); a.onclick = function( ) { alert( i ) }; document.getElementById( "foo" ).appendChild( a ); } The alerted v...

JavaScript Variable Scope

I'm having a problem with some JavaScript code. Script setTimeout(function() { for (var i = 0; i < 5; i++) { setTimeout(function() { console.log(i); }, i * 200); } }, 200); Outputs 5, 5, 5, 5, 5 instead of 1, 2, 3, 4, 5 I can kind of understand why this doesn't work, but I was wondering if someon...

Passing values to onclick

If I create a whole lot of HTML elements using a loop, like for (i= 1; i < 100; i++) { var my_element = document.createElement ("td"); row.appendChild (my_element); my_element.onclick = function () {my_function (i)); } then when the element is clicked, the value of i passed to my_function is always 100, regardless of what ...

Why are closures suddenly useful for optimizing programs to run on multiple cores?

I read an article that claims that closures (or "blocks") are a useful weapon in the "War on Multicores", because [...] they allow you to create units of work, which each have their own copy of the stack, and don’t step on each others toes as a result. What’s more, you can pass these units around like they are values, when ...

In Groovy SwingBuilder, how do I attatch a closure to a JTable that fires when a cell is selected?

I have a JTable being constructed via Groovy's SwingBuilder. I'd like to attach a closure to the table that fires when a cell is selected, but I can't seem to find the right hook. How do I do that? ...

Are these examples C# closures?

I still don't quite understand what a closure is so I posted these two examples and I want to know whether these examples are both closures or not? Example A: List<DirectoryInfo> subFolders = new List<DirectoryInfo>(); Action<string> FilterSubFoldersStartA = s => subFolders. AddRange((new DirectoryInfo(s)).GetDirectories(). ...