Hi, I've been trying to learn Python, and while I'm enthusiastic about using closures in Python, I've been having trouble getting some code to work properly:
def memoize(fn):
def get(key):
return (False,)
def vset(key, value):
global get
oldget = get
def newget(ky):
if key==ky: return...
In the two following snippets, is the first one safe or must you do the second one?
By safe I mean is each thread guaranteed to call the method on the Foo from the same loop iteration in which the thread was created?
Or must you copy the reference to a new variable "local" to each iteration of the loop?
var threads = new List<Thread>(...
I'm creating my own JavaScript Array-like object and I have methods that call closures. I was just wondering where was the most efficient place to define a closure.
For example, imagine I have a map function and a chop function:
MyObject.prototype =
{
map: function(fn) { ... applies fn to each element ... };
chop: function()
...
Is there some way to use default parameters values with closures in Groovy?
This is what I tried so far:
class Persona {
String name
Persona( String name ) {
this.name = name
}
String salute( String salute = "Hola" ) {
salute + ' ' + this.name
}
}
Persona.metaClass.salute2 = {
String salute ...
The testit() method is a closure. aString has fallen out of scope but testit() can still execute on it. testit2() is using a variable that hasn't fallen out of scope (mystring) but which was also not been passed into testit2(). Is testit2() considered a closure?
string mystring = "hello world";
Action testit = new Action(delegate { st...
Is there a way to serialize a lexical closure in Python using the standard library? pickle and marshal appear not to work with lexical closures. I don't really care about the details of binary vs. string serialization, etc., it just has to work. For example:
def foo(bar, baz) :
def closure(waldo) :
return baz * waldo
...
I've been mulling about a post by Misko Hevery that static methods in Java are a death to testability. I don't want to discuss the testability issue but more on the concept of static methods. Why do people hate it so much?
It's true that we don't have closures (but we have a slightly awkward anonymous functions), lambdas & functions as ...
I have a method that uses setTimeOut property and makes a call to another method. On initial load method 2 works fine. However, after timeout, I get an error that says method2 is undefined. What am I doing wrong here?
ex:
test.prototype.method = function()
{
//method2 returns image based on the id passed
this.method2('useSomeEl...
I'm trying to create a function on the fly that would return one constant value.
In JavaScript and other modern imperative languages I would use closures:
function id(a) {
return function() {return a;};
}
but Emacs lisp doesn't support those.
I can create mix of identity function and partial function application but it's not sup...
Duplicate
Closures in .NET
What are closures in C#?
...
Current setup:
var placeId;
function selectPlace(place) {
$('#selectPlace').html('Selected Place: <b>' + place.Name + '</b>');
$('#map').hide(400);
placeId = place.Id;
}
$(document).ready(function()
{
$('#postMessage').click(function() {
alert("PlaceId: " + placeId);
});
});
Can/should I be using closures?
...
I asked a question about callbacks and arrived at another question (see comment).
How is a closure different from a callback?
...
I was going to post a question, but figured it out ahead of time and decided to post the question and the answer - or at least my observations.
When using an anonymous delegate as the WaitCallback, where ThreadPool.QueueUserWorkItem is called in a foreach loop, it appears that the same one foreach-value is passed into each thread.
List...
Is it possible to simulate constants in Javascript using closures? If so, can you please furnish me with an example?
...
I'm trying to wrap my head around closures (there's a joke in there somewhere) and I ran across this:
(function () { /* do cool stuff */ })();
How does this work? What's the purpose of putting the function in parens? Why the empty parens afterwards?
...
I sort of understand closures in javascript, but what I'm not sure about is how it treats nested functions. For example:
var a = function(o) {
o.someFunction(function(x) {
// do stuff
});
}
I know a new closure is created everytime I call function a, but does that closure also include a new instance of the anonymous fu...
I am wondering if the following jquery code causes any memory leak:
$( function() {
var parent=$('table#mytable tbody').get(0);
$('tr:last', parent).click(function(){...});
});
For my understanding, $('tr:last', parent) is the last row which is the DOM object, but in the anonymous function, the closure has this DOM obje...
I am very puzzled about this code:
var closures = [];
function create() {
for (var i = 0; i < 5; i++) {
closures[i] = function() {
alert("i = " + i);
};
}
}
function run() {
for (var i = 0; i < 5; i++) {
closures[i]();
}
}
create();
run();
From my understanding it should print 0,1,2,3,4 (isn't this the conc...
I've been reading Diaz's book Pro JavaScript Design Patterns. Great book. I myself am not a pro by any means. My question: can I have a static function that has access to private instance variables? My program has a bunch of devices, and an output of one can be connected to the input of another. This information is stored in the inputs a...
When I pass an anonymous inner class into a function, I can refer to variables in the current scope from within a method of that class, like so:
class Caller {
private Object callerPrivate;
// ...
public void someMethod() {
final String callerLocal = "Eyes only";
ISomeInterface anon = new ISomeInterface() ...