closures

Javascript Closures and 'this' context

I have a problem with an object I have created that looks something like this: var myObject = { AddChildRowEvents: function(row, p2) { if(document.attachEvent) { row.attachEvent('onclick', function(){this.DoSomething();}); } else { row.addEventListener('click', function(){this.DoSomething();}...

jQuery Closures, Loops and Events

I have a question similar to the one here: http://stackoverflow.com/questions/341723/event-handlers-inside-a-javascript-loop-need-a-closure#341759 but I'm using jQuery and the solution given seems to fire the event when it's bound rather than on click. Here's my code: for(var i in DisplayGlobals.Indicators) { var div = d.createElem...

What is the differences and possible similarities of closures and currying?

I've read through some of the post on here about closures and currying but I feel like I didn't find the answer. So what's the differences and possibly the similarities of closures and currying? Thanks for the help :) ...

Modify bound variables of a closure in Python

Is there any way to modify the bound value of one of the variables inside a closure? Look at the example to understand it better. def foo(): var_a = 2 var_b = 3 def _closure(x): return var_a + var_b + x return _closure localClosure = foo() # Local closure is now "return 2 + 3 + x" a = localClosure(1) # 2 + 3 + 1 == 6 # DO SO...

Validate reflected method return type and parms in Java

I have a generic Callback object which provides a (primitive) callback capability for Java, in the absence of closures. The Callback object contains a Method, and returns the parameter and return types for the method via a couple of accessor methods that just delegate to the equivalent methods in Method. I am trying to validate that a ...

Closures as class members?

I have taken a liking to jQuery/Javascript's way of extending functionality via closures. Is it possible to do something similar in PHP 5.3? class Foo { public $bar; } $foo = new Foo; $foo->bar = function($baz) { echo strtoupper($baz); }; $foo->bar('lorem ipsum dolor sit amet'); // LOREM IPSUM DOLOR SIT AMET [edit] mixed up 'it' ...

ruby: can a block affect local variables in a method?

I'm just learning ruby and trying to understand the scope of code executed in blocks. For example, I want to be able to create a block that affects the method that it is attached to, like so: def test(&block) block.call() if block_given? puts "in test, foo is #{foo}" puts "in test, bar is #{bar}" end test() { foo="this is foo" ...

How to fix closure problem in ActionScript 3 (AS3)

Hello, In the code below I'm trying to load some images and put them in the stage as soon as they get individually loaded. But it is bugged since only the last image is displayed. I suspect it's a closure problem. How can I fix it? Isn't the behaviour of closures in AS3 the same as in Java Script ? var imageList:Array = new Array(); im...

How to create custom MouseEvent.CLICK event in AS3 (pass parameters to function)?

Hello, This question doesn't relate only to MouseEvent.CLICK event type but to all event types that already exist in AS3. I read a lot about custom events but until now I couldn't figure it out how to do what I want to do. I'm going to try to explain, I hope you understand: Here is a illustration of my situation: for(var i:Number; i <...

What are 'closures' in .NET?

What is a 'closure'? Do we have them in .NET? If they do exist in .NET, could you please provide a code snippet (preferably in C#) explaining it? EDIT: I went through Jon Skeet's article to understand what closures are and how to use them in .NET. EDIT: I found another interesting article here ...

Closures and Context Free Grammars

I'm looking over my syllabus for my theoretical computer science class and within the heading of Context Free Grammars it lists "closure properties". I have looked through my textbook on this subject and found quite little. The little it does have is a bit above my head at the moment (I haven't taken the course yet) but I understand a li...

passing functions as arguments in ruby

I'm trying to wrap my head around functional programming in ruby and there doesn't seem to be much good documentation out there. Essentially, I'm trying to write a combine function that would have a Haskell type signature of: [a] -> [a] -> (a -> a -> a) -> [a] So combine([1,2,3], [2,3,4], plus_func) => [3,5,7] combine([1,2,3], [2,3...

java and javascript callbacks compared

Hello, It seems I don't understand javascript callbacks quite as well as I thought. In the following example, I would think that each copy of function in setTimeout would refer to its own copy of the variable "index". Therefore, running the example should produce the following alerts: "zero" "one" "two". var array = ["zero", "one", "t...

What causes this code not to work?

I'm trying to understand why this does not work. (Basic example with no validation yet) When I test it, firebug states Product.addPage is not found. var Product = function () { var Page = function () { var IMAGE = ''; return { image : function () { return IMAGE; ...

Cleaner (nested) closure in JavaScript with jQuery's each()

I was wondering if there is a cleaner (more succinct) way to do what each() is doing in following JavaScript code. $(".moreinfodialog") .before('<a href="#">Click for more info.</a>') .each(function() { var temp = this; $(this).prev("a").click(function() { $(temp).dialog("open"); return f...

How do I create a list of Python lambdas (in a list comprehension/for loop)?

I want to create a list of lambda objects from a list of constants in Python; for instance: listOfNumbers = [1,2,3,4,5] square = lambda x: x * x listOfLambdas = [lambda: square(i) for i in listOfNumbers] This will create a list of lambda objects, however, when I run them: for f in listOfLambdas: print f(), I would expect that i...

Based on how they are constructed, can callbacks also be defined as closures?

In JavaScript, I know that a closure is can be defined as a nested function that has access to its containing function's variables. For example: function outerFunction(x, y) { function innerFunction() { return x + y + 10; } return innerFunction; } Now, the following code is wiring up a callback for the onreadystate...

Are there any good reasons why closures aren't immutable in C#?

I've been going over and over this in my head, and I can't seem to come up with a good reason why C# closures are mutable. It just seems like a good way to get some unintended consequences if you aren't aware of exactly what's happening. Maybe someone who is a little more knowledgeable can shed some light on why the designers of C# woul...

What is the most pythonic way to make a bound method act like a function?

I'm using a Python API that expects me to pass it a function. However, for various reasons, I want to pass it a method, because I want the function to behave different depending on the instance it belongs to. If I pass it a method, the API will not call it with the correct 'self' argument, so I'm wondering how to turn a method into a fu...

closures and objects

Functional programming .. is like classic ( Mark Twain's type). While reading another articles about SICP, where people are talking about the great impact closures had on there thinking, i got reminded of this, which i read ages ago Closures are poor man's objects Objects are poor man's closure ( Can't recall exact source but it was pr...