closures

Return object for a method inside completion block

夙愿已清 提交于 2019-12-23 02:34:45
问题 I want to make a method with URL parameter that returns the response of calling that URL. How can I return the data obtained inside a completion block for a method? class func MakeGetRequest(urlString: String) -> (data: NSData, error: NSError) { let url = NSURL(string: urlString) var dataResponse: NSData var err: NSError let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in //How can I return the data obtained here.... }) task

Why do we need blocks, function literals, closures in programming languages?

 ̄綄美尐妖づ 提交于 2019-12-23 01:59:22
问题 I program in Objective-C, I know a bit of Scala, Python and Javascript. While I'm comfortable with blocks in Obj-C, I would like to know what specific problem do they solve that I couldn't with the earlier versions of the language. Also - are blocks, closures, function literals, named functions, anonymous functions - one and the same thing? If you can answer with some code examples that would be great. 回答1: First of all, in order to answer the question in the title: Why do we need blocks,

JavaScript Closure: Returning a Function

本秂侑毒 提交于 2019-12-23 01:41:56
问题 I am working my way through a JavaScript lecture series by Douglas Crockford. I am confused by a code example he is showing to illustrate 'closure': var digit_name = (function () { var names = ['zero', 'one', 'two', 'three']; return function (n) { return names[n]; }; }()); alert(digit_name(3)); How/why can digit_name take an argument when no parameter is specified in the definition (the outermost function)? How does the argument (in this case 3 ) know to correspond to n within the inner

How to Test That a static Constant Value Is Passed Into MailService sendMail closure WIthout Requiring a class Prefix on the Constant?

自作多情 提交于 2019-12-23 01:40:46
问题 My grails Unit Test mocks out the MailService using an Expando and redefining the sendMail method. The original code uses a static constant called EMAIL_SUBJECT without a prefix. However, when I run the test and remove from Something.EMAIL_SUBJECT the "Something." it fails the assertion saying that mockMailService.subject was passed a null How can I improve the test so the "Something." is not needed on EMAIL_SUBJECT? Snippet of Test Code: def createMockMailService() { def mockMailService =

Swift 3.0 closure expression: what if the variadic parameters not at the last place in the parameters list?

旧街凉风 提交于 2019-12-23 00:53:08
问题 Update at 2016.09.19 There is a tricky, indirect way to use variadic parameters before some other parameters in closure expression parameters list, haha let testClosure = { (scores: Int...) -> (_ name: String) -> String in return { name in return "Happy" } } let k = testClosure(1, 2, 3)("John") And I found some related issues in bugs.swift.org: SR-2475 SR-494 Original Post According to the document of Swift 3.0, for a closure expression, "variadic parameters can be used if you name the

How to create new closure cell objects?

谁说胖子不能爱 提交于 2019-12-22 16:40:10
问题 I need to monkey-patch my library to replace an instance of a symbol, and it's getting referenced by some function closures. I need to copy those functions (since I also need access to original unpatched version of the function as well), but __closure__ is immutable, and I can't copy.copy it, so how can I create new closure cells objects in Python 2.7? I for example given this function def f(): def incorrectfunction(): return 0 def g(): return incorrectfunction() return g def correctfunction(

Same object different address. Why?

匆匆过客 提交于 2019-12-22 15:03:17
问题 Since both f and bar[42]! point to the same closure in the following code I would expect the unsafe pointers to point to the same address. They do not. Can anyone please explain why? To clarify: I'm looking up the address returned by withUnsafePointer in Xcode using "view memory". var bar = [Int : (() -> Void)]() bar[42] = { print("foo") } var f = bar[42]! f() // prints "foo" bar[42]!() // prints "foo" withUnsafePointer(to: &f) { print( type(of: $0) ) ; print( $0 ) } // UnsafePointer<(()) ->

why is IIFE needed to create a new scope?

大憨熊 提交于 2019-12-22 11:16:23
问题 From You Don't Know JS: for (var i=1; i<=5; i++) { setTimeout( function timer(){ console.log( i ); }, i*1000 ); } gives 6 6 6 6 6 but using an IIFE like so for (var i=1; i<=5; i++) { (function(){ var j = i; setTimeout( function timer(){ console.log( j ); }, j*1000 ); })(); } gives 1 2 3 4 5 My question: why doesn't for (var i=1; i<=5; i++) { setTimeout( function timer(){ var j = i; console.log( j ); }, i*1000 ); } or for (var i=1; i<=5; i++) { function timer() { var j = i; console.log(j); }

Have Python 2.7 functions remember value and not reference? Closure Weirdness

こ雲淡風輕ζ 提交于 2019-12-22 11:10:58
问题 I'm trying to return from a function a list of functions, each of which uses variables from the outside scope. This isn't working. Here's an example which demonstrates what's happening: a = [] for i in range(10): a.append(lambda x: x+i) a[1](1) # returns 10, where it seems it should return 2 Why is this happening, and how can I get around it in python 2.7 ? 回答1: The i refers to the same variable each time, so i is 9 in all of the lambdas because that's the value of i at the end of the loop.

how can I pass argument (names) to a function factory?

我的梦境 提交于 2019-12-22 10:59:55
问题 I need to build a lot of functions with lots of different arguments, though they otherwise share a lot of code and structure. To avoid duplication, I thought I'd be clever and build myself a function factory (aka closure). I can't figure out how to pass the function arguments inside the function factory . My use case is a bunch of S3 constructor functions, all of which share the same validation mechanism. So I'll use that as an example to explain my problem. Say, I have a ClassA and ClassB ,