Hello,
I tried this pseudo-generator style experiment (see below). The "<<-"
operator assigns to in the calling environment which would be the
environment of  "getN".
Yet when the function incr is returned, isn't this environment lost?
Also the print displays GlobalEnv, yet the globalenv does not have any
mention of startgiven.
This code was inspired from and old issue of RJournal , but I'm not
sure how this works.

I'm not sure how this works and an explanation would be helpful.

Thank you
Saptarshi

getN <- function(start){
  startgiven <- start;
  incr <- function(n=NA){
    print(parent.frame())
    if(!is.na(n)) startgiven<<-n
    startgiven<<-startgiven+1
    return(startgiven)
  }
}
a=getN(10)
a() #11
a()#12

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to