closures

Why don't all of these variables get treated the same way?

淺唱寂寞╮ 提交于 2019-12-09 09:52:03
问题 I was checking that the position of variable declarations in VB.NET don't matter, except for scope, (for this question) and I thought I better check what happens when they're "lifted" into a closure. I haven't read the spec, but I can't explain these results: Dim outer As Integer For i = 1 To 2 Dim inner As Integer Try Dim inner2 As Integer Do Dim inner3 As Integer Call Sub() Dim inner4 As Integer Console.WriteLine(outer & ", " & inner & ", " & inner2 & ", " & inner3 & ", " & inner4) outer =

perl closures and $_

我是研究僧i 提交于 2019-12-09 08:05:44
问题 One of the first things I try to learn in an unfamiliar programming language is how it handles closures. Their semantics are often intertwined with how the language handles scopes and various other tricky bits so understanding them reveals several other aspects of the language. Plus, closures are a really powerful construct and often times cut down on the amount of boilerplate I have to type. So I was messing around with perl closures and I stumbled upon a little gotcha: my @closures; foreach

Groovy Closure with optional arguments

孤街浪徒 提交于 2019-12-09 07:48:00
问题 I want to define a closure which takes one argument (which i refer to with it ) sometimes i want to pass another additional argument to the closure. how can i do this? 回答1: You could set the second argument to a default value (such as null): def cl = { a, b=null -> if( b != null ) { print "Passed $b then " } println "Called with $a" } cl( 'Tim' ) // prints 'Called with Tim' cl( 'Tim', 'Yates' ) // prints 'Passed Yates then Called with Tim Another option would be to make b a vararg List like

What exactly is a PowerShell ScriptBlock

醉酒当歌 提交于 2019-12-09 05:37:47
问题 A PowerShell ScriptBlock is not a lexical closure as it does not close over the variables referenced in its declaring environment. Instead it seems to leverage dynamic scope and free variables which are bound at run time in a lambda expression. function Get-Block { $b = "PowerShell" $value = {"Hello $b"} return $value } $block = Get-Block & $block # Hello # PowerShell is not written as it is not defined in the scope # in which the block was executed. function foo { $value = 5 function bar {

Private field captured in anonymous delegate

柔情痞子 提交于 2019-12-09 05:19:24
问题 class A { public event EventHandler AEvent; } class B { private A _foo; private int _bar; public void AttachToAEvent() { _foo.AEvent += delegate() { ... UseBar(_bar); ... } } } Since delegate captures variable this._bar , does it implicitly hold to the instance of B ? Will instance of B be referenced through the event handler and captured variable by an instance of A ? Would it be different if _bar was a local variable of the AttachToAEvent method? Since in my case an instance of A lives far

multithreading re-entrancy issue

和自甴很熟 提交于 2019-12-09 03:51:10
问题 I'm trying to spawn different threads for some processing. I use the for loop index for some logic inside each thread. How can I get the different threads to print 1,2,3,4, 5 in the code below? Each time I run this, I get different numbers as output - 3,3,3,4,6,6 & 2,2,3,5,5,6 etc. I tried using the lock object, but it stil wasn't doing it correctly. Can anyone help me achive this. I just want to make sure each thread/task gets the right index. Note that each task has been forced to run on a

Why do we need blocks, function literals, closures in programming languages?

半腔热情 提交于 2019-12-08 22:14:28
I program in Objective-C, I know a bit of Scala, Python and Javascript. While I'm comfortable with blocks in Obj-C, I would like to know what specific problem do they solve that I couldn't with the earlier versions of the language. Also - are blocks, closures, function literals, named functions, anonymous functions - one and the same thing? If you can answer with some code examples that would be great. First of all, in order to answer the question in the title: Why do we need blocks, function literals, closures in programming languages? Answer #1: we don't. Brainfuck is Turing-complete without

In LISP how to inspect free variables in a closure?

这一生的挚爱 提交于 2019-12-08 21:24:18
问题 In lisp I can bind free variables bound in a closure like this... (let ((x 1) (y 2) (z 3)) (defun free-variables () (+ x y z))) (free-variables) results in ... 6 What I want to know is if it is possible to inspect bound closure variables dynamically? E.g. (inspect-closure free-variables) resulting in something like... ((x 1) (y 2) (z 3)) Thanks SO 回答1: Common Lisp Access to the closure's internal variables is only possible from functions in the same scope (See Jeff's answer). Even those can't

JavaScript Closure: Returning a Function

爱⌒轻易说出口 提交于 2019-12-08 18:23:26
I am working my way through a JavaScript lecture series by Douglas Crockford. I am confused by a code example he is showing to illustrate 'closure': var digit_name = (function () { var names = ['zero', 'one', 'two', 'three']; return function (n) { return names[n]; }; }()); alert(digit_name(3)); How/why can digit_name take an argument when no parameter is specified in the definition (the outermost function)? How does the argument (in this case 3 ) know to correspond to n within the inner function definition during invocation? The digit_name stores the inner function return ed by the outer

PHP: How to pass instance variable to closure?

时光总嘲笑我的痴心妄想 提交于 2019-12-08 18:03:24
问题 If you want to use a closure from within a class, how do you pass in an instance variable from that class? class Example { private $myVar; public function test() { $this->myVar = 5; $func = function() use ($this->myVar) { echo 'myVar is: ' . $this->myVar; }; // The next line is for example purposes only if you want to run this code. // $func is actually passed as a callback to a library, so I don't have // control over the actual call. $func(); } } $e = new Example(); $e->test(); PHP doesn't