closures

javascript closures and object reference

淺唱寂寞╮ 提交于 2019-12-02 03:24:30
i am a bit obscure situation. mainly because i thought i already grasp closures. so basically what i want is to reset to default values a collection. so let say i have collection which has constructor with array of objects parameter. var c = new collection([{x},{y},{z}]); then collection periodically get updated.since i am not keeping somewhere initial values of array, after sometime i would like to reset to initial values. now i am not asking how to implement this, there could be multiple ways my question about closures itself. please read further so the way i might thought to trap this

Storing a Closure Function in a Class Property in PHP

邮差的信 提交于 2019-12-02 03:20:31
问题 ok I do have the code below <?php class foo{ public $bar = NULL; public function boo(){ $this->bar(); } } $mee = new foo(); //save a closure function on the property $mee->bar = function(){ echo 'hahaha'; }; //invoke the closure function by using a class method $mee->boo(); ?> and you can see it running here http://codepad.org/s1jhi7cv now what i want here is to store the closure function on the class method. well closures are possible as i read the documentation about it here http://php.net

Iterate and print content of groovy closures

会有一股神秘感。 提交于 2019-12-02 03:13:32
In a loop I create 4 closures and add them to a list: closureList = [] for (int i=0; i<4; i++) { def cl = { def A=i; } closureList.add(cl) } closureList.each() {print it.call()println "";}; This results in the following output: 4 4 4 4 But I would have expected 0,1,2,3 instead. Why does the 4 closures have the same value for A? Yeah, this catches people out , the free variable i is getting bound to the last value in the for loop, not the value at the time the closure was created. You can either, change the loop into a closure based call: closureList = (0..<4).collect { i -> { -> def a = i } }

How are closures implemented in scala?

雨燕双飞 提交于 2019-12-02 03:12:38
问题 How are the variables outside of the scope of the function pulled into the function when it's created? I tried decompiling, but I had trouble understanding it. It looked like it uses putfield. Does putfield make a pointer to an object reference? 回答1: The answer is "it depends". There will probably be some major changes to this with the scala 2.11 release. Hopefully 2.11 will be able to inline simple closures. But anyway, let's try to give an answer for the current scala version (javap below

Moving from `prototype` and `new` to a closure-and-exposure pattern

纵然是瞬间 提交于 2019-12-02 02:55:19
问题 I have been re-factoring someone else's JavaScript code. BEFORE: function SomeObj(flag) { var _private = true; this.flag = (flag) ? true : false; this.version="1.1 (prototype)"; if (!this._someProperty) this._init(); // leading underscore hints at what should be a 'private' to me this.reset(); // assumes reset has been added... } SomeObj.prototype.reset = function() { /* perform some actions */ } /* UPDATE */ SomeObj.prototype.getPrivate = function() { return _private; // will return

How to use struct self in member method closure

我是研究僧i 提交于 2019-12-02 02:48:48
问题 How can I call a method in closure? get_access_token method can set new access token based on self.get_base_url() : fn fetch_access_token(_base_url: &String) -> String { String::new() } fn get_env_url() -> String { String::new() } pub struct App { pub base_url: Option<String>, pub access_token: Option<String>, } impl App { pub fn new() -> App { App { base_url: None, access_token: None, } } pub fn get_base_url(&mut self) -> &String { self.base_url.get_or_insert_with(|| get_env_url()) } pub fn

javascript closures and object reference

我们两清 提交于 2019-12-02 02:48:01
问题 i am a bit obscure situation. mainly because i thought i already grasp closures. so basically what i want is to reset to default values a collection. so let say i have collection which has constructor with array of objects parameter. var c = new collection([{x},{y},{z}]); then collection periodically get updated.since i am not keeping somewhere initial values of array, after sometime i would like to reset to initial values. now i am not asking how to implement this, there could be multiple

Why Scala can serialize Function but not PartialFunction?

社会主义新天地 提交于 2019-12-02 02:27:56
问题 I have 2 functions (1 of them is partial) defined similarly under an object: val partialFn: scala.PartialFunction[String, Int] = new AbstractPartialFunction[String, Int] { override def isDefinedAt(v: String): Boolean = { counter += 1 if (v == "abc") true else false } override def applyOrElse[A1 <: String, B1 >: Int](v: A1, default: A1 => B1): B1 = { counter += 1 if (v == "abc") { v.length } else { default(v) } } } val optionFn: (String) => Option[Int] = { (v: String) => { counter += 1 if (v =

PowerShell : GetNewClosure() and Cmdlets with validation

落花浮王杯 提交于 2019-12-02 01:22:44
问题 I'm trying to understand how .GetNewClosure() works within the context of a script cmdlet in PowerShell 2. In essence I have a function that returns an object like so: function Get-AnObject { param( [CmdletBinding()] [Parameter(....)] [String[]]$Id .. [ValidateSet('Option1','Option2')] [String[]]$Options ) ... $T = New-Object PSCustomObject -Property @{ ..... } $T | Add-Member -MemberType ScriptProperty -Name ExpensiveScriptProperty -Value { $this | Get-ExpensiveStuff }.GetNewClosure() .. }

How are closures implemented in scala?

二次信任 提交于 2019-12-02 01:14:03
How are the variables outside of the scope of the function pulled into the function when it's created? I tried decompiling, but I had trouble understanding it. It looked like it uses putfield. Does putfield make a pointer to an object reference? The answer is "it depends". There will probably be some major changes to this with the scala 2.11 release. Hopefully 2.11 will be able to inline simple closures. But anyway, let's try to give an answer for the current scala version (javap below is from scala 2.10.2). Below is a very simple closure that uses a val and a var, and the javap output of the