lexical-closures

Scope of lambda functions and their parameters?

安稳与你 提交于 2020-01-08 09:32:32
问题 I need a callback function that is almost exactly the same for a series of gui events. The function will behave slightly differently depending on which event has called it. Seems like a simple case to me, but I cannot figure out this weird behavior of lambda functions. So I have the following simplified code below: def callback(msg): print msg #creating a list of function handles with an iterator funcList=[] for m in ('do', 're', 'mi'): funcList.append(lambda: callback(m)) for f in funcList:

Scope of lambda functions and their parameters?

元气小坏坏 提交于 2020-01-08 09:30:45
问题 I need a callback function that is almost exactly the same for a series of gui events. The function will behave slightly differently depending on which event has called it. Seems like a simple case to me, but I cannot figure out this weird behavior of lambda functions. So I have the following simplified code below: def callback(msg): print msg #creating a list of function handles with an iterator funcList=[] for m in ('do', 're', 'mi'): funcList.append(lambda: callback(m)) for f in funcList:

Do we have closures in C++?

大兔子大兔子 提交于 2019-12-18 10:27:08
问题 I was reading about closures on the net. I was wondering if C++ has a built-in facility for closures or if there is any way we can implement closures in C++? 回答1: The latest C++ standard, C++11, has closures. http://en.wikipedia.org/wiki/C%2B%2B11#Lambda_functions_and_expressions http://www.cprogramming.com/c++11/c++11-lambda-closures.html 回答2: If you understand closure as a reference to a function that has an embedded, persistent, hidden and unseparable context (memory, state), than yes:

Identify how the function has been called in closure javascript

前提是你 提交于 2019-12-12 19:50:34
问题 Recently i faced one problem in hackerrank which has to calculate multiplication operation and has to return the answer. For example function multiply(a,b) { return a*b; } Now here is the problem the function might call in different ways such as multiply(4,5); multiply(4)(5); multiply(4)(5)(6); I know we have to closure apporach for the second one which is multiply(4)(5). I had written code for that function multiply(a,b) { return function(b) { return a*b; } } Now what if its been multiply

python closure weird behavior

拜拜、爱过 提交于 2019-12-11 02:53:36
问题 I am trying a piece of code from the question in Lexical closures in Python flist = [] for i in xrange(3): def func(x): return x*i flist.append(func) for f in flist: print f.func_closure The output is: None None None Shouldn't it be?: (<cell at 0x9222d94: int object at 0x8cabdbc>,) (<cell at 0x9222d94: int object at 0x8cabdbc>,) (<cell at 0x9222d94: int object at 0x8cabdbc>,) I have got the above output using the following code: flist = [] def actualFact(): for i in xrange(3): def func(x):

How does the JS scope of these blocks work?

徘徊边缘 提交于 2019-12-10 17:23:03
问题 Can anyone explain why the following produces 1,2 and the other produces 5? Should they not both produce 5? //produces 1,2 (function () { var a = [5]; function bar() { if (!a) { var a = [1, 2]; } console.log(a.join()); } bar(); })(); Based on reading some articles about JS closure, I expect them both to produce 5. Can't seem to find an article anywhere that would give some insight as to why the first block produces otherwise. //produces 5 (function () { var a = [5]; function bar() { if (a) {

Using NSUndoManager, how to register undos using Swift closures

戏子无情 提交于 2019-12-01 23:40:24
问题 I am trying to grok how to use NSLayoutManager using Swift closures. I can successfully register an undo as follows: doThing(); undoManager?.registerUndoWithTarget(self, handler: { _ in undoThing(); } undoManager?.setActionName("do thing") Of course I need to support redo which amounts to an undo of an undo. I can do that: doThing(); undoManager?.registerUndoWithTarget(self, handler: { _ in undoThing(); undoManager?.registerUndoWithTarget(self, handler: { _ in doThing(); } undoManager?

Using NSUndoManager, how to register undos using Swift closures

喜你入骨 提交于 2019-12-01 21:12:14
I am trying to grok how to use NSLayoutManager using Swift closures. I can successfully register an undo as follows: doThing(); undoManager?.registerUndoWithTarget(self, handler: { _ in undoThing(); } undoManager?.setActionName("do thing") Of course I need to support redo which amounts to an undo of an undo. I can do that: doThing(); undoManager?.registerUndoWithTarget(self, handler: { _ in undoThing(); undoManager?.registerUndoWithTarget(self, handler: { _ in doThing(); } undoManager?.setActionName("do thing") } undoManager?.setActionName("do thing") But now I need to support an undo of the

Accessing VUE JS's data from Axios

烈酒焚心 提交于 2019-11-30 05:22:37
I have a Vue JS (Vuetify) App that makes an ajax request that I would like to populate a div's content with the response, however I am having difficulties accessing the instance's data . All examples I have seen use this to point to the data object, but when I do I get this error Unable to set property 'message' of undefined or null reference The app is quite simple: main.js : import Vue from 'vue' import App from './App.vue' import Vuetify from 'vuetify' Vue.use(Vuetify) new Vue({ el: '#app', render: h => h(App) }) App.vue export default { data () { return { .... message: '', order: {}, ... }

Do we have closures in C++?

拈花ヽ惹草 提交于 2019-11-29 21:15:49
I was reading about closures on the net. I was wondering if C++ has a built-in facility for closures or if there is any way we can implement closures in C++? Apeirogon Prime The latest C++ standard, C++11 , has closures. http://en.wikipedia.org/wiki/C%2B%2B11#Lambda_functions_and_expressions http://www.cprogramming.com/c++11/c++11-lambda-closures.html If you understand closure as a reference to a function that has an embedded, persistent, hidden and unseparable context (memory, state), than yes: class add_offset { private: int offset; public: add_offset(int _offset) : offset(_offset) {} int