Questions tagged [scoping]

Scoping associates a name with an entity. For an object to be *in scope* means that it is possible to write a snippet of code referencing value and location of that object by its name. Lexical scoping refers to a proportion of text whereas dynamic scoping corresponds to the proportion of run time.

584 questions
17
votes
5 answers

Variables as default arguments of a function, using dplyr

Goal My goal is to define some functions for use within dplyr verbs, that use pre-defined variables. This is because I have some of these functions that take a bunch of arguments, of which many always are the same variable names. My understanding:…
Axeman
  • 32,068
  • 8
  • 81
  • 94
16
votes
4 answers

Scope functions apply/with/run/also/let: Where do their names come from?

There are quite a few blog posts (like this) on usages of the standard library functions apply/with/run/also/let available that make it a bit easier to distingish when to actually use which of those pretty functions. For a few weeks now, the…
s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
16
votes
3 answers

Can I "extend" a closure-defined "class" in Javascript?

I have a Javascript "class" defined like so: var Welcomer = function(name) { var pName = name; var pMessage = function() { return "Hi, " + pName + "!"; }; return { sayHi: function() { alert(pMessage()); } }; }; new…
James A. Rosen
  • 64,193
  • 61
  • 179
  • 261
16
votes
1 answer

Static Variables Initialization Quiz

#include class C { public: static int i; static int j; }; int i = 10; int C::i = 20; int C::j = i + 1; int main () { printf("%d", C::j); return 0; } What is the value of: C::j I was reading a c++ quiz…
marsh
  • 2,592
  • 5
  • 29
  • 53
16
votes
3 answers

Set a functions environment to that of the calling environment (parent.frame) from within function

I am still struggling with R scoping and environments. I would like to be able to construct simple helper functions that get called from my 'main' functions that can directly reference all the variables within those main functions - but I don't…
Charlie
  • 481
  • 4
  • 16
16
votes
2 answers

Passing variables to $.ajax().done()

I'm lost. How might I pass a loop variable to an AJAX .done() call? for (var i in obj) { $.ajax(/script/).done(function(data){ console.log(data); }); } Obviously, if I were to do console.log(i+' '+data) i would return the very last key in the…
Phil Tune
  • 3,154
  • 3
  • 24
  • 46
15
votes
5 answers

Why does using the same count variable name in nested FOR loops work?

Why does the following not give an error? for (int i=0; i<10; ++i) // outer loop { for (int i=0; i<10;++i) // inner loop { //...do something } //...do something else } The way I understand it, variables in braces ({...}) are in…
M Perry
  • 31,500
  • 2
  • 16
  • 13
14
votes
1 answer

Common Lisp scoping (dynamic vs lexical)

EDIT: I changed the example code after the first answer because I came up with a simple version that begs the same questions. I am currently learning Common Lisp's scoping properties. After I thought I had a solid understanding I decided to code up…
Anthony Naddeo
  • 2,497
  • 25
  • 28
14
votes
4 answers

Scoping and functions in R 2.11.1 : What's going wrong?

This question comes from a range of other questions that all deal with essentially the same problem. For some strange reason, using a function within another function sometimes fails in the sense that variables defined within the local environment…
Joris Meys
  • 106,551
  • 31
  • 221
  • 263
14
votes
8 answers

What methods are there to modularize C code?

What methods, practices and conventions do you know of to modularize C code as a project grows in size?
Ari Ronen
  • 2,222
  • 2
  • 22
  • 24
13
votes
1 answer

Any way to access function installed by makeActiveBinding?

The title basically says it all. If I do this ... makeActiveBinding("x", function() runif(2), .GlobalEnv) x # [1] 0.7332872 0.4707796 x # [1] 0.5500310 0.5013099 ... is there then any way for me to examine x to learn what function it is linked to…
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
12
votes
2 answers

When should i be opening and closing MongoDB connections?

i am very new to MongoDB and NoSQL in general and i've just started building a site with MongoDB / Norm / ASP.NET MVC 3. I am wondering how i should be scoping the connections to my Mongo database. Right now i have a Basecontroller that instanciates…
Kimpo
  • 5,835
  • 4
  • 26
  • 30
12
votes
0 answers

Why does this python code exhibit weird scoping rules

I'm running on Python 2.7.8 (Anaconda Distribution) and this code fails. This looks like a bug in the Python implementation, but am I missing anything? class C: x = {2 : 1} y = {w for w in x if x[w]==1} Running this code gives the following…
12
votes
1 answer

Python scoping in dict comprehension

>>> x = 'foo' >>> {0: locals().get('x')} {0: 'foo'} >>> {0: locals().get('x' + spam) for spam in ['']} {0: None} What is the reason for this discrepancy in behaviour?
wim
  • 338,267
  • 99
  • 616
  • 750
11
votes
3 answers

curve3d can't find local function "fn"

I'm trying to use the curve3d function in the emdbook-package to create a contour plot of a function defined locally inside another function as shown in the following minimal example: library(emdbook) testcurve3d <- function(a) { fn <-…
Jarle Tufto
  • 240
  • 1
  • 13
1
2
3
38 39