closures

Problem with JavaScript closure

半腔热情 提交于 2019-12-23 15:59:43
问题 Hey guys, I am stuck at the following code. At first I'll describe the use-case: The function "addPreset" gets called with an instance of ColorGradient. When calling this.listController.addItem(...) a callback function named onSelect ist supplied, which gets called everytime the onSelect-event on the listController-item is triggered. What I wanted to do is wrapping the call to GLab.ColorSlider.applyColorGradient(...) into a new closure, so that the assigned value of addPreset's "cg" argument"

Why is the following Scala function called a closure?

本小妞迷上赌 提交于 2019-12-23 15:46:27
问题 For the following question: http://pastie.org/4825115, here is my code: http://pastie.org/private/n22zohyshn2ymqrbrb3g def randList(len: Int, n: Int): List[Int] = len match { case 0 => List() case len => scala.util.Random.nextInt(n) :: randList(len-1, n) } but I don't know why randList is called a closure. 回答1: According to my understanding randList is definitely not a closure (Wikipedia seems to agree) , since - in the snippet of code you provided - it only depends on local variables

Error: reached the recursion limit while instantiating `func::<[closure]>`

淺唱寂寞╮ 提交于 2019-12-23 15:34:41
问题 I am trying to test if a binary search tree is valid: use std::{cell::RefCell, rc::Rc}; pub struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool { preorder_traverse(root.as_ref(), |_| true) } fn preorder_traverse<F: Fn(i32) -> bool>(root: Option<&Rc<RefCell<TreeNode>>>, predict: F) -> bool { if let Some(node) = root { let root_val = root.as_ref().unwrap().borrow().val; if

Error: reached the recursion limit while instantiating `func::<[closure]>`

亡梦爱人 提交于 2019-12-23 15:33:46
问题 I am trying to test if a binary search tree is valid: use std::{cell::RefCell, rc::Rc}; pub struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool { preorder_traverse(root.as_ref(), |_| true) } fn preorder_traverse<F: Fn(i32) -> bool>(root: Option<&Rc<RefCell<TreeNode>>>, predict: F) -> bool { if let Some(node) = root { let root_val = root.as_ref().unwrap().borrow().val; if

How do you expose a global javascript function without ESLint no-unused-var error?

送分小仙女□ 提交于 2019-12-23 12:21:20
问题 The following code is valid in ESLint with Google's style guide with one exception; the closure function Counter gets a no-unused-vars error when the script is checked using ESLint. /** * Create a counter that is incremented and returned when called * @return {object} - incrementor function */ function Counter() { var _i = 0; /** * increment counter * @return {int} - The incremented integer */ function _incrementor() { _i++; return _i; } _incrementor.incr = function() { this.call(); return

Javascript: why the access to closure variable might be slow

爱⌒轻易说出口 提交于 2019-12-23 12:16:58
问题 Recently I've read this performance guide Let's make the web faster and was puzzled by "Avoiding pitfalls with closures" recommendations (as if these advices were given for CommonLisp users where variable scoping is dynamic): var a = 'a'; function createFunctionWithClosure() { var b = 'b'; return function () { var c = 'c'; a; b; c; }; } var f = createFunctionWithClosure(); f(); when f is invoked, referencing a is slower than referencing b , which is slower than referencing c . It's quite

Linq Where local counter closure different results in VS watch

烂漫一生 提交于 2019-12-23 12:11:12
问题 I try to delete first 3 elements in array with LinQ Where extension function. Here is an example: var array = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var count = 3; var deletedTest1 = 0; var test1 = array.Where(x => ++deletedTest1 > count).ToList(); Console.WriteLine($"{{{String.Join(", ", test1)}}}"); var deletedTest2 = 0; var test2 = array.Where(x => ++deletedTest2 > count).AsEnumerable(); Console.WriteLine($"{{{String.Join(", ", test2)}}}"); var deletedTest3 = 0; var test3 = array.Where(x =>

javascript prototype and “this” access in closure

余生颓废 提交于 2019-12-23 10:17:31
问题 I am a beginner in js, and am puzzled by the following code: Foo = function(arg) { this.arg = arg; }; Foo.prototype = { init: function () { var f = function () { alert("current arg: " + this.arg); // am expecting "bar", got undefined } f(); } }; var yo = Foo("bar"); yo.init(); I was expected to get "current arg: bar", but got "current arg: undefined". I noticed that by copying this.arg into a "normal" variable first, and refering this variable in the closure works: Foo.prototype = { init:

Scala variable binding when used with Actors

自闭症网瘾萝莉.ら 提交于 2019-12-23 10:10:39
问题 I am fairly new to Scala. I am trying to understand how/if scala does dynamic binding when a closure is passed as part of a message to an Actor. I am using Akka 1.2 with Scala 2.9. I have the following code segment (modified from http://gleichmann.wordpress.com/2010/11/15/functional-scala-closures/) var minAge = 18 val isAdult = (age: Int) => age >= minAge actor ! answer(19, isAdult) minAge = 20 actor ! answer(19, isAdult) On the actor side, it simply applies isAdult to the first parameter

accessing mutable variable in an event closure

对着背影说爱祢 提交于 2019-12-23 09:30:17
问题 I am trying to use the mousetrap javascript plugin to handle some key strokes in a similar fashion, so I thought to code them up as follows: var keys = [ 'b', 'i', 'u']; for (var i=0; i < 3; ++i) { var iKey = keys[i]; var iKeyUpper = iKey.toUpperCase(); Mousetrap.bind( [ 'command+' + iKey, 'command+' + iKeyUpper, 'ctrl+' + iKey, 'ctrl+' + iKeyUpper], ( function( e ) { console.log( "you clicked: " + i ); } ) ); } But, obviously, i is mutable. However, I am not sure how to write a closure where