# Force evaluation of a symbol when a function is created

11 messages
Open this post in threaded view
|
Report Content as Inappropriate

## Force evaluation of a symbol when a function is created

 I am porting a program in matlab to R, The problem is that Matlab has a feature where symbols that aren't arguments are evaluated immediately. That is: Y=3 F=@(x) x*Y Will yield a function such that F(2)=6. If later say. Y=4 then F(2) will still equal 6. R on the other hand has lazy evaluation. F<-function(x){x*Y} Will do the following Y=3 F(2)=6 Y=4 F(2)=8. Does anyone know of away to defeat lazy evaluation in R so that I can easily simulate the Matlab behavior.  I know that I can live without this in ordinary programming but it would make my port much easier. Thanks. The information in this e-mail is intended only for the ...{{dropped:14}} ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Force evaluation of a symbol when a function is created

 You could use local(), as in    > F <- local({    +        Y <- 3    +        function(x) x * Y    +    })    >    F(7)    [1] 21    > Y <- 19    > F(5)   [1] 15 Look into 'environments' for more. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: [hidden email] [mailto:[hidden email]] On Behalf > Of Schoenfeld, David Alan,Ph.D.,Biostatistics > Sent: Monday, August 06, 2012 2:08 PM > To: '[hidden email]' > Subject: [R] Force evaluation of a symbol when a function is created > > > I am porting a program in matlab to R, > The problem is that Matlab has a feature where symbols that aren't arguments are > evaluated immediately. > That is: > Y=3 > F=@(x) x*Y > > Will yield a function such that F(2)=6. > If later say. Y=4 then F(2) will still equal 6. > > R on the other hand has lazy evaluation. > F<-function(x){x*Y} > Will do the following > Y=3 > F(2)=6 > Y=4 > F(2)=8. > Does anyone know of away to defeat lazy evaluation in R so that I can easily simulate the > Matlab behavior.  I know that I can live without this in ordinary programming but it would > make my port much easier. > > Thanks. > > > > > The information in this e-mail is intended only for the ...{{dropped:14}} > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Force evaluation of a symbol when a function is created

 Thanks to both: Cute question, clever, informative answer. However, Bill, I don't think you **quite** answered him, although the modification needed is completely trivial. Of course, I could never have figured it out without your response. Anyway, I interpret the question as asking for the function definition to _implicitly_ pick up the value of Y at the time the function is defined, rather than explicitly assigning it in local(). The following are two essentially identical approaches: I prefer the second, because it's more transparent to me, but that's just a matter of taste. Y <- 3 F <-local({y <- Y;function(x)x*y}) G <- evalq(function(x)x*y,env=list(y=Y)) Yielding: > Y <- 3 > F <-local({y <- Y;function(x)x*y}) > G <- evalq(function(x)x*y,env=list(y=Y)) > F(5) [1] 15 > G(5) [1] 15 > Y <- 2 > F(5) [1] 15 > G(5) [1] 15 > F <-local({y <- Y;function(x)x*y}) > G <- evalq(function(x)x*y,env=list(y=Y)) > F(5) [1] 10 > G(5) [1] 10 Cheers, Bert On Mon, Aug 6, 2012 at 2:24 PM, William Dunlap <[hidden email]> wrote: > You could use local(), as in >    > F <- local({ >    +        Y <- 3 >    +        function(x) x * Y >    +    }) >    >    F(7) >    [1] 21 >    > Y <- 19 >    > F(5) >   [1] 15 > > Look into 'environments' for more. > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > >> -----Original Message----- >> From: [hidden email] [mailto:[hidden email]] On Behalf >> Of Schoenfeld, David Alan,Ph.D.,Biostatistics >> Sent: Monday, August 06, 2012 2:08 PM >> To: '[hidden email]' >> Subject: [R] Force evaluation of a symbol when a function is created >> >> >> I am porting a program in matlab to R, >> The problem is that Matlab has a feature where symbols that aren't arguments are >> evaluated immediately. >> That is: >> Y=3 >> F=@(x) x*Y >> >> Will yield a function such that F(2)=6. >> If later say. Y=4 then F(2) will still equal 6. >> >> R on the other hand has lazy evaluation. >> F<-function(x){x*Y} >> Will do the following >> Y=3 >> F(2)=6 >> Y=4 >> F(2)=8. >> Does anyone know of away to defeat lazy evaluation in R so that I can easily simulate the >> Matlab behavior.  I know that I can live without this in ordinary programming but it would >> make my port much easier. >> >> Thanks. >> >> >> >> >> The information in this e-mail is intended only for t...{{dropped:26}} ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Force evaluation of a symbol when a function is created

 In reply to this post by dschoenfeld@partners.org Hi, Try this: Â F<-function(x,type="local"){Y=3 Â x*Y} F(3) #[1] 9 Â Y<-4 Â F(3) #[1] 9 Â Y<-5 Â F(3) #[1] 9 A.K. ----- Original Message ----- From: "Schoenfeld, David Alan,Ph.D.,Biostatistics" <[hidden email]> To: "'[hidden email]'" <[hidden email]> Cc: Sent: Monday, August 6, 2012 5:07 PM Subject: [R] Force evaluation of a symbol  when a function is created I am porting a program in matlab to R, The problem is that Matlab has a feature where symbols that aren't arguments are evaluated immediately. That is: Y=3 F=@(x) x*Y Will yield a function such that F(2)=6. If later say. Y=4 then F(2) will still equal 6. R on the other hand has lazy evaluation. F<-function(x){x*Y} Will do the following Y=3 F(2)=6 Y=4 F(2)=8. Does anyone know of away to defeat lazy evaluation in R so that I can easily simulate the Matlab behavior.Â  I know that I can live without this in ordinary programming but it would make my port much easier. Thanks. The information in this e-mail is intended only for the ...{{dropped:14}} ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Force evaluation of a symbol when a function is created

