closures

Creating a lambda from an s-expression

I have an s-expression bound to a variable in Common Lisp: (defvar x '(+ a 2)) Now I want to create a function that when called, evaluates the expression in the scope in which it was defined. I've tried this: (let ((a 4)) (lambda () (eval x))) and (let ((a 4)) (eval `(lambda () ,x))) But both of these create a problem: EVAL...

What is the difference between a 'closure' and a 'lambda'?

Could someone explain? I understand the basic concepts behind them but I often see them used interchangeably and I get confused. And now that we're here, how do they differ from a regular function? ...

Problem sorting lists using delegates

I am trying to sort a list using delegates but I am getting a signature match error. The compiler says I cannot convert from an 'anonymous method' List<MyType> myList = GetMyList(); myList.Sort( delegate (MyType t1, MyType t2) { return (t1.ID < t2.ID); } ); What am I missing? Here are some references I found and they do it the same ...

Closures in Java 7

I have heard that closures could be introduced in the next Java standard that is scheduled to be released somewhere around next summer. What would this syntax look like? I read somewhere that introducing closures in java is a bigger change than generic was in java 5. Is this true? pros and cons? (By now we definitely know that closur...

Lexical closures in Python

While I was investigating a problem I had with lexical closures in Javascript code, I came along this problem in Python: flist = [] for i in xrange(3): def func(x): return x * i flist.append(func) for f in flist: print f(2) Note that this example mindfully avoids lambda. It prints "4 4 4", which is surprising. I'd expect...

Access to Modified Closure

string [] files = new string[2]; files[0] = "ThinkFarAhead.Example.Settings.Configuration_Local.xml"; files[1] = "ThinkFarAhead.Example.Settings.Configuration_Global.xml"; //Resharper complains this is an "access to modified closure" for (int i = 0; i < files.Length; i++ ) { // Resharper disable AccessToM...

javascript closures and function placement

Does the placement of a function have an effect on the performance of closures within scope? If so, where is the optimal place to put these functions? If not, is the implied association by closure enough reason to place a function in another place logically? For instance, if foo does not rely on the value of localState, does the fact ...

Event-based async in C#; any generic refactoring possible?

Some APIs, like the WebClient, use the Event-based Async pattern. While this looks simple, and probably works well in a loosely coupled app (say, BackgroundWorker in a UI), it doesn't chain together very well. For instance, here's a program that's multithreaded so the async work doesn't block. (Imagine this is going in a server app and...

Passing state to a callback in JavaScript an appropriate use of closures?

Suppose you want to make an async request in JavaScript, but you want to pass some state along to the callback method. Is the following an appropriate use of closures in JavaScript? function getSomethingAsync(someState, callback) { var req = abc.createRequestObject(someParams); req.invoke(makeCallback(someState, callback)); } f...

Can someone explain Anonymous methods to me?

Delphi 2009, among some cool stuff, has also just got Anonymous methods. I've seen the examples, and the blog posts regarding anonymous methods, but I don't get them yet. Can someone explain why I should be excited? ...

When to use closure?

I have seen samples of closure from - http://stackoverflow.com/questions/36636/what-is-a-closure Can anyone provide simple example of when to use closure? Specifically, scenarios in which closure makes sense? Lets assume that the language doesn't have closure support, how would one still achieve similar thing? Not to offend anyone, p...

C# Captured Variable In Loop

I met a interesting issue about C#. I have code like below List<Func<int>> actions = new List<Func<int>>(); int variable = 0; while (variable < 5) { actions.Add(() => variable * 2); ++ variable; } foreach (var act in actions) { Console.WriteLine(act.Invoke()); } I expect it to output 0, 2, 4, 6, 8. However, it actually o...

Javascript Reflection

Is there a way to get all methods (private, privileged, or public) of a javascript object from within? Here's the sample object: var Test = function() { // private methods function testOne() {} function testTwo() {} function testThree() {} // public methods function getMethods() { for (i in this) { alert(i)...

Simple text menu in C++

I am writing a silly little app in C++ to test one of my libraries. I would like the app to display a list of commands to the user, allow the user to type a command, and then execute the action associated with that command. Sounds simple enough. In C# I would end up writing a list/map of commands like so: class MenuItem { ...

Firefox Javascript Events Anonymous Function

I'm attempting to register an anonymous function when a user clicks a cell in an HTML table. Here's some of the raw, unadulterated code: document.getElementById( "course"+displayed_year_index+occurrences_indices[displayed_year_index]).onclick = eval("function() {PrintReceipt("+result.years[result_year_index].rul_code+");};"...

Access to Modified Closure (2)

This is an extension of question from Access to Modified Closure. I just want to verify if the following is actually safe enough for production use. List<string> lists = new List<string>(); //Code to retrieve lists from DB foreach (string list in lists) { Button btn = new Button(); btn.Click += new EventHandler(delegate { MessageBox....

How do I use the value of a variable at declaration time in a JavaScript anonymous function?

Hi, This is a really basic question but... I have some code like this var arr = Array('blah.jpg','ha.jpg'); for (var i=0; i<array.length; i++) { $('div#blah' + i).click(function() { $('img').attr('src', arr[i]); }); } This should bind the div with id="blah0" to change all images to 'blah.jpg' when clicked. Similarly, ...

Do you use Template Method Pattern in programming languages with closures/delegates/function pointers ?

I have been going back and forth between C# and Java for the last 8 years. One thing that strikes me is that I have completely stopped using the "Template Method" design pattern in C#. Actually, in C# I Have come to think of this pattern as an anti-pattern. http://en.wikipedia.org/wiki/Template_method_pattern Coming back to Java, I ...

Running unit tests on nested functions

I come from the Java world, where you can hide variables and functions and then run unit tests against them using reflection. I have used nested functions to hide implementation details of my classes so that only the public API is visible. I am trying to write unit tests against these nested functions to make sure that I don't break th...

Event handlers inside a Javascript loop - need a closure?

I'm working with a bit of html and Javascript code that I've taken over from someone else. The page reloads a table of data (via an asynchronous request) every ten seconds, and then re-builds the table using some DOM code. The code in question looks something like this: var blah = xmlres.getElementsByTagName('blah'); for(var i = 0; i < ...