closures

Why can I not return a mutable reference to an outer variable from a closure?

风格不统一 提交于 2019-12-19 06:22:21
问题 I was playing around with Rust closures when I hit this interesting scenario: fn main() { let mut y = 10; let f = || &mut y; f(); } This gives an error: error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements --> src/main.rs:4:16 | 4 | let f = || &mut y; | ^^^^^^ | note: first, the lifetime cannot outlive the lifetime as defined on the body at 4:13... --> src/main.rs:4:13 | 4 | let f = || &mut y; | ^^^^^^^^^ note: ...so that closure can access

Why can I not return a mutable reference to an outer variable from a closure?

风流意气都作罢 提交于 2019-12-19 06:22:09
问题 I was playing around with Rust closures when I hit this interesting scenario: fn main() { let mut y = 10; let f = || &mut y; f(); } This gives an error: error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements --> src/main.rs:4:16 | 4 | let f = || &mut y; | ^^^^^^ | note: first, the lifetime cannot outlive the lifetime as defined on the body at 4:13... --> src/main.rs:4:13 | 4 | let f = || &mut y; | ^^^^^^^^^ note: ...so that closure can access

deeper understanding of closure in Javascript

风格不统一 提交于 2019-12-19 05:19:05
问题 I was reading the comments on an answer and saw this comment: [the closure] doesn't persist the state of foo so much as creates a special scope containing (1) the returned function and (2) all the external variables referenced at the time of the return. This special scope is called a closure. OK, so far so good. Now here is the interesting part that I didn't know about: Case in point... if you had another var defined in foo that was not referenced in the return function, it would not exist in

Do closures keep the whole execution context alive?

安稳与你 提交于 2019-12-19 03:42:24
问题 I understand that closures keep the execution context alive by saving a reference to the executed function. I'm wondering whether the whole context is saved or only the required parts. In the former case I need to structure the functions in a way that no memory is wasted. This should be the design goal anyway, but I would like to know if JavaScript takes care of it, too. Here's a simple example (on the basis of Single Page Web Applications , Mikowski/Powell, p. 56): var ctx; var outer

Why higher order procedures?

非 Y 不嫁゛ 提交于 2019-12-19 03:22:23
问题 So if a language provides higher order procedure then I can have procedure that returns procedure. Something like: (define (Proc a b c) (lambda (x) ( #| method body here in terms of a b c and x |# ))) To create new procedure, I would just do something like: (define ProcA (Proc a1 b1 c1)) ; Would create ProcA that has 1 argument Similar task could be done in a language which does not support higher order procedure by defining Proc that takes 4 instead of 3 arguments and calling this procedure

Why higher order procedures?

妖精的绣舞 提交于 2019-12-19 03:22:09
问题 So if a language provides higher order procedure then I can have procedure that returns procedure. Something like: (define (Proc a b c) (lambda (x) ( #| method body here in terms of a b c and x |# ))) To create new procedure, I would just do something like: (define ProcA (Proc a1 b1 c1)) ; Would create ProcA that has 1 argument Similar task could be done in a language which does not support higher order procedure by defining Proc that takes 4 instead of 3 arguments and calling this procedure

Use of Functional Interface in Java 8

不想你离开。 提交于 2019-12-19 03:07:25
问题 This is a follow up question from :: (double colon) operator in Java 8 in which Java allows you to refer to methods using :: operator. Is it possible to provide some custom Functional Interface that I create and use it with :: operator? And how to do it? 回答1: “Is it possible to provide some custom Functional Interface that I create and use it with :: operator? And how to do it?” It is possible and it’s as easy as you could imagine: just create an interface with exactly one method. You don’t

Why does this closure-scoped variable lose its value?

与世无争的帅哥 提交于 2019-12-19 03:07:10
问题 I saw this Javascript quiz here: http://www.netfxharmonics.com/2008/01/NetFX-Harmonics-JavaScript-Quiz and I could not figure out this problem: (function(){ var a = 1; var b = 2; (function( ) { a = b; var b; })( ); console.log('a:'+ a); // => "a:undefined" console.log('b:'+ b); // => "b:2" })() However, if you remove the var b; declaration from the inner function, then a == 2 as you would expect. Why is this happening? (You can play with it here: http://jsfiddle.net/gnhMZ/) 回答1: It's

How do closures capture values from previous calls?

孤街醉人 提交于 2019-12-18 22:31:14
问题 typealias IntMaker = (Void)->Int func makeCounter() ->IntMaker{ var n = 0 // Line A func adder()->Integer{ n = n + 1 return n } return adder } let counter1 = makeCounter() counter1() // returns 1 counter1() // returns 2 counter1() // returns 3 Isn't 'Line A' called each time we call counter1() ? meaning that var n = 0 should be called every time... Why is that the counter returns different values? Shouldn't they always be returning '1' ? 回答1: You've called makeCounter() once. That creates

Running unit tests on nested functions

元气小坏坏 提交于 2019-12-18 21:52:03
问题 I come from the Java world, where you can hide variables and functions and then run unit tests against them using reflection. I have used nested functions to hide implementation details of my classes so that only the public API is visible. I am trying to write unit tests against these nested functions to make sure that I don't break them as I develop. I have tried calling one of the nested functions like: def outer(): def inner(): pass outer.inner() which results in the error message: