lexical-scope

R - Checking if a string is a valid mathematical expression using non-standard evaluation

爱⌒轻易说出口 提交于 2021-01-28 12:16:02
问题 I would like to check if the strings below are valid mathematical expressions: s1 = 'sin(x)' s2 = 'sin(x*m)' s3 = 'sin' s4 = 'sin(xm)' By 'valid', I mean the expression is a combination of operators (must be used in conjunction with variables or constants) variables x and/or m constants. By this definition s1 and s2 are valid while s3 and s4 are not. To identify if a string is valid, I wrote a function checkFxn that first attempts to convert the string into a call or one of its parts. If

Lexical scoping in a for loop enclosing a promise?

南笙酒味 提交于 2019-12-31 05:27:10
问题 I have an ids object, which maps id strings to product objects. for id of ids product = ids[id] console.log product # Prints out something different each loop. :) Product.create(product).then -> console.log product # Only prints out the last id each loop. :( I'm using a library for database interactions, which exposes promises (indicated by the then function above). I'm trying to print out the product variable inside the then function, but I only seem to be getting the last id in ids , so it

Lexical scoping vs dynamic scoping

[亡魂溺海] 提交于 2019-12-29 06:25:00
问题 So I have this problem where I have to figure out the output using two different scoping rules. I know the output using lexical scoping is a=3 and b=1 , but I am having hard time figure out the output using dynamic scoping. Note:the code example that follows uses C syntax, but let's just treat it as pseudo-code. int a,b; int p() { int a, p; a = 0; b = 1; p = 2; return p; } void print() { printf("%d\n%d\n",a,b); } void q () { int b; a = 3; b = 4; print(); } main() { a = p(); q(); } Here is

Closures in Python

放肆的年华 提交于 2019-12-29 06:20:38
问题 I've been trying to learn Python, and while I'm enthusiastic about using closures in Python, I've been having trouble getting some code to work properly: def memoize(fn): def get(key): return (False,) def vset(key, value): global get oldget = get def newget(ky): if key==ky: return (True, value) return oldget(ky) get = newget def mfun(*args): cache = get(args) if (cache[0]): return cache[1] val = apply(fn, args) vset(args, val) return val return mfun def fib(x): if x<2: return x return fib(x-1

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

我是研究僧i 提交于 2019-12-25 08:20:07
问题 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 either

What types of scope exist in Javascript?

穿精又带淫゛_ 提交于 2019-12-25 02:24:29
问题 I understand that there is global scope, and additionally nestable functional scope . But are there any other types of scopes or closures in Javascript? While we're on the topic, what's the difference between a scope, and a closure? 回答1: A closure is a stack of visible scopes. Let's say you have the following code: var v1; function a() { var v2; function b() { var v3; function c() { var v4; } return c; } return b(); } var f = a(); c is a function that has 4 visible scopes: its own scope

why is IIFE needed to create a new scope?

大憨熊 提交于 2019-12-22 11:16:23
问题 From You Don't Know JS: for (var i=1; i<=5; i++) { setTimeout( function timer(){ console.log( i ); }, i*1000 ); } gives 6 6 6 6 6 but using an IIFE like so for (var i=1; i<=5; i++) { (function(){ var j = i; setTimeout( function timer(){ console.log( j ); }, j*1000 ); })(); } gives 1 2 3 4 5 My question: why doesn't for (var i=1; i<=5; i++) { setTimeout( function timer(){ var j = i; console.log( j ); }, i*1000 ); } or for (var i=1; i<=5; i++) { function timer() { var j = i; console.log(j); }

Why are variables declared with “our” visible across files?

风格不统一 提交于 2019-12-20 12:31:27
问题 From the "our" perldoc: our has the same scoping rules as my, but does not necessarily create a variable. This means that variables declared with our should not be visible across files, because file is the largest lexical scope. But this is not true. Why? 回答1: You can consider our to create a lexically-scoped alias to a package global variable. Package globals are accessible from everywhere; that's what makes them global. But the name created by our is only visible within the lexical scope of

Understanding the environment model of evaluation

笑着哭i 提交于 2019-12-18 09:16:41
问题 Exercise 3.20 in SICP: Draw environment diagrams to illustrate the evaluation of the sequence of expressions (define x (cons 1 2)) (define z (cons x x)) (set-car! (cdr z) 17) (car x) 17 using the procedural implementation of pairs given above. My eyes are destroyed so I cannot draw. I will instead try to imagine as best as I can how the environment model evolves. First, here is the procedural pairs implementation. (define (cons x y) (define (set-x! v) (set! x v)) (define (set-y! v) (set! y v)

Ruby - Lexical scope vs Inheritance

那年仲夏 提交于 2019-12-17 17:35:06
问题 This is a continuation this original SO question: Using "::" instead of "module ..." for Ruby namespacing In the original SO question, here is the scenario presented which I'm still having trouble understanding: FOO = 123 module Foo FOO = 555 end module Foo class Bar def baz puts FOO end end end class Foo::Bar def glorf puts FOO end end puts Foo::Bar.new.baz # -> 555 puts Foo::Bar.new.glorf # -> 123 Can someone provide some explanation behind why the first call is returning 555 and why the