closures
Big execution time difference between java Lambda vs Anonymous class
UPDATE A few comments wondering if my benchmark at the bottom was flawed – after introducing a lot of randomness (to prevent the JIT from optimising too much stuff), I still get similar results so I tend to think it is ok. In the meantime, I have come across this presentation by the lambda implementation … Read more
Why should grails actions be declared as methods instead of closures and what difference does it make?
The answer is here From above link Leveraging methods instead of Closure properties has some advantages: Memory efficient Allow use of stateless controllers (singleton scope) You can override actions from subclasses and call the overridden superclass method with super.actionName() Methods can be intercepted with standard proxying mechanisms, something that is complicated to do with Closures … Read more
How to avoid access mutable variable from closure
You need to create a scope to correctly capture tmp_id using a self-executing function. That’s because the entire for loop is one scope, meaning each time through, you’re capturing the same variable. So the callback will end up with the wrong ids, because temp_id‘s value will get changed before the callback is called. I’d ignore … Read more
Why can’t Python increment variable in closure?
You can’t re-bind closure variables in Python 2. In Python 3, which you appear to be using due to your print(), you can declare them nonlocal: def foo(): counter = 1 def bar(): nonlocal counter counter += 1 print(“bar”, counter) return bar bar = foo() bar() Otherwise, the assignment within bar() makes the variable local, … Read more
Modify bound variables of a closure in Python
It is quite possible in python 3 thanks to the magic of nonlocal. def foo(): var_a = 2 var_b = 3 def _closure(x, magic = None): nonlocal var_a if magic is not None: var_a = magic return var_a + var_b + x return _closure localClosure = foo() # Local closure is now “return 2 + … Read more
@noescape attribute in Swift 1.2
@noescape can be used like this: func doIt(code: @noescape () -> ()) { /* what we CAN */ // just call it code() // pass it to another function as another `@noescape` parameter doItMore(code) // capture it in another `@noescape` closure doItMore { code() } /* what we CANNOT do ***** // pass it as … Read more
Blocks on Swift (animateWithDuration:animations:completion:)
The completion parameter in animateWithDuration takes a block which takes one boolean parameter. In Swift, like in Obj-C blocks, you must specify the parameters that a closure takes: UIView.animateWithDuration(0.2, animations: { self.blurBg.alpha = 1 }, completion: { (value: Bool) in self.blurBg.hidden = true }) The important part here is the (value: Bool) in. That tells … Read more
What does $0 and $1 mean in Swift Closures?
$0 is the first parameter passed into the closure. $1 is the second parameter, etc. That closure you showed is shorthand for: let sortedNumbers = numbers.sort { (firstObject, secondObject) in return firstObject > secondObject }
How to use Swift @autoclosure
Consider a function that takes one argument, a simple closure that takes no argument: func f(pred: () -> Bool) { if pred() { print(“It’s true”) } } To call this function, we have to pass in a closure f(pred: {2 > 1}) // “It’s true” If we omit the braces, we are passing in an … Read more