closures

Creating ASYNC task in Swift 2

主宰稳场 提交于 2019-12-07 16:23:14
问题 I want to show users current location in map and I know it's not instantaneous task. I want to call my showCurrentLocation() function in the ASYNC task. I was trying to learn call back closures but i could not understand how can I create ASYNC task for this. I know this is not a best question template for stackoverflow but I don't know how can I ask differently. Thank you for any help. Have a nice coding. 回答1: In the past I've made a class called AsyncTask for usage purposes like the one you

Swift 3 closure overload resolution

流过昼夜 提交于 2019-12-07 15:55:44
问题 I'm confused by function overload resolution with closures in Swift 3. For example, in the code: func f<T>(_ a: T) { print("Wide") } func f(_ a: (Int)->(Int)) { print("Narrow") } f({(a: Int) -> Int in return a + 1}) I expect Narrow , not Wide , to be printed to the console. Can anyone explain why the more specific overload gets chosen for non-closure arguments but not for closures or is this a compiler bug? Swift 2 exhibited the expected behavior. 回答1: This is probably due to the change in

How to make a closure in Swift extract two integers from a string to perform a calculation

元气小坏坏 提交于 2019-12-07 15:27:26
问题 I am currently using map property with a closure in Swift to extract linear factors from an array and calculate a list of musical frequencies spanning one octave. let tonic: Double = 261.626 // middle C let factors = [ 1.0, 1.125, 1.25, 1.333, 1.5, 1.625, 1.875] let frequencies = factors.map { $0 * tonic } print(frequencies) // [261.62599999999998, 294.32925, 327.03249999999997, 348.74745799999994, 392.43899999999996, 425.14224999999999, 490.54874999999993] I want to do this by making the

Undefined Behavior with the C++0x Closure: II

冷暖自知 提交于 2019-12-07 15:20:25
问题 I find the use of the C++0x closure perplexing. My initial report, and the subsequent one, have generated more confusion than explanations. Below I will show you troublesome examples, and I hope to find out why there is an undefined behavior in the code. All the pieces of the code pass the gcc 4.6.0 compiler without any warning. Program No. 1: It Works #include <iostream> int main(){ auto accumulator = [](int x) { return [=](int y) -> int { return x+y; }; }; auto ac=accumulator(1); std::cout

Why aren't these two bits of JavaScript equivalent?

三世轮回 提交于 2019-12-07 14:26:49
问题 in jquery 1.4.2, ff 3.6.6: The following code produces three divs, which write messages to the firebug console as you would expect. However, if you uncomment out the loop and comment out the 3 lines doing it manually, it doesn't work - mousing over any of the divs results in "three" being written to the console. Why are these two methods any different than each other? In each one you use a selector to find the element and add an event to it. <head> <script type="text/javascript" src="/media

Reference Instance Variables in Javascript Constructor

戏子无情 提交于 2019-12-07 12:44:59
问题 I'm trying to maintain state on an object by doing something like this: obj = function() { this.foo = undefined; this.changeState = function () { (function () { this.foo = "bar" })(); // This is contrived, but same idea. }; }; I want to set the instance variable foo to "bar" when I call the changeState method. For instance: o = new obj(); o.changeState(); alert(o.foo); // This should say "bar" As far as I can tell, what is happening is that "this" in the inner anonymous function is pointing

Javascript scope referencing outer object

蹲街弑〆低调 提交于 2019-12-07 12:03:25
问题 Basically, I use a meta-class framework called Joose for Javascript that allows me to make use of a more elegant class syntax - but I don't know how I might go about referencing the scope of the object from within deeper methods of the class declaration. I also use require.js for dependemcy management... Here's an example class definition: define([ 'jquery', 'handlebars', ], function($, Handlebars){ var MyClass = Class("MyClass", { //inheritance isa: SuperClass, //instance vars has: { hello:{

Passing property type as parameter

为君一笑 提交于 2019-12-07 10:10:45
问题 Is there a way to pass the property to a function as a parameter ? class Car { let doors : Int = 4 let price : Int = 1000 } Is there a way to pass the Car property as a type to a function ? I would like to achieve the following: func f1(car: Car, property: SomeType) { println(car.property) } let c1 = Car() f1(c1, doors) f1(c1, price) Would closures help, if so how ? 回答1: I'm not sure this is what you want, but using closure: func f1<T>(car: Car, getter: Car -> T) { println(getter(car)) } let

python, confused in decorate and closure

旧巷老猫 提交于 2019-12-07 09:16:40
问题 I have some test code: def num(num): def deco(func): def wrap(*args, **kwargs): inputed_num = num return func(*args, **kwargs) return wrap return deco @num(5) def test(a): return a + inputed_num print test(1) when run this code, I got an error shows that 'inputed_num' is not defined My question is: In wrap function, is there not a closure that func can got 'inputed_num' ? Anyway, If not, how should I do to got my aim: Initialize some value, and use this value directly in the main function.

Dynamic class generation in CoffeeScript

别来无恙 提交于 2019-12-07 09:14:36
问题 What is the best way to dynamically create classes in CoffeeScript, in order to later instantiate objects of them? I have found ways to do it, but I am not sure if there is maybe an even better (or simpler) way to achieve it. Please let me know your thoughts on my code. Let's start with simple non-dynamic classes: class Animal constructor: (@name) -> speak: -> alert "#{@name} says #{@sound}" class Cat extends Animal constructor: (@name) -> @sound = "meow!" garfield = new Cat "garfield"