closures

Supporting lexical-scope ScriptBlock parameters (e.g. Where-Object)

风流意气都作罢 提交于 2019-12-23 09:22:03
问题 Consider the following arbitrary function and test cases: Function Foo-MyBar { Param( [Parameter(Mandatory=$false)] [ScriptBlock] $Filter ) if (!$Filter) { $Filter = { $true } } #$Filter = $Filter.GetNewClosure() Get-ChildItem "$env:SYSTEMROOT" | Where-Object $Filter } ################################## $private:pattern = 'T*' Get-Help Foo-MyBar -Detailed Write-Host "`n`nUnfiltered..." Foo-MyBar Write-Host "`n`nTest 1:. Piped through Where-Object..." Foo-MyBar | Where-Object { $_.Name -ilike

How can “NameError: free variable 'var' referenced before assignment in enclosing scope” occur in real code?

大城市里の小女人 提交于 2019-12-23 09:06:07
问题 While I was hanging out in the Python chatroom, someone dropped in and reported the following exception: NameError: free variable 'var' referenced before assignment in enclosing scope I'd never seen that error message before, and the user provided only a small code fragment that couldn't have caused the error by itself, so off I went googling for information, and ... there doesn't seem to be much. While I was searching, the user reported their problem solved as a "whitespace issue", and then

Are PHP closures broken or am I missing something?

自古美人都是妖i 提交于 2019-12-23 08:51:25
问题 I've been reading on the new features of PHP 5.3, and one of the major features are closures . Unless I'm very badly mistaken, the PHP developers are either: a) confusing closures with just anonymous functions b) the closures are broken in PHP 5.3.1 in which I'm testing From what wikipedia says closures are the mechanism of anonymous functions plus the binding of the function's parent's scope variables to the function's scope. The last part seems broken in PHP. I've checked PHP bugs, and

Matlab function handle workspace shenanigans

佐手、 提交于 2019-12-23 07:39:29
问题 In short : is there an elegant way to restrict the scope of anonymous functions, or is Matlab broken in this example? I have a function that creates a function handle to be used in a pipe network solver. It takes as input a Network state which includes information about the pipes and their connections (or edges and vertices if you must), constructs a large string which will return a large matrix when in function form and "evals" that string to create the handle. function [Jv,...] =

ASP.NET Core MvcJsonOptions dependency injection without modified closure?

两盒软妹~` 提交于 2019-12-23 05:49:12
问题 (This question is similar to ASP.NET Core MvcOptions dependency injection without modified closure? However, I struggle to apply the solution of that question to this one.) In my ASP.NET Core 1.1.3 project, I currently inject an ITraceWriter dependency into MvcJsonOptions in the ConfigureServices method in the following way: public override IServiceProvider ConfigureServices(IServiceCollection services) { services.AddProjectSpecificStuff(); ITraceWriter traceWriter = null; services.AddMvc()

Is nested XMLHttpRequests with multiple closures a good idea?

徘徊边缘 提交于 2019-12-23 03:52:17
问题 I have a Greasemonkey script which operates on a search results page at a video site. The function of the script is to take a javascript link that opens a new window with a flash player, jump through some redirection hoops, and insert a regular link to the desired FLV file. I have changed the script to do silly but structurally equivalent things to en.wikipedia.org. My question is whether the 3 nested closures and the nested xmlhttprequests is the best way to go about this. // ==UserScript==

Swift iOS: Closure will not run in background when Remote Notification received

南楼画角 提交于 2019-12-23 03:01:19
问题 I am writing an iOS app that requires the device's GPS loction to be updated when a push notification is received. I use a closure to get the current GPS location. This code runs perfectly when the app is in the foreground (Both "New remote notification" and "Got location" is printed to console), but when the app is in the background, only "New remote notification" (and no location error) is printed to console. As a result I don't have the GPS location of the device at this point, which is

implementing python generators with closures

£可爱£侵袭症+ 提交于 2019-12-23 02:57:08
问题 how can i get rid of the globals in fib_gen2? I don't want to use native generators or classes per this gist, this is an academic exercise, though I am interested improvements in any of the implementations. def ftake(fnext, last): return [fnext() for _ in xrange(last)] def fib_gen2(): global a; a = 1 global b; b = 1 def next(): global a; global b; r = a a, b = b, a + b return r return next assert [1,1,2,3,5] == ftake(fib_gen2(), 5) 回答1: In Python 3.x, you can use the nonlocal statement: def

golang closure(anonymous function)catch wrong parameter`s value

匆匆过客 提交于 2019-12-23 02:47:08
问题 see test code: package main import "fmt" func main() { i := 10 closure1 := func() { fmt.Printf("closure, i: %d\n", i) i = 15 } closure1() fmt.Printf("in main, i: %d\n", i) closure2 := func(x int) { fmt.Printf("function call, pass parameter, i: %d\n", x) } i = 20 closure1() closure2(i) } I think the output of the closure2 should be 20, but the real result is 15, i do not know why???? anybody can help me , please see my comment in my code, thanks in advance. 回答1: The problem is that your first

simple closure vs closure with nested function return

China☆狼群 提交于 2019-12-23 02:39:27
问题 var names = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight"]; var digit_name = function(n){ return names[n]; } //Execute digit_name(0) VERSUS var digit_name = (function() { var names = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight"]; return function(n) { return names[n]; } })(); then execute it like this: digit_name(2) I know these are both closures, but I also think that there are some fundamental differences between the way the two are setup.