closures

Define a method that is a closure in Ruby

I'm re-defining a method in an object in ruby and I need the new method to be a closure. For example: def mess_it_up(o) x = "blah blah" def o.to_s puts x # Wrong! x doesn't exists here, a method is not a closure end end Now if I define a Proc, it is a closure: def mess_it_up(o) x = "blah blah" xp = Proc.new {|| p...

"Closures are poor man's objects and vice versa" - What does this mean?

Closures are poor man's objects and vice versa. I have seen this statement at many places on the web (including SO) but I don't quite understand what it means. Could someone please explain what it exactly means? If possible, please include examples in your answer. Thanks. ...

How can i call that function inside that anonymous javascript? (TinyMce example)

How can i call test() inside that method? It's possible? (function() { tinymce.create('tinymce.plugins.WrImagerPlugin', { init : function(editor, url) { editor.addCommand('mceWrImagerLink', function() { //--> how can i refer to test() here? }); }, test: function () ...

scoping problem in recursive closure

why does this work: def function1(): a = 10 def function2(): print a function2() ...

Ugly thing and advantage of anonymos method -C#

I was asked to explain the ugly thing and advantages of anonymous method. I explained possibly Ugly thing anonymous methods turning quickly into spaghetti code. Advantages We can produce thread safe code using anonymous method :Example static List<string> Names = new List<string>( new string[] { "Jon Skeet", "Marc Grav...

C# -Closure -Clarification

I am learning C#.Can I mean closure as a construct that can adopt the changes in the environment in which it is defined. Example : List<Person> gurus = new List<Person>() { new Person{id=1,Name="Jon Skeet"}, new Person{id=2,Name="Marc Gravell"}, new Person{id=3,N...

Closures in Java 7

So is Java 7 finally going to get the closures? What's the latest news? ...

Strange recursive infinite loop I cannot track down

Hey there, I've got a prototype having a method I can add callbacks with: /* * Add a callback function that is invoked on every element submitted and must return a data object. * May be used as well for transmitting static data. * * The callback function is supposed to expect a jQuery element as single parameter * and must retur...

Bind a java class as a closure into a groovy-script

Is it possible to bind a closure written in java into a groovy-script. Is there an interface or something to implement so i can provide a closure? Something like this? public class Example implements Closure { public void closure(Object... args) { System.out.println(args[0]); } } Bind this into the groovyscript. Binding...

How do I pass the value (not the reference) of a JS variable to a function?

Here is a simplified version of something I'm trying to run: for (var i = 0; i < results.length; i++) { marker = results[i]; google.maps.event.addListener(marker, 'click', function() { change_selection(i); }); } but I'm finding that every listener uses the value of results.length (the value when the for loop terminates)....

Access to modified closure, is this a ReSharper bug?

I have the latest ReSharper 5.0 build (1655), where I have encountered the suggestion 'Access to modified closure' on the following code: var now = new DateTime(1970, 1, 1); var dates = new List<DateTime>(); dates.Where(d => d > now); ... now = new DateTime(); and the now inside the lambda expression is underlined with the warning. I...

Why can't I toggle the visibility of every element (within a group) in a for statement, only the last element in the group gets toggled?

The script fires Toggle fine on each Control's (Controls[i]) Click. If the Control's first OL element is not Visible it should be set Visible and all other elements in Controls that are not the current Control (Controls[i]) should be set Hidden. If the Control's first OL element is Visible it should be set Hidden. .js function Toggle(C...

Closures and universal quantification

I've been trying to work out how to implement Church-encoded data types in Scala. It seems that it requires rank-n types since you would need a first-class const function of type forAll a. a -> (forAll b. b -> b). However, I was able to encode pairs thusly: import scalaz._ trait Compose[F[_],G[_]] { type Apply = F[G[A]] } trait Closu...

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

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 h...

Scala closure context

I am not a Groovy expert, but I did read the book "Groovy in Action". In Groovy, each closure comes with a "context", where the items inside the closure can get access to pseudo-variables like "this", "owner", and "delegate", that let the items know who called the closure. This allows one to write DSLs like this (from Groovy in Action): ...

Why not .NET-style delegates rather than closures in Java?

OK, this is going to be my beating a dying horse for the 3rd time. However, this question is different from my earlier two about closures/delegates, which asks about plans for delegates and what are the projected specs and implementation for closures. This question is about - why is the Java community struggling to define 3 different t...

Does this incorporate JavaScript closures?

In trying to learn JavaScript closures, I've confused myself a bit. From what I've gathered over the web, a closure is... Declaring a function within another function, and that inner function has access to its parent function's variables, even after that parent function has returned. Here is a small sample of script from a recent proj...

Scala: Can't catch exception thrown inside a closure

Disclaimer: absolute novice in Scala :( I have the following defined: def tryAndReport(body: Unit) : Unit = { try { body } catch { case e: MySpecificException => doSomethingUseful } } I call it like this: tryAndReport{ someCodeThatThrowsMySpecificException() } While the call to someCodeThatThrowsMySpecificException...

Javscript closure questions

While I was reading the book Javascript: The Good Parts. I can not understand the piece of code bellow: We can generalize this by making a function that helps us make memoized functions. The memoizer function will take an initial memo array and the fundamental function. It returns a shell function that manages the memo st...

javascript. is it posible for one member of an object to access another member of that object without explicit reference to the object itself?

For example: var myObj={ myValue="hola", asMember=function(){ alert( this.myValue ); } }; myObj.asMember(); // will work fine var asGlobal=myObj.asMember; // global alias for that member function asGlobal(); // won't work in javascript (will work in AS3, but i need js now) So the question is, can I rewrite asMember so that i...