closures

need help understanding JS code [closed]

一世执手 提交于 2019-12-01 10:03:53
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . Hi I am new to javascript and I cannot understand the following code: var createAdders = function () { var fns = []; for (var i = 1; i < 4; i++) { fns[i]

Javascript object literal, how to solve context?

大兔子大兔子 提交于 2019-12-01 09:31:46
I would like to start organizing my code properly, so I want to use object literals. In the following case, I'm doing a pseudo class. I would like that init() could work as a constructor, but unfortunately, I'm not seeing how to set attributes based on object context. var car = { context : this, wheels : 0, color : '', speed : 0, init : (function(x){ console.log(x); x.wheels = 4; x.color = 'red'; x.speed = 120; })(context) }; console.log(car.color); You can't immediately run a function like that whilst declaring an object literal. What you can do: var car = { init : function(wheels,color,speed

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

↘锁芯ラ 提交于 2019-12-01 09:15:32
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 = 0; let closure = |n| data == n; let mut mut_closure = |n| { data += n; }; foo(0, &closure, &mut mut

Task not serializable when using object in REPL

萝らか妹 提交于 2019-12-01 08:58:41
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 yell and the other does not. 来源: https://stackoverflow.com/questions/29762964/task-not-serializable-when

Swift lazy stored property versus regular stored property when using closure

半城伤御伤魂 提交于 2019-12-01 08:50:54
问题 In Swift, we can set a stored property to use closure: class Test { var prop: String = { return "test" }() } vs or make lazy stored property use closure: class Test { lazy var prop: String = { return "test" }() } In both cases, the code used to obtain the value for the property is only run once. It seems like they are equivalent. When should I use lazy stored property versus computed property when using closure with it? 回答1: import Foundation struct S { var date1: NSDate = { return NSDate() }

Closures and ES2015

淺唱寂寞╮ 提交于 2019-12-01 08:42:59
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, when evaluating this code: Each execution context has a "LexicalEnvironment" component used to resolve

Calling closure on different object?

梦想的初衷 提交于 2019-12-01 08:25:16
问题 Suppose I have this class: class MyClass { int myInt MyClass(myInt) { this.myInt = myInt } def myMethod() { print this.myInt } } And somewhere I have: def myClass1 = new MyClass(1) def myMethodClosure = myClass1.&myMethod def myClass2 = new MyClass(2) Now if I call myMethodClosure() it will call myMethod() on myClass1 instance which will print 1. What I want is to call the same myMethodClosure but on a different instance, in this case on myClass2 so it can print 2. Is this possible? I have

Initializing property via closure

…衆ロ難τιáo~ 提交于 2019-12-01 08:12:28
问题 I've observed that people sometimes using closures to initialize properties. e.g. instead of lazy var test1: String = String("a string") they use lazy var test2: String = { String("a string") }() What is the benefit/convenience in using closure to initialize property? 回答1: These two do the same work. Closure initialization comes handy when you need extra code to configure property object. E.g.: lazy var point: CGPoint = { let x = ... let y = ... return CGPoint(x: x, y: y) }() 回答2: In general,

Capturing a struct reference in a closure doesn't allow mutations to occur

夙愿已清 提交于 2019-12-01 08:10:42
I am trying to see if I can use structs for my model and was trying this. When I call vm.testClosure() , it does not change the value of x and I am not sure why. struct Model { var x = 10.0 } var m = Model() class ViewModel { let testClosure:() -> () init(inout model: Model) { testClosure = { () -> () in model.x = 30.5 } } } var vm = ViewModel(model:&m) m.x vm.testClosure() m.x An inout argument isn't a reference to a value type – it's simply a shadow copy of that value type, that is written back to the caller's value when the function returns. What's happening in your code is that your inout

Parallel.ForEach - Access To Modified Closure Applies?

百般思念 提交于 2019-12-01 07:34:43
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, startTime, endTime)); // move back a day and continue the process startTime = startTime.AddDays(-1);