closures

Swift behaves differently on debug and release mode

一个人想着一个人 提交于 2019-12-05 15:57:44
问题 Not sure if that's an issue with Swift, XCode or Alamofire but I recognized strange behavior on different places within my mixed Swift/Objc app . It only happens in parts which are written in Swift and use closures/networking. Here's an example code where it happens: Alamofire.request(.DELETE, "http://someUrl.com/user", parameters: nil) .response { (request, response, data, error) in // some cleanup code and an alert } When I run my app in Debug mode on my iPhone then it all just works , the

odd lambda behavior

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 15:23:20
问题 I stumbled across this article and found it very interesting, so I ran some tests on my own: Test One: List<Action> actions = new List<Action>(); for (int i = 0; i < 5; ++i) actions.Add(() => Console.WriteLine(i)); foreach (Action action in actions) action(); Outputs: 5 5 5 5 5 Test Two: List<Action> actions = new List<Action>(); for (int i = 0; i < 5; ++i) { int j = i; actions.Add(() => Console.WriteLine(j)); } foreach (Action action in actions) action(); Outputs: 0 1 2 3 4 According to the

PHP: Pass anonymous function as argument

我怕爱的太早我们不能终老 提交于 2019-12-05 15:02:21
问题 Is it possible to pass an anonymous function as an argument, and have it execute immediately, thus passing the function's return value? function myFunction(Array $data){ print_r($data); } myFunction(function(){ $data = array( 'fruit' => 'apple', 'vegetable' => 'broccoli', 'other' => 'canned soup'); return $data; }); This throws an error due to the Array type-hint, complaining of an object being passed. Alright, if I remove the type-hint, it of course spits out Closure Object , rather than the

Python closures and cells (closed-over values)

帅比萌擦擦* 提交于 2019-12-05 14:43:13
What is the Python mechanism that makes it so that [lambda: x for x in range(5)][2]() is 4? What is the usual trick for binding a copy of x to each lamba expression so that the above expression will equal 2? My final solution: for template, model in zip(model_templates, model_classes): def create_known_parameters(known_parms): return lambda self: [getattr(self, p.name) for p in known_parms] model.known_parameters = create_known_parameters(template.known_parms) I usually use functools.partial : [ partial(lambda x: x, x) for x in range(5) ] Or, of course, you can do it yourself: [ (lambda x:

Can't pass event to addEventListener: closure issue

不羁岁月 提交于 2019-12-05 14:42:10
问题 This one's driving me crazy... I have a loop which adds an event listener to an SVG object. The object is, for the sake of argument, a small circle, and I have to add mouseover and mouseout events for each of the 10 circles. My first problem is the standard closure-scope thing - because all the listeners are added in the same loop, they all see the same invalid value of the loop variable. I can fix this, I think, but the second problem is that I have to pass 'event' to the listeners, and I

Passing property type as parameter

旧巷老猫 提交于 2019-12-05 14:39:27
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 ? I'm not sure this is what you want, but using closure: func f1<T>(car: Car, getter: Car -> T) { println(getter(car)) } let c1 = Car() f1(c1, {$0.doors}) f1(c1, {$0.price}) You can use key value coding: class Car : NSObject { let

Function closure vs. callable class

不羁岁月 提交于 2019-12-05 13:53:16
问题 In many cases, there are two implementation choices: a closure and a callable class. For example, class F: def __init__(self, op): self.op = op def __call__(self, arg1, arg2): if (self.op == 'mult'): return arg1 * arg2 if (self.op == 'add'): return arg1 + arg2 raise InvalidOp(op) f = F('add') or def F(op): if op == 'or': def f_(arg1, arg2): return arg1 | arg2 return f_ if op == 'and': def g_(arg1, arg2): return arg1 & arg2 return g_ raise InvalidOp(op) f = F('add') What factors should one

inout parameter in closure crashes the Swift compiler

余生颓废 提交于 2019-12-05 13:30:13
问题 All I need to do is start a new project in Swift and add to main.swift struct Foo { let bar: (inout baz: String) -> () } When I try to build I get an error: Command failed due to signal: Segmentation fault: 11 Am I doing anything wrong? I thought that perhaps inout parameters in closures are not supported, but if I define a closure like so: let baz: (inout baz: String) -> () = { baz in baz += "x" return } or even var baz: (inout baz: String) -> ()? it compiles and runs OK 回答1: Just tested it

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

南笙酒味 提交于 2019-12-05 13:30:01
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( 'touchmove', function(e) { x = something; y = something; i++; $mySelection.doSomething(); // more code.. });

Lambda treated as a closed delegate in Visual Studio 2015

99封情书 提交于 2019-12-05 12:41:10
I was surprised that there would be any runtime difference between these two delegates ( fn1 and fn2 ): static int SomeStaticMethod(int x) { return x; } // fn1.Target == null in this case Func<int, int> fn1 = SomeStaticMethod; // fn2.Target != null in this case Func<int, int> fn2 = x => x; But apparently the second lambda is treated like an instance method, because its Target property is non-null. And it was treated differently before I switched to Visual Studio 2015 (in VS2012, I am pretty sure it was treated as a static method). Is there a reason why a lambda with no closures is treated as a