closures

Issue with scope and closures in JavaScript

My question is really more about scope in JavaScript, rather then closures. Let's take the following code: var f = function () { var n = 0; return function () { return n++; }; }(); console.log(f()); console.log(f()); The above code outputs: 0 1 As you can see from the above code, f (self-invoked) returns a function, creating...

Nested Lambdas in Python

Hi, I'm a beginning python programmer, and I'd like someone to clarify the following behavior. I have the following code: env = lambda id: -1 def add(id, val, myenv): return lambda x: val if x == id else myenv(id) test_env = add("a", 1, env) test_env_2 = add("b", 2, test_env) When I look up "a" in test_env, it functions correc...

Closure/scope JavaScript/jQuery

Hello all, I'm trying to group some exisiting top-level functions inside a closure (to avoid polluting the global namespace) but I'm not quite getting it to work. First, all the JS works outside my anonymous function, but once I put it in the anonymous function I get an error of "crossfade is not defined". Does anyone see anything comp...

Writing my first DSL in C# and getting hung up on func<T> & Action

I'm taking a crack at writing my first DSL for a simple tool at work. I'm using the builder pattern to setup the complex parent object but am running into brick walls for building out the child collections of the parent object. Here's a sample: Use: var myMorningCoffee = Coffee.Make.WithCream().WithOuncesToServe(16); Sample with cl...

Closures in Java - syntax differences between the three major proposals?

Three major proposals for adding closures to the Java language has been presented: BGGA (Bracha Gafter Gosling Ahé) also known as "full closures", by Gilad Bracha, Neal Gafter, James Gosling and Peter von der Ahé CICE (Concise Instance Creation Expressions) also known as "simplified inner classes", by Bob Lee, Doug Lea and Josh Bloch F...

What exactly does "closure" refer to in JavaScript?

I understand what closures are, but I am having some trouble grokking exactly what the term closure refers to. I have seen the term used in many websites, but rarely do they agree on the actual definition of it. Is it the variables that are kept on the stack frame? Is it the function that is being returned? Is it the scope of the oute...

Closures in Ruby

hi i'm having a little trouble with closures and i'd like to know what the equivalent code for the canonical make-adder procedure would be in ruby, in scheme it would be like (define (make-adder n) (lambda (x) (+ x n)) thanks in advance ...

Groovy : Closures or Methods

Hi, I've got into the habbit of using Closures everywhere I can in place of regular methods, even when I don't need access to free variables. So, I will use this: def addNumbers = { left, right -> left + right } .. instead of this: def addNumbers (left,right) { left + right } Is this bad practice? I far prefer the extra power I get ...

PowerShell: an elegant way to create closures

Keith Hill explained me that blocks in PowerShell are not closures and that to create closures from blocks I have to call method .GetNewClosure(). Is there any elegant way to create closures from blocks? (e.g. create a wrapping function, an alias?, ...) Example: { block } ${ closure } # ??? ...

What's the name of the problem that relates to optimizing closures on a stack-based system?

I remember hearing about a general optimization problem that relates to function closures, stating that in general it's difficult to optimize the creation of a closure using only stack-based memory management. Do any of you remember the name of this optimization problem, possibly with an example or link to relevant page? ...

Callback, return value and HTML5 executeSql function

Hi. I have a big problem. I know it's about callback, closure but I don't know how to solve the problem. Here is my code $.Model.extend('Article', { findAll : function(params, success, error){ var result = [] db.transaction(function(tx) { tx.executeSql('select * fr...

Django Admin app: building a dynamic list of admin actions

I am trying to dynamically build a list of admin actions using the get_actions() method on a ModelAdmin. Each action relates to a particular instance of another model, and as new instances may be added or removed, I want to make sure the list of actions reflects that. Here's the ModelAdmin: class PackageAdmin(admin.ModelAdmin): lis...

What are good JavaScript OOP resources?

JavaScript is a lightweight and powerful language, but it's often misunderstood and hard to learn (especially about its object oriented programming). Here are what I found: Books JavaScript: The Good Parts by Douglas Crockfond, discusses many OOP topics in his 150 page book. Object-Oriented JavaScript: Create scalable, reusable high-q...

More explanation on Lexical Binding in Closures?

There are many SO posts related to this, but I am asking this again with a different purpose I am trying to understand why closures are important and useful. One of things that I've read in other SO posts related to this is that when you pass a variable to closure, the closure starts remembering this value from then onwards. Is this the...

Intellisense support in Visual Studio 2008/2010 for jQuery closures {

I'm trying to get Intellisense to correctly work for closure. As a plugin author, I always use a closure to create an isolated environment for my plugin code: (function($) { // code here })(jQuery); But the problem here is that Intellisense doesn't pick up that jQuery is being passed in the execution of the function. Adding $ = jQue...

Silverlight closure with Google Maps

I'm using Silverlight as the client for an application that uses the Google Maps API. I use the HTML bridge to talk to the JS API and that works quite well. One of my issues was that I couldn't use closures. In a full JS environment, I would register an event on the map thus: google.maps.Event.addListener(marker, "click", function() {...

Differing behavior when starting a thread: ParameterizedThreadStart vs. Anonymous Delegate. Why does it matter?

When I run the code below the output is "DelegateDisplayIt", typically repeated 1-4 times. I've run this code probably 100 times, and not once has the output ever been "ParameterizedDisplayIt". So it seems the manner in which the thread is created and subsequently started affects how the parameter is passed. When creating a new thread wi...

How do closures work behind the scenes? (C#)

I feel I have a pretty decent understanding of closures, how to use them, and when they can be useful. But what I don't understand is how they actually work behind the scenes in memory. Some example code: public Action Counter() { int count = 0; Action counter = () => { count++; }; return counter; } Norm...

Using Closures to keep track of a variable: Good idea or dirty trick?

Ok, i have a need to be able to keep track of value type objects which are properties on another object, which cannot be done without having those properties implement an IObservable interface or similar. Then i thought of closures and the famous example from Jon Skeet and how that prints out 9 (or 10) a bunch of times and not an ascendi...

testing for empty groovy closure?

I want to let users supply a groovy class with a property that is a file-selector closure which I pass on to AntBuilder's 'copy' task: class Foo { def ANT = { fileset(dir:'/tmp/tmp1') } } in my code, I pick up the ANT property as 'fAnt' and pass to Ant: ant.copy(todir:'/tmp/tmp2', fAnt) This works - but, if the user passe...