closures

Swift behaves differently on debug and release mode

て烟熏妆下的殇ゞ 提交于 2019-12-04 01:17:57
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 cleanup code and the alert get presented like they should when I do the "delete account" action which

How do I “generify” a closure type alias in Swift?

Deadly 提交于 2019-12-04 01:10:04
In order to make my code easier to read, I am using type aliases in Swift for various types of closures. I have the following basic set of closures: public typealias FailureClosure = (error: NSError?) -> Void public typealias ProgressClosure = (progress: Float32) -> Void public typealias BasicClosure = () -> Void I would like to add a closure typealias that supports generic arrays, but I can't seem to figure out the syntax for it. This is as far as I am able to get, but I get the compile time error " Use of undeclared type 'T' " public typealias ArrayClosure = <T>(array:[T]?) -> Void Does

PHP: Pass anonymous function as argument

為{幸葍}努か 提交于 2019-12-04 00:53:19
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 results I want. I understand that I am technically passing an object instance of Closure to myFunction

inout parameter in closure crashes the Swift compiler

我们两清 提交于 2019-12-04 00:29:51
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 Just tested it in Swift 1.2 shipped with Xcode 6.3 beta, and it compiled successfully. So it was definitely a bug on the

Function closure vs. callable class

巧了我就是萌 提交于 2019-12-04 00:27:27
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 consider in making the choice, in either direction? I can think of two: It seems a closure would always

python closure with assigning outer variable inside inner function

旧街凉风 提交于 2019-12-04 00:11:15
问题 I've got this piece of code: #!/usr/bin/env python def get_match(): cache=[] def match(v): if cache: return cache cache=[v] return cache return match m = get_match() m(1) if I run it, it says: UnboundLocalError: local variable 'cache' referenced before assignment but if I do this: #!/usr/bin/env python def get(): y = 1 def m(v): return y + v return m a=get() a(1) it runs. Is there something with list? or my code organizing is wrong? 回答1: The problem is that the variable cache is not in the

Checking if an RDD element is in another using the map function

耗尽温柔 提交于 2019-12-03 23:09:40
I'm new to Spark and was wondering about closures. I have two RDDs, one containing a list of IDs and a values, and the other containing a list of selected IDs. Using a map, I want to increase the value of the element, if the other RDD contains its ID, like so. val ids = sc.parallelize(List(1,2,10,5)) val vals = sc.parallelize(List((1, 0), (2, 0), (3,0), (4,0))) vals.map( v => { if(ids.collect().contains(v._1)){ (v._1, 1) } }) However the job hangs and never completes. What is the proper way to do this, Thanks for your help! Your implementation tries to use one RDD ( ids ) inside a closure used

CasperJs, how to repeat a step X times onWaitTimeout?

99封情书 提交于 2019-12-03 23:05:32
问题 So what I want to do is create a casperJS function which allows us to repeat a step X times, by refreshing the page first, when this step function reaches the timeout. For unreliable test due to a specific page bug/freeze at the moment and reduce the percentage of false negative. I have just a problem, I don't know how to break this loop, because I'm in IIFE scope, see following code : var echoTest = function(){ casper.echo('Hi'); }; var trueFunction = function(){ return true; }; var

Swift: Unable to decompose tuple in certain closures (e.g., reduce with enumerate)?

一笑奈何 提交于 2019-12-03 22:50:02
问题 When using map() with enumerate(), Swift will decompose the enumerate tuple: map(enumerate([1,2,3])) { (index, element) in index + element } However, this does not appear to work alongside an additional closure parameter (e.g., with reduce()): reduce(enumerate([1,2,3]), 0) { (accum, (index, element)) in accum + index + element } This fails with error: use of undeclared type 'index' . Am I missing something simple, or does Swift simply not allow decomposing a tuple alongside an additional

Generic completion handler in Swift

試著忘記壹切 提交于 2019-12-03 21:43:26
I have a method which has a method named performRequest() . It takes a JSONRequest parameter. JSONRequest looks something like this: public typealias JSONCompletionHandler = ([Entity]?, NSError?) -> Void public class JSONRequest: Request { public var completionHandler: JSONCompletionHandler public var endPoint: String } And performRequest() looks like this: public func performJSONRequest<T where T: Entity>(jsonRequest: JSONRequest, _: Type) { // Make a request which returns a data object var entities = self.convertJSONData(data, jsonKey: jsonRequest.jsonKey, T.self) // Error: 'T' is not