lexical-scope

Dynamic and Lexical variables in Common Lisp

不问归期 提交于 2019-12-17 17:32:08
问题 I am reading the book 'Practical Common Lisp' by Peter Seibel. In Chapter 6, "Variables" sections "Lexical Variables and Closures" and "Dynamic, a.k.a. Special, Variables". http://www.gigamonkeys.com/book/variables.html My problem is that the examples in both sections show how (let ...) can shadow global variables and doesn't really tell the difference between the Dynamic and Lexical vars. I understand how closures work but I don't really get whats so special about let in this example:

emacs lexical scoping and quoted variable name

末鹿安然 提交于 2019-12-13 01:05:35
问题 I was experimenting with interplay between Emacs lexical scoping (new feature of Emacs 24) and add-to-list and found the interplay confusing and I don't know how to make sense of it. Here is a minimal example, except I use set instead of add-to-list . ( set is similar to add-to-list in that it usually takes a quoted variable name) (eval '(progn (setq a "global") (let ((a "apple")) (defun my-print-a () (print a) (set 'a "by set") (print a)) (setq a "mature apple")) (let ((a "banana")) (my

Compiling ES6 arrow functions to Es5 using Babel.js

亡梦爱人 提交于 2019-12-12 11:00:29
问题 While looking into ES6 arrow functions' documentation on Mozilla documentation, I got to know that Arrow functions applies all the rules of strict mode, except one as described in the link var f = () => { 'use strict'; return this}; var g = function () { 'use strict'; return this;} console.log(f()); //prints Window console.log(g()); // prints undefined //we can test this in firefox! But, Babel.js is transpiling the arrow function code to ES5 code that returns undefined rather than Window

What does lexical scope look like in memory model?

心已入冬 提交于 2019-12-11 10:07:39
问题 Say we have a function: function foo() { var x = 10; function bar() { var y = 20; return x + y; } return bar(); } console.log(foo()); What would this look like in a memory model. So far this is what I imagine it looks like on the stack? TOP OF THE STACK -------------- bar() y = 20 return x + 20 -------------- foo() x= 10 bar() -------------- BOTTOM OF THE STACK What does lexical scope look like how does bar know what x is? Is foo() on the heap? or does bar() have a pointer to foo() ? 回答1:

In Perl, do “$a” and “$b” have any special use outside of the sort() function?

天涯浪子 提交于 2019-12-10 10:19:53
问题 I asked a question about the use of "$a" and "$b" in Perl's sort() function the other day: What exactly are "$a" and "$b" in Perl's "sort()" function? I now have a follow up question. Are "$a" and "$b" only used by sort() or is there any other Perl function that takes advantage of these special global variables? Or even if no other function uses them, are there any situations outside of sort() that you would use "$a" or "$b"? Edit: To clarify: In short, the question is can "$a" and "$b" be

In LISP how to inspect free variables in a closure?

这一生的挚爱 提交于 2019-12-08 21:24:18
问题 In lisp I can bind free variables bound in a closure like this... (let ((x 1) (y 2) (z 3)) (defun free-variables () (+ x y z))) (free-variables) results in ... 6 What I want to know is if it is possible to inspect bound closure variables dynamically? E.g. (inspect-closure free-variables) resulting in something like... ((x 1) (y 2) (z 3)) Thanks SO 回答1: Common Lisp Access to the closure's internal variables is only possible from functions in the same scope (See Jeff's answer). Even those can't

Why does jQuery has a “window=this” at the very begining and say it would speed up references to window?

余生颓废 提交于 2019-12-07 12:05:15
问题 When I open jQuery's source code I find this line. var // Will speed up references to window, and allows munging its name. window = this Why and how this line will speed up? 回答1: javascript functions have lexical scope jQuery wraps its entire implementation in an anonymous function when said function begins execution it is executing in the "global" scope (ie this == window ). "window = this;" simply creates a local identifier in that scope so that references to it do not have to "bubble up"

Does my $_; do anything if $_ is implied

喜你入骨 提交于 2019-12-07 03:09:38
问题 I think the answer is yes but I just want to make sure. so if I have sub something { my $_; my @array = ...; while ( @array ) { say; } } is the my $_; actually effective at lexicalizing the parameter passed to the say? In this particular case I'm using the DZP::UnusedVarsTests and it's complaining that I haven't used my $_; and I suspect it's a bug since I'm using it in a case where it's implied. 回答1: The short answer is Yes. It makes the functions in that scope use the lexically scoped $_ ,

Sharing a thread variable without making it global (Perl)

最后都变了- 提交于 2019-12-06 22:52:28
问题 I'm trying to write a simple script that uses threads and shares a variable, but I don't want to make this variable global to the whole script. Below is a simplified example. use strict; use warnings; use threads; use threads::shared; my $val:shared; # Create threads for my $i (1 .. 5) { threads->create(\&do_something, $i); } # Wait for all threads to complete map { $_->join(); } threads->list(); # $val is global to the script so this line will work! print "VAL IS: $val\n"; sub do_something {

why is IIFE needed to create a new scope?

谁都会走 提交于 2019-12-06 04:05:40
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); } setTimeout(timer, i*1000 ); } work like the IIFE example? It seems to me they both have a function