lexical-scope

Emacs lisp: why does this sexp cause an invalid-function error?

走远了吗. 提交于 2019-11-28 03:54:45
问题 The sexp in question is (((lambda (b) (lambda (a) (+ b a))) 3) 5) which, to me, looks like it should evaluate to 8 , and in other lisps (e.g. Racket) it does, but in elisp it instead throws this error: Debugger entered--Lisp error: (invalid-function ((lambda (b) (lambda (a) (+ b a))) 3)) It appears to be telling me that ((lambda (b) (lambda (a) (+ b a))) 3) Is not a valid function. This seems wrong, because when I evaluate that expression I get (lambda (a) (+ b a)) which looks like a valid

How to translate 'this' in D3 JavaScript to TypeScript?

不羁的心 提交于 2019-11-28 01:21:01
I know that 'this' in JavaScript has a different meaning than in in TypeScript, as per this article 'this' in TypeScript . I have the following code in JavaScript used to create a thicker stroke on the selected node, and give all other nodes a smaller stroke. node.on('click', function (d) { d3.selectAll('circle').attr('stroke-width', 1.5); d3.select(this).select('circle').attr('stroke-width', 5); }) In TypeScript I have this.node.on('click', (d:any) => { this.node.selectAll('circle').attr('stroke-width', 1.5); [this is where I need help].select('circle').attr('stroke-width', 5); } Gerardo

ES6 arrow function lexical this in V8

大兔子大兔子 提交于 2019-11-27 23:38:16
I have the following ES6 code using a fat arrow function: var test = { firstname: 'David', fn: function() { return ['one', 'two', 'tree'].map(() => this.firstname) } } console.log(test.fn()) According to how arrow functions are supposed to work I'd expect this to be the test object. ES6Fiddle , Traceur and Firefox produce the expected output which is ["David", "David", "David"] . When enabling those features in Chrome using chrome://flags/#enable-javascript-harmony , however, I get [undefined, undefined, undefined] . If you console.log(this) it shows that it is the window object and you get an

Difference between <- and <<- [duplicate]

好久不见. 提交于 2019-11-27 08:11:51
问题 This question already has an answer here: How do you use “<<-” (scoping assignment) in R? 6 answers CASE 1: rm(list = ls()) foo <- function(x = 6){ set <- function(){ x <- x*x} set() x} foo() # [1] 6 CASE 2: rm(list = ls()) foo <- function(x = 6){ set <- function(){ x <<- x*x} set() x} foo() # [1] 36 I read that <<- operator can be used to assign a value to an object in an environment that is different from the current environment. It says that object initialization using <<- can be done to

ES6 arrow function lexical this in V8

北慕城南 提交于 2019-11-27 04:40:59
问题 I have the following ES6 code using a fat arrow function: var test = { firstname: 'David', fn: function() { return ['one', 'two', 'tree'].map(() => this.firstname) } } console.log(test.fn()) According to how arrow functions are supposed to work I'd expect this to be the test object. ES6Fiddle, Traceur and Firefox produce the expected output which is ["David", "David", "David"] . When enabling those features in Chrome using chrome://flags/#enable-javascript-harmony , however, I get [undefined,

What is the meaning of the dollar sign “$” in R function()?

让人想犯罪 __ 提交于 2019-11-27 03:11:50
问题 Through learning R , I just came across the following code explained here. open.account <- function(total) { list( deposit = function(amount) { if(amount <= 0) stop("Deposits must be positive!\n") total <<- total + amount cat(amount, "deposited. Your balance is", total, "\n\n") }, withdraw = function(amount) { if(amount > total) stop("You don't have that much money!\n") total <<- total - amount cat(amount, "withdrawn. Your balance is", total, "\n\n") }, balance = function() { cat("Your

Where can we use Variable Scoping and Shadowing in Go?

断了今生、忘了曾经 提交于 2019-11-26 21:45:05
问题 Some related posts I've found: go variable scope and shadowing Golang: variable scope inside if statements Limit the scope of variables storing error Also there are many use cases to Variable Scoping and Shadowing. Any code samples or answers will be appreciated. 回答1: Variable scoping and shadowing: Go is lexically scoped using blocks: The scope of a predeclared identifier is the universe block. The scope of an identifier denoting a constant, type, variable, or function (but not method)

How to translate 'this' in D3 JavaScript to TypeScript?

旧街凉风 提交于 2019-11-26 17:19:40
问题 I know that 'this' in JavaScript has a different meaning than in in TypeScript, as per this article 'this' in TypeScript. I have the following code in JavaScript used to create a thicker stroke on the selected node, and give all other nodes a smaller stroke. node.on('click', function (d) { d3.selectAll('circle').attr('stroke-width', 1.5); d3.select(this).select('circle').attr('stroke-width', 5); }) In TypeScript I have this.node.on('click', (d:any) => { this.node.selectAll('circle').attr(

Referencing “this” inside setInterval/setTimeout within object prototype methods

旧巷老猫 提交于 2019-11-26 10:30:56
Normally I'd assign an alternative "self" reference when referring to "this" within setInterval. Is it possible to accomplish something similar within the context of a prototype method? The following code errors. function Foo() {} Foo.prototype = { bar: function () { this.baz(); }, baz: function () { this.draw(); requestAnimFrame(this.baz); } }; Unlike in a language like Python, a Javascript method forgets it is a method after you extract it and pass it somewhere else. You can either Wrap the method call inside an anonymous function This way, accessing the baz property and calling it happen at

Referencing “this” inside setInterval/setTimeout within object prototype methods

╄→尐↘猪︶ㄣ 提交于 2019-11-26 01:16:32
问题 Normally I\'d assign an alternative \"self\" reference when referring to \"this\" within setInterval. Is it possible to accomplish something similar within the context of a prototype method? The following code errors. function Foo() {} Foo.prototype = { bar: function () { this.baz(); }, baz: function () { this.draw(); requestAnimFrame(this.baz); } }; 回答1: Unlike in a language like Python, a Javascript method forgets it is a method after you extract it and pass it somewhere else. You can