closures

Javascript closures function parameters?

ぐ巨炮叔叔 提交于 2019-12-03 07:40:32
Code belongs to javascriptissexy.com My question is why invoking mjName ("Jackson") returns "This celebrity is Michael Jackson"? Is it that second parameter given in ANY outer function always, says to js = inner function parameter? Could someone explain the whole concept in great detail? function celebrityName (firstName) { var nameIntro = "This celebrity is "; // this inner function has access to the outer function's variables, including the parameter function lastName (theLastName) { return nameIntro + firstName + " " + theLastName; } return lastName; } var mjName = celebrityName ("Michael")

Closures and universal quantification

不问归期 提交于 2019-12-03 07:26:35
问题 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 Closure[F[_],G[_]] { def apply[B](f: F[B]): G[B] } def pair[A,B](a: A, b: B) = new Closure[Compose[({type f[x] = A => x})#f, ({type f[x] = B => x})#f]#Apply, Id] {

C# Action, Closure, and Garbage Collection

怎甘沉沦 提交于 2019-12-03 07:14:43
问题 Do I need to set MyAction to null so that garbage collection will be able to proceed with either of these classes? I am less concerned when both classes are to have almost the same lifespan. My question is more appropriate when Class1’s lifespan is much longer then Class2 or when Class2’s lifespan is much longer then Class1. The code here is stripped down. Assume that both Class1 and Class2 contain other members and methods that may affect their lifespan. public class Class1 : IDisposable {

Two functions with the same name in JavaScript - how can this work?

时间秒杀一切 提交于 2019-12-03 07:03:48
问题 As far as I know, function foo() { aaa(); } is just var foo = function(){ aaa() } in JavaScript. So adding function foo() { bbb(); } should either overwrite the foo variable, or ignore the second definition - that's not the point. The point is that there should be one variable foo . So, in this example the me variable should not be correctly resolved from inside the methods and it is not in Explorer 8 :-) . I came to this example by trying to wrap them into another closure where ( var ) me

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

佐手、 提交于 2019-12-03 07:01:37
问题 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 types of closures when we could simply steal the whole concept of delegates lock, stock and barrel from our beloved and friendly neighbour - Microsoft. There are

Does a (JS) Closure Require a Function Inside a Function

走远了吗. 提交于 2019-12-03 06:55:52
I'm having a little difficulty with the inherent concept of a closure. I get the basic idea, but here's the thing: I thought that, technically, there "is a closure" inside every Javascript function. To quote wikipedia: In computer science, a closure (also lexical closure, function closure or function value) is a function together with a referencing environment for the nonlocal names (free variables) of that function. Such a function is said to be "closed over" its free variables. So since you can define variables inside a function, they are "closed off" to the rest of your code, and so I see

Why do Python yield statements form a closure?

▼魔方 西西 提交于 2019-12-03 06:46:13
问题 I have two functions that return a list of functions. The functions take in a number x and add i to it. i is an integer increasing from 0-9. def test_without_closure(): return [lambda x: x+i for i in range(10)] def test_with_yield(): for i in range(10): yield lambda x: x+i I would expect test_without_closure to return a list of 10 functions that each add 9 to x since i 's value is 9 . print sum(t(1) for t in test_without_closure()) # prints 100 I expected that test_with_yield would also have

capture by value class members

两盒软妹~` 提交于 2019-12-03 06:41:40
问题 Is there a way, when writing a lambda function within a member function, to capture fields of the enclosing class by value? The default catch-all = doesn't work because when I reference the variable inside the lambda I get instead a dereferencing from the captured this pointer, as well as explicitly naming the variable in the capture list, because I get two compile error: capture of non-variable <name> , and ‘this’ was not captured for this lambda function 回答1: No, data members cannot be

What is the difference between a monad and a closure?

拟墨画扇 提交于 2019-12-03 06:29:19
i am kinda confused reading the definition between the two. Can they actually intersect in terms of definition? or am i completely lost? Thanks. Closures, as the word tends to be used, are just functions (or blocks of code, if you like) that you can treat like a piece of data and pass to other functions, etc. (the "closed" bit is that wherever you eventually call it, it behaves just as it would if you called it where it was originally defined). A monad is (roughly) more like a context in which functions can be chained together sequentially, and controls how data is passed from one function to

Should I pass a lambda by const reference.

拥有回忆 提交于 2019-12-03 06:22:28
Typically I use the following pattern when accepting a lambda as an argument to a function (A template class passed-by-value): template <class Function> void higherOrderFunction(Function f) { f(); } Does this copy (the closure of) the argument? If so, is there anything wrong with accepting the lambda by const reference instead? template <class Function> void higherOrderFunction(const Function& f) { f(); } A simple test seems to indicate that this works fine, but I want to know if there are any special considerations that I should be aware of. If you pass by value you will copy the closure