closures

Understanding shorthand closure syntax for map function in Swift

无人久伴 提交于 2019-12-06 07:03:17
问题 I'm trying to understand some of the short hand syntax used by the map function. The following is the setup let array = [1, 2, 3] // these make sense let arr1 = array.map({String($0)}) let arr2 = array.map{String($0)} let arr3 = array.map({ number in return String(number) }) let arr4 = array.map({ (number) -> String in String(number) }) Here is where the confusion lays. In swift I can forgo the curly braces for map, but this seems like something that can't be done, for my own functions where

Why use (function(){})() or !function(){}()?

限于喜欢 提交于 2019-12-06 06:44:01
问题 I was reading In JavaScript, what is the advantage of !function(){}() over (function () {})()? then it hit me, why use : (function(){})() or !function(){}() instead of just function(){}() ? Is there any specific reason? 回答1: It depends on where you write this. function(){}() by itself will generate a syntax error as it is evaluated as function declaration and those need names. By using parenthesis or the not operator, you enforce it to be interpreted as function expression , which don't need

“Side-effecting lexical closure” vs function in Scala

别等时光非礼了梦想. 提交于 2019-12-06 06:08:29
问题 In his answer's comment section, Apocalisp states the following: Well, you did ask for a function. A side-effenting [sic] lexical closure is emphatically not a function. What exactly does he mean by "side-effecting lexical closure", and how is that different from a function? My guess is that they're trying to differentiate functions in a functional programming sense - where no side effects are allowed (such as changing state of variables or outputting values), from mere procedures , which do

How do I return a boxed closure from a method that has a reference to the struct?

痞子三分冷 提交于 2019-12-06 06:06:21
问题 I have a structure that contains a value and I want to obtain a function that operates on this value: struct Returner { val: i32, } impl<'a> Returner { fn get(&'a self) -> Box<Fn(i32) -> i32> { Box::new(|x| x + self.val) } } This fails compilation: error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements --> src/main.rs:7:18 | 7 | Box::new(|x| x + self.val) | ^^^^^^^^^^^^^^^^ | note: first, the lifetime cannot outlive the lifetime 'a as defined on the impl at 5:1...

How to create an 'closure function' in Matlab as in python and js?

冷暖自知 提交于 2019-12-06 05:37:30
Background In Python and JS we have closures, which returns a function with some of the variables pre-defined. e.g. def make_printer(msg): def printer(): print msg return printer Do we have similar stuff in Matlab? Here I have a callback function function show(object, eventdata) that is to be set to the callback of my GUI func = @show; set(gcf, 'WindowButtonMotionFcn', func); However, I want to add some additional parameters to this show function. Currently I'm using global variables to do that. But I think it would be elegant if we have a 'closures function'. About anonymous function Yes we

Setting a variable in the closure scope

瘦欲@ 提交于 2019-12-06 05:33:05
问题 I think I understand why variables exist outside of the function they were declared in, because you're returning another function: myFunction = function() { var closure = 'closure scope' return function() { return closure; } } A = myFunction(); // myFunction returns a function, not a value B = A(); // A is a function, which when run, returns: console.log(B); // 'closure scope' The way that it's written now, calling A() is like a getter. Q: How can I write myFunction so that calling A(123) is

Why do javascript variables in closure functions not reset to a default when called multiple times?

风流意气都作罢 提交于 2019-12-06 05:05:48
问题 In the code below please can someone explain to me why multiple calls to counter result in the value of i increasing each time it is called? My understanding is that as we specifically set i = 0; in makeCounter , each time makeCounter is called through the counter variable, i should be reset to 0. I cannot understand why this is not the case. function makeCounter() { // `i` is only accessible inside `makeCounter`. var i = 0; return function() { console.log( ++i ); }; } // Note that `counter`

overriding a function defined in jquery closure

烂漫一生 提交于 2019-12-06 04:43:03
I have a function defined in jquery closure and called by another function in the same closure. Could I override the function being called without changing the closure itself. See the code for example (function($){ function Myfunction(value) { //do something with the value } $('a').live('click',function(){ MyFunction($(this).val()) }); }(JQuery)); is there a way I can override Myfunction so that overriden copy of Myfuntion is called inside event handler. carlosfigueira You can't. Functions defined inside closures are private - and they: [cannot] be accessed directly from outside the anonymous

Swift 4: Find value in nested, dynamic Dictionary<String, Any> recursively

谁说胖子不能爱 提交于 2019-12-06 04:40:40
问题 In a given Dictionary, I need to find a nested Dictionary ( [String : Any] ) for a given key. The general structure of the Dictionary (e.g. nesting levels, value types) is unknown and given dynamically. [1] Inside of this sub-Dictionary, there is a given value for the key "value" (don't ask) which needs to be fetched. Here's an example: let theDictionary: [String : Any] = [ "rootKey" : [ "child1Key" : "child1Value", "child2Key" : "child2Value", "child3Key" : [ "child3SubChild1Key" :

Same object different address. Why?

流过昼夜 提交于 2019-12-06 04:22:05
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<(()) -> ()> 0x00007fff5fbff778 -> 0x100002100 withUnsafePointer(to: &bar[42]!) { print( type(of: $0) ) ; print(