Open this post in threaded view
|
Report Content as Inappropriate

## Re: Force evaluation of a symbol when a function is created

Open this post in threaded view
|
Report Content as Inappropriate

## Re: Force evaluation of a symbol when a function is created

 On Mon, Aug 6, 2012 at 9:03 PM, Schoenfeld, David Alan,Ph.D.,Biostatistics <[hidden email]> wrote: > Thank you both, this was very helpful.  I need to study environments more. Do either of you know a good source? Disclaimer: I really have no idea what I'm talking about. They are a somewhat subtle, but exceptionally powerful concept: see, inter alia, cran.r-project.org/doc/contrib/Fox-Companion/appendix-scope.pdf http://www.lemnica.com/esotericR/Introducing-Closures/If you know a little bit of C, it will go a long way in understanding environments in R. You'll want to (eventually) start to associate R names with C pointers and environments with symbol tables (hence the fact the printed environment is just a memory address) , but that's perhaps a little bit down the road. Environments are different in their fundamental behavior because of this though: they're the best way to get pass by reference in R. Best, Michael ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Force evaluation of a symbol when a function is created

 In reply to this post by dschoenfeld@partners.org Here's one more way.  It seems to me this is the most R-like way to do what you want:    multiply_by_Y <- function(Y) {      force(Y)      function(x) x*Y    }    F <- multiply_by_Y(3) The "force" call forces Y to be evaluated at that point, so its value is fixed from that point forward. Duncan Murdoch On 12-08-06 5:07 PM, Schoenfeld, David Alan,Ph.D.,Biostatistics wrote: > > I am porting a program in matlab to R, > The problem is that Matlab has a feature where symbols that aren't arguments are evaluated immediately. > That is: > Y=3 > F=@(x) x*Y > > Will yield a function such that F(2)=6. > If later say. Y=4 then F(2) will still equal 6. > > R on the other hand has lazy evaluation. > F<-function(x){x*Y} > Will do the following > Y=3 > F(2)=6 > Y=4 > F(2)=8. > Does anyone know of away to defeat lazy evaluation in R so that I can easily simulate the Matlab behavior.  I know that I can live without this in ordinary programming but it would make my port much easier. > > Thanks. > > > > > The information in this e-mail is intended only for the ...{{dropped:14}} > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Force evaluation of a symbol when a function is created

 Duncan, et.al: Many thanks: let the closure do the work automatically rather than manually manipulating it. However, in the spirit of the OP's original request, I believe the call would be: >Y <- 3  ## That is, Y gets a value at some prior point, perhaps programmatically. >   F <- multiply_by_Y(Y) # ... F picks up this value "implicitly" -- no need for explicit assignment. But then there is no need for force(), is there? > fy <- function(Y)function(x) x*Y > Y <- 2 > F <- fy(Y) > F(5) [1] 10 > Y <- 3 > F(5) [1] 10 > G <- fy(Y) > G(5) [1] 15 That is, one simply relies on lexical scoping/closures to "retain" the value of  Y used as a free variable in  function(x)x*Y when it is defined. No need to explicitly force() it. If wrong, I would be grateful for correction. This appears to me to duplicate the Matlab behavior rather closely. -- Bert On Tue, Aug 7, 2012 at 3:48 AM, Duncan Murdoch <[hidden email]> wrote: > Here's one more way.  It seems to me this is the most R-like way to do what > you want: > >   multiply_by_Y <- function(Y) { >     force(Y) >     function(x) x*Y >   } > >   F <- multiply_by_Y(3) > > The "force" call forces Y to be evaluated at that point, so its value is > fixed from that point forward. > > Duncan Murdoch > > On 12-08-06 5:07 PM, Schoenfeld, David Alan,Ph.D.,Biostatistics wrote: >> >> >> I am porting a program in matlab to R, >> The problem is that Matlab has a feature where symbols that aren't >> arguments are evaluated immediately. >> That is: >> Y=3 >> F=@(x) x*Y >> >> Will yield a function such that F(2)=6. >> If later say. Y=4 then F(2) will still equal 6. >> >> R on the other hand has lazy evaluation. >> F<-function(x){x*Y} >> Will do the following >> Y=3 >> F(2)=6 >> Y=4 >> F(2)=8. >> Does anyone know of away to defeat lazy evaluation in R so that I can >> easily simulate the Matlab behavior.  I know that I can live without this in >> ordinary programming but it would make my port much easier. >> >> Thanks. >> >> >> >> >> The information in this e-mail is intended only for the ...{{dropped:14}} >> >> ______________________________________________ >> [hidden email] 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. >> > > ______________________________________________ > [hidden email] 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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Force evaluation of a symbol when a function is created

 On 12-08-07 10:46 AM, Bert Gunter wrote: > Duncan, et.al: > > Many thanks: let the closure do the work automatically rather than > manually manipulating it. > > However, in the spirit of the OP's original request, I believe the > call would be: > >> Y <- 3  ## That is, Y gets a value at some prior point, perhaps programmatically. >>    F <- multiply_by_Y(Y) # ... F picks up this value "implicitly" -- no need for explicit assignment. > > But then there is no need for force(), is there? You still need force: fy <- function(Y)function(x) x*Y Y <- 2 F <- fy(Y) Y <- 3 F(5) This will print 15, because F only contains a promise to evaluate Y, it hasn't been evaluated until the very last line, and by that time Y has been changed to 3. If you are going to construct functions in functions, and their results depend on the arguments to the constructor, then it's almost always a good idea to force the arguments.  Sometimes it isn't necessary (the value will be forced implicitly), and in some rare circumstances you might want to capture the promise instead of its value, but it's generally a good idea.  It is a fairly cheap operation. Duncan Murdoch > >> fy <- function(Y)function(x) x*Y >> Y <- 2 >> F <- fy(Y) >> F(5) > [1] 10 >> Y <- 3 >> F(5) > [1] 10 >> G <- fy(Y) >> G(5) > [1] 15 > > That is, one simply relies on lexical scoping/closures to "retain" the > value of  Y used as a free variable in  function(x)x*Y when it is > defined. No need to explicitly force() it. If wrong, I would be > grateful for correction. This appears to me to duplicate the Matlab > behavior rather closely. > > > -- Bert > > On Tue, Aug 7, 2012 at 3:48 AM, Duncan Murdoch <[hidden email]> wrote: >> Here's one more way.  It seems to me this is the most R-like way to do what >> you want: >> >>    multiply_by_Y <- function(Y) { >>      force(Y) >>      function(x) x*Y >>    } >> >>    F <- multiply_by_Y(3) >> >> The "force" call forces Y to be evaluated at that point, so its value is >> fixed from that point forward. >> >> Duncan Murdoch >> >> On 12-08-06 5:07 PM, Schoenfeld, David Alan,Ph.D.,Biostatistics wrote: >>> >>> >>> I am porting a program in matlab to R, >>> The problem is that Matlab has a feature where symbols that aren't >>> arguments are evaluated immediately. >>> That is: >>> Y=3 >>> F=@(x) x*Y >>> >>> Will yield a function such that F(2)=6. >>> If later say. Y=4 then F(2) will still equal 6. >>> >>> R on the other hand has lazy evaluation. >>> F<-function(x){x*Y} >>> Will do the following >>> Y=3 >>> F(2)=6 >>> Y=4 >>> F(2)=8. >>> Does anyone know of away to defeat lazy evaluation in R so that I can >>> easily simulate the Matlab behavior.  I know that I can live without this in >>> ordinary programming but it would make my port much easier. >>> >>> Thanks. >>> >>> >>> >>> >>> The information in this e-mail is intended only for the ...{{dropped:14}} >>> >>> ______________________________________________ >>> [hidden email] 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. >>> >> >> ______________________________________________ >> [hidden email] 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. > > > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.