closures

Printing out the fibonacci series

大兔子大兔子 提交于 2020-01-21 08:56:27
问题 I am trying to write a simple Python program. It's supposed to return a a closure that returns successive fibonacci numbers: def fibGen(): n_1 = 0 n_2 = 0 n = 1 def fib(): if n_1 ==0 and n_2 ==0: n_1 = 1 return n else: n = n_1 + n_2 n_2 = n_1 n_1 = n return n return fib f = fibGen() for i in range(0,10): print(f()) I get this error at run time: UnboundLocalError: local variable 'n_1' referenced before assignment EDIT: In my original post, I had not included n = 1 in the definition of fibGen

How can I create a reference cycle using dispatchQueues?

本秂侑毒 提交于 2020-01-19 06:09:06
问题 I feel that I've always misunderstood that when reference cycles are created. Before I use to think that almost any where that you have a block and the compiler is forcing you to write .self then it's a sign that I'm creating a reference cycle and I need to use [weak self] in . But the following setup doesn't create a reference cycle. import Foundation import PlaygroundSupport PlaygroundPage.current.needsIndefiniteExecution class UsingQueue { var property : Int = 5 var queue : DispatchQueue?

Javascript Closure with google.maps.Geocoder::geocode

佐手、 提交于 2020-01-17 12:27:12
问题 I'm using google.maps javascript API v3. I need to translate a set of civic addresses to markers on a google map with 'click' listeners. When the user clicks on the marker in the map, I want another window to open with a url related to that address (another tab would be ideal). I can't manage to keep the relation between the address and the url, because I'm using javascript Closure with the google method geocode(). I have a set of addresses such as var addresses = [ '123 street street', '124

How to save/clear setTimeout's array using loop's index?

可紊 提交于 2020-01-17 04:59:25
问题 I am calling a for loop multiple times. I would like to save a single setTimeout for each index. The idea is to use the loop's index as setTimeout's array index, but setTimeout returns an incremental ID number, I would like to reset it, or override to take control of the returning ID, so I can identify each timeout with a given index, always within the range of loop's index that is executed over and over again. Being able to use clearTimeout later on with a specific index. var timeouts = [];

JavaScript closure inside loops – simple practical example

Deadly 提交于 2020-01-16 16:29:07
问题 var funcs = []; // let's create 3 functions for (var i = 0; i < 3; i++) { // and store them in funcs funcs[i] = function() { // each should log its value. console.log("My value: " + i); }; } for (var j = 0; j < 3; j++) { // and now let's run each one to see funcs[j](); } It outputs this: My value: 3 My value: 3 My value: 3 Whereas I'd like it to output: My value: 0 My value: 1 My value: 2 The same problem occurs when the delay in running the function is caused by using event listeners: var

MKDirections calculateETAWithCompletionHandler executing with delay Swift 2.0

孤街醉人 提交于 2020-01-16 03:57:27
问题 I'm trying to calculate the ETA between two coordinates in Swift using calculateETAWithCompletionHandler from the MKDirections class. I have the following code in my program, MapViewController.swift print("A") calculateETAofClosestBus(closestBusStop) print("B") func calculateETAofClosestBus(destination: BusStop) { var shortestETA = 0 print("C") var request = MKDirectionsRequest() var sourceItem = MKMapItem(placemark: MKPlacemark(coordinate: listBuses[0].pin.coordinate, addressDictionary: nil)

Simulate static variables in python with closures

这一生的挚爱 提交于 2020-01-16 01:21:06
问题 Is it possible to write a function in python, which takes an argument a and prints the result of h+a where h is a local variable. Then it should return itself, with h increased by one. 回答1: Yes, you can def f(a): def inner(h, a): print h+a return lambda (x): inner(h+1, x) return inner(1, a) example g = f(0) # +1 g = g(0) # +2 g = g(0) # +3 f(0) # +1 g(0) # +4 g(0) # +4 prints 1 2 3 1 4 4 Q.E.D. 回答2: In python 3, you can do this: >>> def f(a): ... h = 1 ... def inner(): ... nonlocal h ...

multiple errors while momoizing function inside another function

北城余情 提交于 2020-01-16 00:34:05
问题 I have something like this: function main() { function create_node() { console.log("create_node") (function() { var memo; console.log(memo); function f() { var value; console.log(value); if (memo) { value = memo.cloneNode(); console.log("clone node"); console.log(value); } else { var value = document.createElement("div"); var style = ""; value.setAttribute("style", style); value.innerHTML = "hello"; console.log("new node"); console.log(value); } return value; } return f; })(); } var colection

jQuery: click function bind in for-loop with closure fix

你。 提交于 2020-01-15 03:56:09
问题 Im stuck here, any hint would be nice. I have an array of Objects objects[] and an array of divnames[]. My object has functions like play() and stop() on it. The Object and its' functions were tested in multiple situations, they are working. Now im Trying to iterate over the divnames[] and assign actions of the appropriate objects[] to mouseover, mouseout and click. There was a closure problem, that i fixed with a solution that i found in another thread here on StackOverflow. That works. But

Binding “this” when passing an object's member function

眉间皱痕 提交于 2020-01-14 19:41:26
问题 I had a "class" defined and was making only one instance of it. The instance possessed a member function that would end up being passed around (it's a mouse handler, but that's not important). Since I would only ever make one instance of my "class", I decided to rewrite it as a singleton by using an object literal. So I have var mySingleton = { theObjects : []; } mySingleton.mouseHandler = (function() { var that = this; return function (e) { for (var indx = 0; indx < that.theObjects.length;