closures

Is there a standard closure interface that is Serializable?

If not, maybe you could tell me why. I get NotSerializableException when my class is being serialized. Inside the class I am passing this anonymous class to a method: new org.apache.commons.collections.Closure() { ... }; The question is still unanswered. I want to know if there is a standard Closure interface that implements Seri...

Mutable or immutable closures

In an imperative, object orients language, would make more sense to have mutable or immutable closures? For example: int i=5; function() f={print(i);}; f(); i=6; f(); If the closure is mutable, this would print: 5 6 If it is immutable, it would print: 5 5 I realize that even with immutable closures, you could still do this: cl...

How captured value in anonymous methods are implemented in .NET

I am curious about the actual .NET implementation and the decision behind it. For example in Java, all captured values used in an anonymous classes are required to be final. This requirement seems to be dropped in .NET. Also, is there a difference in an implementation of captured values for value types as opposed to reference types? T...

Javascript closure inside loops - simple practical example

Closures are one of those things which has been discussed a lot on SO, but this situation pops up a lot for me and I'm always left scratching my head what to do. var funcs = {}; for (var i = 0; i < 3; i++) { // let's create 3 functions funcs[i] = function() { // and store them in funcs console.log("My val...

Best groovy closure idiom replacing java inner classes?

As new to groovy... I'm trying to replace the java idiom for event listeners, filters, etc. My working code in groovy is the following: def find() { ODB odb = ODBFactory.open(files.nodupes); // data nucleus object database Objects<Prospect> src = odb.getObjects(new QProspect()); src.each { println it }; odb.close(); ...

What is so special about closures?

I've been reading this article about closures in which they say: "all the plumbing is automatic" the compiler "creates a wrapper class" and "extends the life of the variables" "you can use local variables without worry" the .NET compiler takes care of the plumbing for you, etc. So I made an example based on their code and to me, it s...

How does one return from a groovy closure and stop its execution?

I would like to return from a closure, like one would if using a break statement in a loop. For example: largeListOfElements.each{ element-> if(element == specificElement){ // do some work return // but this will only leave this iteration and start the next } } In the above if statement I would lik...

How safe would it be to use functional-java to add closures to a Java production project?

I would love to use closures in Java. I have read that they may or may not make it into Java 7. But an open-source project called functional-java has implemented functional features including closures. How safe would it be to use such a library in an enterprise production app? Is there a better way to add closures to Java currently? ...

What is the difference between a monad and a closure?

i am kinda confused reading the definition between the two. Can they actually intersect in terms of definition? or am i completely lost? Thanks. ...

Difference between class(java) and closure(javascript)?

I don't understand how closure is more powerful than class. It looks like I can achieve the same behavior of closure using class. Any help would be appreciated ...

Groovy: meaning of 'this' inside a closure

Hi, The following example is adapted from 'Groovy in Action' class Mother { Closure birth() { def closure = { caller -> [this, caller] } return closure } } Mother julia = new Mother() closure = julia.birth() con...

implementing Interfaces with closures in Groovy - what method was called?

There is an idiom in groovy to implement an Interface with a single closure. The closure must be prepared to handle whatever arguments are passed. That works fine. But how does one determine what method was called at the interface? interface X { void f(); void g(int n); void h(String s, int n); } x = {Object[] args -> println "method c...

Why higher order procedures??

So if a language provides higher order procedure then I can have procedure that returns procedure... something like (define (Proc a b c) (lambda (x) ( // method body here in terms of a b c and x))) To create new proc I would just do something like.. (define ProcA (Proc a1 b1 c1)) // Would create ProcA that has 1 argument Similar task...

jQuery, hover method and closure

Have been struggling with Javascript closure for a while trying to wrap brain around function scopes, but I think they're wrapping around me instead. I've looked at a number of posts (Nyman's was the most helpful) but obviously still don't get it. Trying to run a loop over the hover method in jQuery. Need hover functions to ultimate trig...

groovy closure parameters

Hi, The following example of using the sendMail method provided by the grails mail plugin appears in this book. sendMail { to "[email protected]" subject "Registration Complete" body view:"/foo/bar", model:[user:new User()] } I understand that the code within {} is a closure that is passed to sendMail as a parameter. I also...

Set a different id for every new element in the callback off the load event

edited my original question The problem is the same for setting the id off a newly added element or if I use the argument noteid from the function. The result is that every element get's the same id if this function get's looped. How can I set a different id for every newly added element??? As it is now, the value in the submit event...

how to get the closure in lua?

suppose i have a file name "test.lua" containing lines below: --[[ test.lua --]] local f = function() print"local function f in test.lua" end f_generate = function() local fun = loadstring(" f()") -- local env = getfenv(1) -- set(fun,env) return fun end f_generate()() --[[ end of test.lua--]] because loadstring doing its stuff...

How is data passed to anonymous functions in JavaScript?

When I pass 'this' to an anonymous function like so: MyClass.prototype.trigger = function(){ window.setTimeout(function(){this.onTimeout();},1000); } I get a "this.onTimeout is not a function"-error. I guess that 'this' is no longer available at the time the anonymous funtion is executing? So I've been doing this: MyClass.prototy...

How are local variables referenced in closures?

I am reading an article (JavaScript Closures for Dummies) and one of the examples is as follows. function buildList(list) { var result = []; for (var i = 0; i < list.length; i++) { var item = 'item' + list[i]; result.push( function() {alert(item + ' ' + list[i])} ); } return result; } function testList() { var fnlist ...

Javascript Closure and Data Visibility

I am trying to wrap my head around the idea of classes, data visibility and closures (specifically in Javascript) and I am On the jQuery docs page for types, it mentions that closures are used to hide data: The pattern allows you to create objects with methods that operate on data that isn't visible to the outside—the very basis of o...