closures

local variable scope in linq anonymous method ( closure)

大憨熊 提交于 2019-12-01 04:46:50
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 become out of scope after first line (in foreach loop). So i can be declared again. Actual behavior is

What is Closures/Lambda in PHP or Javascript in layman terms? [duplicate]

筅森魡賤 提交于 2019-12-01 04:23:48
This question already has an answer here: What is the difference between a 'closure' and a 'lambda'? 11 answers What are Closures/Lambda in PHP or JavaScript in layman terms? An Example would be great to aid my understanding. I am assumning Lambda and Closures are the same thing? simshaun SO already has the answers: What is a lambda (function)? How do JavaScript closures work? A lambda is an anonymous function. A closure is a function that carries its scope with it. My examples here will be in Python, but they should give you an idea of the appropriate mechanisms. print map(lambda x: x + 3, (1

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

混江龙づ霸主 提交于 2019-12-01 04:09:46
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 Throws a NullReferenceException. It seems as though the With is using the closure as its temp storage,

Deducing PHP Closure parameters

耗尽温柔 提交于 2019-12-01 03:46:40
Is there any chance that I can deduce PHP Closure parameters type information? Consider this example: <?php $foo = function(array $args) { echo $args['a'] . ' ' . $args['b']; }; $bar = function($a, $b) { echo $a . ' ' . $b; }; $closure = /* some condition */ $foo : $bar; if(/* $closure accepts array? */) { call_user_func($closure, ['a' => 5, 'b' => 10]); } else { call_user_func($closure, 5, 10); } ?> I want to leave some freedom for user so he or she could decide which way is better to define a Closure that will be registered in my dispatcher - will it accept parameters in associative array or

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

守給你的承諾、 提交于 2019-12-01 03:30: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 = HashSet::new(); let mut insert = |k| seq.insert(k); (1..10).cycle().take_while(insert); } Here are the errors

In closure, what triggers a new instance of the captured variable?

你。 提交于 2019-12-01 03:23:17
问题 I'm reading Jon Skeet's C# in Depth. On page 156 he has an example, Listing 5.13 "Capturing multiple variable instantiations with multiple delegates". List<ThreadStart> list = new List<ThreadStart>(); for(int index=0; index < 5; index++;) { int counter = index*10; list.Add(delegate { Console.WriteLine(counter); counter++; } ); } foreach(ThreadStart t in list) { t(); } list[0](); list[0](); list[0](); list[1](); In the explanation after this listing, he says "each of the delegate instances has

python closure with assigning outer variable inside inner function

為{幸葍}努か 提交于 2019-12-01 03:05:15
I've got this piece of code: #!/usr/bin/env python def get_match(): cache=[] def match(v): if cache: return cache cache=[v] return cache return match m = get_match() m(1) if I run it, it says: UnboundLocalError: local variable 'cache' referenced before assignment but if I do this: #!/usr/bin/env python def get(): y = 1 def m(v): return y + v return m a=get() a(1) it runs. Is there something with list? or my code organizing is wrong? The problem is that the variable cache is not in the scope of the function match. This is not a problem if you only want to read it as in your second example, but

deeper understanding of closure in Javascript

本秂侑毒 提交于 2019-12-01 02:57:46
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 the closure scope. I guess that makes sense, but what implications does this have other than memory

exec doesn't pick up variables from closure

谁说胖子不能爱 提交于 2019-12-01 02:55:41
问题 I'm a little curious why the following code raises a NameError . >>> s = """ ... foo = [1,2,3] ... def bar(): ... return foo[1] ... """ >>> namespace = {} >>> exec(s, {'__builtins__': None}, namespace) >>> print namespace {'foo': [1, 2, 3], 'bar': <function bar at 0x7f79871bd0c8>} >>> namespace['bar']() At the normal interpreter level, we can find foo in bar.func_globals or bar.func_closure if in a function. I guess I'm wondering why namespace['bar'] doesn't put foo in func_closure ... 回答1:

Assign an anonymous method to an interface variable or parameter?

梦想的初衷 提交于 2019-12-01 02:49:31
Anonymous methods are essentially interface s with an Invoke method: type TProc = reference to procedure; IProc = interface procedure Invoke; end; Now, is there a possibility to assign them to an actual interface variable or pass them as interface parameter? procedure TakeInterface(const Value: IInterface); begin end; var P: TProc; I: IInterface; begin I := P; // E2010 TakeInterface(P); // E2010 end; [DCC32 Error] E2010 Incompatible types: 'IInterface' and 'procedure, untyped pointer or untyped parameter' Question: What would be the use case for this? There are a lot of objects out there, that