scoping

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

本小妞迷上赌 提交于 2019-11-29 06:29:31
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 scope only within these braces. But the inner loop is inside the braces of the outer loop. So as soon as I declare int i=0 for the inner loop, shouldn't I get an error about multiple definitions? You are actually making a new variable with the same name as another variable. Since they are in different scopes this is allowed, and the variable in the inner scope "owns"

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

て烟熏妆下的殇ゞ 提交于 2019-11-28 23:27:53
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 of the first function are not found back in the second function. The classical pattern in pseudo-code : ff <- function(x){ y <- some_value some_function(y) } ff(x) Error in eval(expr, envir, enclos) : object 'y' not found First I thought it had something to do with S4 methods and the scoping in there, but it also happens with other functions. I've

Dynamic Scoping - Deep Binding vs Shallow Binding

北战南征 提交于 2019-11-28 22:41:59
I've been trying to get my head around shallow binding and deep binding, wikipedia doesn't do a good job of explaining it properly. Say I have the following code, what would the output be if the language uses dynamic scoping with a) deep binding b) shallow binding? x: integer := 1 y: integer := 2 procedure add x := x + y procedure second(P:procedure) x:integer := 2 P() procedure first y:integer := 3 second(add) ----main starts here--- first() write_integer(x) Deep binding binds the environment at the time the procedure is passed as an argument Shallow binding binds the environment at the time

Python class scoping rules

只愿长相守 提交于 2019-11-28 17:52:17
EDIT: Looks like this is a very old "bug" or, actually, feature. See, e.g., this mail I am trying to understand the Python scoping rules. More precisely, I thought that I understand them but then I found this code here : x = "xtop" y = "ytop" def func(): x = "xlocal" y = "ylocal" class C: print(x) print(y) y = 1 func() In Python 3.4 the output is: xlocal ytop If I replace the inner class by a function then it reasonably gives UnboundLocalError . Could you explain me why it behaves this strange way with classes and what is the reason for such choice of scoping rules? TL;DR : This behaviour has

Can I use blocks to manage memory consumtion in C++?

时间秒杀一切 提交于 2019-11-28 12:30:45
I'm trying to gain some memory saving in a C++ program and I want to know if I can use blocks as a scope for variables (as in Perl). Let's say I have a huge object that performs some computations and gives a result, does it makes sense to do: InputType input; ResultType result; { // Block of code MyHugeObject mho; result = mho.superHeavyProcessing(); } /* My other code ... */ Can I expect the object to be destroyed when exiting the block? Yes, you can. The destructor will be called as soon as the variable falls out of scope and it should release the heap-allocated memory. Yes absolutely, and

Java scoping rules and inner classes

浪子不回头ぞ 提交于 2019-11-28 09:30:41
All the crazy Java scoping rules are making my head spin and the public static void nonsense isn't helping matters. So far all the programming languages I have used either lexical scoping or some approximation of it without any access modifiers, i.e. inner stuff captures outer stuff and has access to the outer stuff as long as the inner stuff exists. So how do I make sense of the scoping rules for inner classes in Java? Do they get access to variables declared in the outer class or is there some weird edge cases I have to worry about because of all the public static private stuff floating

attach() inside function

放肆的年华 提交于 2019-11-28 09:01:45
I'd like to give a params argument to a function and then attach it so that I can use a instead of params$a everytime I refer to the list element a. run.simulation<-function(model,params){ attach(params) # # Use elements of params as parameters in a simulation detach(params) } Is there a problem with this? If I have defined a global variable named c and have also defined an element named c of the list "params" , whose value would be used after the attach command? Noah has already pointed out that using attach is a bad idea, even though you see it in some examples and books. There is a way

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

旧城冷巷雨未停 提交于 2019-11-28 08:27:41
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 want to have to define the helper functions within each of my main functions. helpFunction<-function(){ #can I add a line here to alter the environment of this helper function to that of the calling function? return(importantVar1+1) } mainFunction<-function(importantVar1){ return(helpFunction()) } mainFunction(importantVar1=3) #so this should output 4

could not find function inside foreach loop

不想你离开。 提交于 2019-11-28 06:27:17
I'm trying to use foreach to do multicore computing in R. A <-function(....) { foreach(i=1:10) %dopar% { B() } } then I call function A in the console. The problem is I'm calling a function Posdef inside B that is defined in another script file which I source. I had to put Posdef in the list of export argument of foreach : .export=c("Posdef") . However I get the following error: Error in { : task 3 failed - "could not find function "Posdef"" Why cant R find this defined function? The short answer is that this was a bug in parallel backends such as doSNOW , doParallel and doMPI , but it has

Verify object existence inside a function in R [duplicate]

本小妞迷上赌 提交于 2019-11-28 04:27:17
问题 This question already has an answer here: How to check if object (variable) is defined in R? 6 answers I want to determine whether an object exists inside a function in R: foo <- function() { y <- "hello" if (exists(y, envir = sys.frame())) print(y) } foo() Error in exists(y, envir = sys.frame()) : invalid first argument I thought adding the envir = sys.frame() would do the trick. Also tried envir = environment() Expected foo() "hello" 回答1: You should have checked ?exists : Usage: exists(x,