closures

Javascript:What is meaning of sum(2)(3) //returns 5;

孤者浪人 提交于 2019-11-30 20:21:32
问题 Here is code blow to return it's value. function sum(a){ return function(b){ return a+b; } } sum(2)(3); It returns 5 but if I type code: function sum(a){ function add(b){ return a+b; } return add(b); } It doesn't return expected value 5. I don't even understand how sum(2)(3) calls function. Any explanation for this is very much appreciated. 回答1: This is called a closure. sum(a) returns a function that takes one parameter, b , and adds it to a . Think of it like this: sum(2)(3); // Is

Error: Bool is not Convertible to Void:

夙愿已清 提交于 2019-11-30 20:14:58
I am moving my code from Obj. C to Swift C and trying to implementing the Twitter sdk.. But, I am getting an error... Can any body tell me what I have done wrong. Please help me with this. I spent 2 days tried everything but didn't work for me. Your block does not have a return statement, therefore the compiler uses the result of the last statement UIApplication.sharedApplication().openURL(url) as return value, which is a Bool and not Void as declared in the block signature. To solve that problem, just add a return statement: { (url: NSURL, oauthToken: String) -> Void in UIApplication

Partial application of protocol method is not allowed

蓝咒 提交于 2019-11-30 19:46:41
Can someone explain this error and why this works with closure? If you change 'Test' to 'A' inside 'B' class everything works in both cases. beta 7 protocol Test { func someFunc() -> String var someClosure: () -> Int { get } } class A: Test { func someFunc() -> String { return "A String" } var someClosure: () -> Int { return { return 2 } } } class B { let a: Test let aString: () -> String let aInt: () -> Int init(a: Test){ self.a = a aString = a.someFunc // Error: Partial application of protocol method is not allowed aInt = a.someClosure // Works fine } } UPDATE Also here is my weird

Is it possible to create in Java 8 a unlimitedly growing in lazy way collection, defined by recursion?

淺唱寂寞╮ 提交于 2019-11-30 19:29:20
I can create a recursive closure: static IntUnaryOperator fibo; fibo = (i) -> i<2 ? 1 : fibo.applyAsInt(i-1)+ fibo.applyAsInt(i-2); But of course, it has sense only as an example. To be useful, such collection should keep already once counted elements and get() them without recounting. The counting of elements should happen in lazy way, at first need. Thus, no member will have to be calculated more than once. In such way we'll a structure that will look like a recursively defined sequence, and will be fast and reusable. When I started to study Java 8 I thought that Stream works in that way.

How to call an objective-c function which accepts Dictionary of blocks as argument from Swift?

烈酒焚心 提交于 2019-11-30 19:00:19
问题 I have a function in my objective c file (lets say class MyBlockExecutor): + (void) runBlockFromDictionary: (NSDictionary*) blocksDict andKey: (NSString*) key { if ( [blocksDict objectForKey: key] != nil ) { ((MyBlock)[blocksDict objectForKey: key])(); } } Now, I want to call this function from Swift. Here is my swift call: MyBlockExecutor.runBlock(from: [ "key1":{ ()->Void in print("block for key1 called") } ], andKey: "key1") This crashes my app. I am getting EXC_BAD_ACCESS error on this

PHP closures and implicit global variable scope

我与影子孤独终老i 提交于 2019-11-30 18:48:48
Is there a way that one can implicitly declare top-level variables as global for use in closures? For example, if working with code such as this: $a = 0; //A TOP-LEVEL VARIABLE Alpha::create('myAlpha') ->bind(DataSingleton::getInstance() ->query('c') ) ->addBeta('myBeta', function($obj){ $obj->bind(DataSingleton::getInstance() ->query('d') ) ->addGamma('myGamma', function($obj){ $obj->bind(DataSingleton::getInstance() ->query('a') ) ->addDelta('myDelta', function($obj){ $obj->bind(DataSingleton::getInstance() ->query('b') ); }); }) ->addGamma('myGamma', function($obj){ $a++; //OUT OF MY SCOPE

python: how binding works

前提是你 提交于 2019-11-30 18:39:12
I am trying to understand, how exactly variable binding in python works. Let's look at this: def foo(x): def bar(): print y return bar y = 5 bar = foo(2) bar() This prints 5 which seems reasonable to me. def foo(x): def bar(): print x return bar x = 5 bar = foo(2) bar() This prints 2, which is strange. In the first example python looks for the variable during execution, in the second when the method is created. Why is it so? To be clear: this is very cool and works exactly as I would like it to. However, I am confused about how internal bar function gets its context. I would like to understand

Cannot convert value of type '() -> _' to specified type 'Town.Size'

孤人 提交于 2019-11-30 18:29:56
Am getting this issue with this struct, on the line which reads "lazy var townSize: Size ={" and can't figure out what the issue is. struct Town { let region = "South" var population = 5422 var numberOfStoplights = 4 enum Size { case Small case Medium case Large } lazy var townSize: Size = { switch self.population { case 0...10000: return Size.Small case 10001...100000: return Size.Medium default: return Size.Large } } func printTownDescription() { print("Population: \(myTown.population), number of stoplights: \(myTown.numberOfStoplights)") } mutating func changePopulation(amount: Int) {

“error: closure may outlive the current function” but it will not outlive it

做~自己de王妃 提交于 2019-11-30 18:24:14
When I try to compile the following code: fn main() { (...) let mut should_end = false; let mut input = Input::new(ctx); input.add_handler(Box::new(|evt| { match evt { &Event::Quit{..} => { should_end = true; } _ => {} } })); while !should_end { input.handle(); } } pub struct Input { handlers: Vec<Box<FnMut(i32)>>, } impl Input { pub fn new() -> Self { Input {handlers: Vec::new()} } pub fn handle(&mut self) { for a in vec![21,0,3,12,1] { for handler in &mut self.handlers { handler(a); } } } pub fn add_handler(&mut self, handler: Box<FnMut(i32)>) { self.handlers.push(handler); } } I get this

Are PHP Closure Objects eligible for garbage collection

会有一股神秘感。 提交于 2019-11-30 17:55:18
问题 I was wondering if anyone knows if PHP's anonymous functions are eligible for garbage collection? I know that functions created with create_function are not garbage collected but I haven't been able to find any reference about ones created with the function(){} syntax (internally represented as a Closure object). 回答1: PHP's garbage collector does not discriminate between types of "things" - if it has at least one reference somewhere, it is kept. The moment this does not apply, the resource is