closures

local variable scope in linq anonymous method ( closure)

被刻印的时光 ゝ 提交于 2019-12-01 02:25:26
问题 What is the scope of local variable declared in Linq Query. I was writing following code static void Evaluate() { var listNumbers = Enumerable.Range(1, 10).Select(i => i); int i = 10; } Compiler flagged error on line int i=10, stating A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else I am unable to understand why this error is coming. My understanding was that i will

Determining, if a variable is a valid closure in PHP

不羁的心 提交于 2019-12-01 02:08:21
Using the following function: function is_closure($t) { return ( !is_string($t) && is_callable($t)); } Can this return true for anything else, than an anonymous closure function? If so, what would be the correct way to determine, if a variable is a closure? Many thanks mario The most deterministic way to check if a callback is an actual closure is: function is_closure($t) { return $t instanceof Closure; } All anonymous functions are represented as objects of the type Closure in PHP. (Which, coming back to above comment, happen to implement the __invoke() method.) Phil I think you can use

Function inside function - every time?

∥☆過路亽.° 提交于 2019-12-01 02:07:29
Let we have this code: def big_function(): def little_function(): ....... ......... The Python documentation says about def statement: A function definition is an executable statement. Its execution binds the function name... So, the question is: Does def little_function() execute every time when big_function is invoked? Question is about def statement exactly, not the little_function() body. You can check the bytecode with the dis module: >>> import dis >>> def my_function(): ... def little_function(): ... print "Hello, World!" ... ... >>> dis.dis(my_function) 2 0 LOAD_CONST 1 (<code object

CasperJs, how to repeat a step X times onWaitTimeout?

♀尐吖头ヾ 提交于 2019-12-01 01:51:42
So what I want to do is create a casperJS function which allows us to repeat a step X times, by refreshing the page first, when this step function reaches the timeout. For unreliable test due to a specific page bug/freeze at the moment and reduce the percentage of false negative. I have just a problem, I don't know how to break this loop, because I'm in IIFE scope, see following code : var echoTest = function(){ casper.echo('Hi'); }; var trueFunction = function(){ return true; }; var verifyFailedTest = function(number, trueReturn, thenFunction){ var i = 0; //outer: <------- for (; i <= number;

VB.Net - “With” and Closures don't mix

一世执手 提交于 2019-12-01 01:37:15
问题 Just thought I'd share this in case anyone else has run into this. I did something similar today and it took me a while to figure out why this was causing a problem at runtime. This code: Public Class foo Public bar As String = "blah" End Class Public Sub DoInline() Dim o As New foo Dim f As Func(Of String) With o f = Function() .bar End With Try Console.WriteLine(f.DynamicInvoke()) Catch ex As Reflection.TargetInvocationException Console.WriteLine(ex.InnerException.ToString) End Try End Sub

Why nested iterator closures won't copy values from outer scope

你。 提交于 2019-12-01 01:17:01
问题 I'm trying to use nested iterators, where the inner iterator uses value from the outer iterator. vec![0;10].iter().flat_map(|&a| { (0..10).map(|b|{ a + b }) }); error: a does not live long enough (0..10).map(|b|{ ^^^ note: reference must be valid for the method call... This compiles if I move the inner closure ( move |b|{ ), but I don't understand why it is necessary, given that a is an integer and could have been copied instead of moved. 回答1: Both flat_map and map are lazy. The inner map

Swift: Unable to decompose tuple in certain closures (e.g., reduce with enumerate)?

◇◆丶佛笑我妖孽 提交于 2019-12-01 00:40:41
When using map() with enumerate(), Swift will decompose the enumerate tuple: map(enumerate([1,2,3])) { (index, element) in index + element } However, this does not appear to work alongside an additional closure parameter (e.g., with reduce()): reduce(enumerate([1,2,3]), 0) { (accum, (index, element)) in accum + index + element } This fails with error: use of undeclared type 'index' . Am I missing something simple, or does Swift simply not allow decomposing a tuple alongside an additional parameter? I have tried this in 1.1 and 1.2. (For now, I am using the shorthand argument names.) The answer

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

旧街凉风 提交于 2019-12-01 00:32:26
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 line: ((MyBlock)[blocksDict objectForKey: key])(); Although, calling the same function from Objective-C

Type mismatch “bound lifetime parameter” vs “concrete lifetime” when filling a collection from a closure

南笙酒味 提交于 2019-11-30 23:53:53
问题 I am trying to find repetitions in an iterable sequence. Furthermore, I want to know the elements that occurred in that sequence up to that point. I created a HashMap and am trying to call insert on it from within a closure used by take_while . However, I have so far not managed to get it to compile due to type mismatches related to concrete / bound lifetimes. Here is a simplified version of my code which exhibits the same error: use std::collections::HashSet; fn main() { let mut seq =

Are PHP Closure Objects eligible for garbage collection

我们两清 提交于 2019-11-30 22:52: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). 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 garbage-collected. This is not the same as using create_function , as PHP throws the create_function