closures

Closures and ES2015

為{幸葍}努か 提交于 2019-12-01 07:05:51
问题 I apologize in advance for yet another closure question, but I'd like to clarify my understanding of the way closures are implemented in JavaScript. Consider the following code: 01 'use strict'; 02 function foo() {} 03 foo(); I have established in a question earlier in the year that a closure is conceptually (if not actually due to engine optimizations) created here. And until foo is invoked on line 3 a corresponding execution context is not created. So as far as I can tell from the spec,

How to access variable dynamically inside an anonymous function/closure?

随声附和 提交于 2019-12-01 06:58:57
To keep the global namespace clean, my JavaScript code is wrapped like this: (function() { /* my code */ })(); Now I have some variables declared in this scope which I want to access using a variable variable name (e.g. the name is 'something' + someVar ). In the global scope I'd simply use window['varname'] , but obviously that doesn't work. Is there a good way to do what I want? If not I could simply put those variables inside an object to use the array notation... Note: eval('varname') is not an acceptable solution. So please don't suggest that. This is a good question because this doesn't

How to serialize object that has closures inside properties?

人走茶凉 提交于 2019-12-01 06:53:32
问题 if I do serialize($obj) , I get: Serialization of 'Closure' is not allowed Is there any way these closures can be ignored when serializing? I don't need them when I unserialize the string anyway (value of those properties can be null or whatever). My class looks smth like this: Class Node{ protected $attrs = array(); } $attrs is an associative array that can contain some elements that are closures, like $attrs['validator'] = function(){...} 回答1: I've written a function that allows any

Task not serializable when using object in REPL

China☆狼群 提交于 2019-12-01 06:32:08
问题 So, another SO question prompted me to try the following: object Foo{ def f = 1 } sc.parallelize(List(1)).map(x=>{ val myF = Foo.f _ x + myF() } Which works, but the following does not: object Foo{ def f = 1 def run(rdd: org.apache.spark.rdd.RDD[Int]) = rdd.map(x=>{ val myF = Foo.f _ x + myF() } } Foo.run(sc.parallelize(List(1))) I will take a deeper look at the serialization stack tomorrow when I can remove the mess of the REPL output. But, this should be the same thing. Why does one way

Avoid “Use of unassigned local variable” error

…衆ロ難τιáo~ 提交于 2019-12-01 06:29:30
I have a two methods that are equivalent to this (pardon the contrived example): public void WithResource(Action<Resource> action) { using (var resource = GetResource()) { action(resource); } } public void Test() { int id; SomeObject someObject; WithResource((resource) => { id = 1; someObject = SomeClass.SomeStaticMethod(resource); }); Assert.IsNotNull(someObject); Assert.AreEqual(id, someObject.Id); } (There's some more logic in the WithResource call I'm trying to factor out.) I'm getting Use of unassigned local variable compile-time errors because the assertions are... using unassigned

Executing closure on Twig

可紊 提交于 2019-12-01 06:20:34
I'm trying to execute a closure that resides inside an array on a Twig template. Below you could find a simplified snippet of which I'm trying: //Symfony controller ... $funcs = array( "conditional" => function($obj){ return $obj->getFoo() === $obj::TRUE_FOO } ); $this->render('template_name', array('funcs' => $funcs)); {# Twig template #} {# obj var is set #} ... {% if funcs.conditional(obj)%} <p>Got it</p> {% endif %} When Twig renders the template, throws an exception complaining about an Array to string conversion An exception has been thrown during the rendering of a template ("Notice:

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

流过昼夜 提交于 2019-12-01 06:04:26
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 captured a different variable in this case." I understand this well enough because I understand that

Closures and for loops in Ruby

青春壹個敷衍的年華 提交于 2019-12-01 06:02:29
I'm kind of new to Ruby and some of the closure logic has me a confused. Consider this code: array = [] for i in (1..5) array << lambda {i} end array.map{|f| f.call} # => [5, 5, 5, 5, 5] This makes sense to me because i is bound outside the loop, so the same variable is captured by each trip through the loop. It also makes sense to me that using an each block can fix this: array = [] (1..5).each{|i| array << lambda {i}} array.map{|f| f.call} # => [1, 2, 3, 4, 5] ...because i is now being declared separately for each time through. But now I get lost: why can't I also fix it by introducing an

Parallel.ForEach - Access To Modified Closure Applies?

╄→гoц情女王★ 提交于 2019-12-01 05:32:40
问题 I've read a number of other questions about Access to Modified closure so I understand the basic principle. Still, I couldn't tell - does Parallel.ForEach have the same issues? Take the following snippet where I recompute the usage stats for users for the last week as an example: var startTime = DateTime.Now; var endTime = DateTime.Now.AddHours(6); for (var i = 0; i < 7; i++) { // this next line gives me "Access To Modified Closure" Parallel.ForEach(allUsers, user => UpdateUsageStats(user,

Can't borrow mutably within two different closures in the same scope

狂风中的少年 提交于 2019-12-01 05:07:06
问题 My goal is to make a function (specifically, floodfill) that works independent of the underlying data structure. I tried to do this by passing in two closures: one for querying, that borrows some data immutably, and another for mutating, that borrows the same data mutably. Example (tested on the Rust Playground): #![feature(nll)] fn foo<F, G>(n: i32, closure: &F, mut_closure: &mut G) where F: Fn(i32) -> bool, G: FnMut(i32) -> (), { if closure(n) { mut_closure(n); } } fn main() { let mut data