closures

Simple text menu in C++

谁都会走 提交于 2020-01-14 05:54:46
问题 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 { public MenuItem(string cmd, string desc, Action action) { Command = cmd; Description = desc; Action = action; } public string Command { get; private set; } public string

JavaScript: Event Handlers: Where to declare variables - local or closure (vs overhead)?

人盡茶涼 提交于 2020-01-13 11:37:10
问题 I find myself writing various functions which contain event handlers. It feels preferable to declare the variables required by the handler functions at the root of the parent function (closure), especially if they are jQuery selections, constants required by more than one handler, or some pre-computations needed which I wouldn't want to repeat each time en event is fired. A simple example: var touchDrag = function() { var x, y, i; var $mySelection = $('.selection'); $('#some-elem').on(

UnboundLocalError: local variable referenced before assignment in python closure

一个人想着一个人 提交于 2020-01-13 09:29:07
问题 I implemented two simple closures in Python. To me, they looks the same, but one works and the other doesn't. The working one is: def makeInc(x, y): def inc(): return y + x return inc inc5 = makeInc(5, 10) inc10 = makeInc(10, 5) inc5 () # returns 15 inc10() # returns 15 But the second one doesn't work: import os def linker(dest, filename): print filename def link(): if os.path.isfile(filename): # line 17 filename = os.path.join(os.getcwd(), filename) dest = os.path.join(dest, filename) y =

Will java allow to use functional interfaces as methods?

廉价感情. 提交于 2020-01-13 08:28:26
问题 With the new java lambdas and the concept of functional interfaces, will it be possible to treat those functional interfaces as methods? interface Func { void execute(int i); } void call(Func f) { f(1); //instead of f.execute(1); } I found a lot of information about the syntax of actual lambda expressions, but nothing about this. 回答1: Your proposition What you propose has been discussed on the lambda-dev mailing list before: http://mail.openjdk.java.net/pipermail/lambda-dev/2012-February

Go closure variable scope

我的未来我决定 提交于 2020-01-12 07:25:09
问题 I'm reading 'CreateSpace An Introduction to Programming in Go 2012' and on page 86 I found this evil magic func makeEvenGenerator() func() uint { i := uint(0) return func() (ret uint) { ret = i i += 2 return } } // here's how it's called nextEven := makeEvenGenerator() fmt.Println(nextEven()) fmt.Println(nextEven()) fmt.Println(nextEven()) 1) Why is i not resetting ? 2) is nextEven() returning and uint or is Println so smart that it can work with everything ? 回答1: For the sake of clarity, I

addEventListener, for(), index. how to use closure? [duplicate]

谁说胖子不能爱 提交于 2020-01-12 07:10:34
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Javascript infamous Loop issue? [duplicate] (5 answers) Closed 6 years ago . I have this code: var items = this.llistat.getElementsByTagName('a'); for( var i = 0; i < items.length; i++ ){ items[i].addEventListener('click', function(event) { alert( i ); }, items[i]); } where the event is listened, but there are 3 items and the alert allways print 3 on any of the elements (it doesn't

closures in groovy vs closures in java 8 (lambda expressions)?

点点圈 提交于 2020-01-12 06:56:07
问题 Given doSomething(Function foo) { println foo(2) } Groovy: doSomething( { it*it } as Function ) Java: doSomething( (x) -> x*x ) Is there any difference between the two? 回答1: In Groovy, closures are first class citizens of type groovy.lang.Closure , whereas lambdas in Java 8 are actual instances of the default method interface they represent. This means you need to use the as keyword in Groovy (as you've shown), but alternatively, in Java you need to specify an interface, so in Groovy, you

Closures vs. classes for encapsulation?

一笑奈何 提交于 2020-01-11 18:00:30
问题 I'm new to JS (from C++/etc), and it's just occurred to me that closures seem to be a simpler and more convenient way to handle encapsulation than classes. This code seems to give a simple way to handle encapsulation: function addProperty(o) { var value; o["get"] = function() { return value; } o["set"] = function(v) { value = v; } } // create two independent objects which are instances of pseudo-class 'addProperty', // which each have their own version of a set of local fields, and methods

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

懵懂的女人 提交于 2020-01-11 10:37:09
问题 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 ascending order of numbers. So i thought why not do this: Class MyClass { ... Func<MyValueType> variable; ... public void DoSomethingThatGetsCalledOften() {

Closures and for loops in Ruby

大憨熊 提交于 2020-01-11 05:36:46
问题 I'm kind of new to Ruby and some of the closure logic has me a confused. Consider this code: array = [] for i in (1..5) array << lambda {i} end array.map{|f| f.call} # => [5, 5, 5, 5, 5] This makes sense to me because i is bound outside the loop, so the same variable is captured by each trip through the loop. It also makes sense to me that using an each block can fix this: array = [] (1..5).each{|i| array << lambda {i}} array.map{|f| f.call} # => [1, 2, 3, 4, 5] ...because i is now being