Re: [R] list of closures

2010-08-26 Thread Stephen T.

Hi Philippe, thanks for the suggestion - for my smaller problems I find that 
closures are quicker to define and deploy. At larger scales, I've implemented 
S4 objects with methods and attributes - though while elegant, I find that OO 
(apart from what's built-in) adds significant biolerplate in defining classes, 
so try to avoid it when I can... but I don't know much about the proto package 
except that it loads with gsubfn() - might look into it. Thanks!Stephen

> Date: Thu, 26 Aug 2010 06:51:50 +0200
> From: phgrosj...@sciviews.org
> To: obsessiv...@hotmail.com
> CC: ggrothendi...@gmail.com; r-help@r-project.org
> Subject: Re: [R] list of closures
> 
> Unless for learning R, you should really consider R.oo or proto packages 
> that may be more convenient for you (but you don't provide enough 
> context to tell).
> Best,
> 
> Philippe
> 
> On 26/08/10 06:28, Gabor Grothendieck wrote:
> > On Thu, Aug 26, 2010 at 12:04 AM, Stephen T.  
> > wrote:
> >>
> >> Hi, I wanted to create a list of closures. When I use Map(), mapply(), 
> >> lapply(), etc., to create this list, it appears that the wrong arguments 
> >> are being passed to the main function. For example:
> >> Main function:
> >>> adder<- function(x) function(y) x + y
> >> Creating list of closures with Map():
> >>> plus<- Map(adder,c(one=1,two=2))>  plus$one(1)[1] 3>  plus$two(1)[1] 3
> >> Examining what value was bound to "x":
> >>> Map(function(fn) get("x",environment(fn)),plus)$one[1] 2$two[1] 2
> >>
> >> This is what I had expected:
> >>> plus<- list(one=adder(1),two=adder(2))>  plus$one(1)[1] 2>  
> >>> plus$two(1)[1] 3
> >>> Map(function(fn) get("x",environment(fn)),plus)$one[1] 1$two[1] 2
> >>
> >> Anyone know what's going on? Thanks much!
> >
> > R uses lazy evaluation of function arguments.  Try forcing x:
> >
> >> adder<- function(x) { force(x); function(y) x + y }
> >> plus<- Map(adder,c(one=1,two=2))
> >> plus$one(1)
> > [1] 2
> >> plus$two(1)
> > [1] 3
> >
> > __
> > 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.
> >
> >
  
[[alternative HTML version deleted]]

__
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.


Re: [R] list of closures

2010-08-26 Thread Stephen T.

Wow, I was aware of R's lazy evaluation but didn't realize that was the cause 
here. If the function, adder(), was not simple as I'd defined, I suppose I 
could also force the evaluation of "x" in a wrapping function before passing it 
to adder() as an alternative:
> adder <- function(x) function(y) x + y> plus <- Map(function(x) {force(x); 
> adder(x)},c(one=1,two=2))> plus$one(1)[1] 2> plus$two(1)[1] 3
Thanks Gabor!

> From: ggrothendi...@gmail.com
> Date: Thu, 26 Aug 2010 00:28:34 -0400
> Subject: Re: [R] list of closures
> To: obsessiv...@hotmail.com
> CC: r-help@r-project.org
> 
> On Thu, Aug 26, 2010 at 12:04 AM, Stephen T.  wrote:
> >
> > Hi, I wanted to create a list of closures. When I use Map(), mapply(), 
> > lapply(), etc., to create this list, it appears that the wrong arguments 
> > are being passed to the main function. For example:
> > Main function:
> >> adder <- function(x) function(y) x + y
> > Creating list of closures with Map():
> >> plus <- Map(adder,c(one=1,two=2))> plus$one(1)[1] 3> plus$two(1)[1] 3
> > Examining what value was bound to "x":
> >> Map(function(fn) get("x",environment(fn)),plus)$one[1] 2$two[1] 2
> >
> > This is what I had expected:
> >> plus <- list(one=adder(1),two=adder(2))> plus$one(1)[1] 2> plus$two(1)[1] 3
> >> Map(function(fn) get("x",environment(fn)),plus)$one[1] 1$two[1] 2
> >
> > Anyone know what's going on? Thanks much!
> 
> R uses lazy evaluation of function arguments.  Try forcing x:
> 
> > adder <- function(x) { force(x); function(y) x + y }
> > plus <- Map(adder,c(one=1,two=2))
> > plus$one(1)
> [1] 2
> > plus$two(1)
> [1] 3
  
[[alternative HTML version deleted]]

__
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.


Re: [R] list of closures

2010-08-25 Thread Philippe Grosjean
Unless for learning R, you should really consider R.oo or proto packages 
that may be more convenient for you (but you don't provide enough 
context to tell).

Best,

Philippe

On 26/08/10 06:28, Gabor Grothendieck wrote:

On Thu, Aug 26, 2010 at 12:04 AM, Stephen T.  wrote:


Hi, I wanted to create a list of closures. When I use Map(), mapply(), 
lapply(), etc., to create this list, it appears that the wrong arguments are 
being passed to the main function. For example:
Main function:

adder<- function(x) function(y) x + y

Creating list of closures with Map():

plus<- Map(adder,c(one=1,two=2))>  plus$one(1)[1] 3>  plus$two(1)[1] 3

Examining what value was bound to "x":

Map(function(fn) get("x",environment(fn)),plus)$one[1] 2$two[1] 2


This is what I had expected:

plus<- list(one=adder(1),two=adder(2))>  plus$one(1)[1] 2>  plus$two(1)[1] 3
Map(function(fn) get("x",environment(fn)),plus)$one[1] 1$two[1] 2


Anyone know what's going on? Thanks much!


R uses lazy evaluation of function arguments.  Try forcing x:


adder<- function(x) { force(x); function(y) x + y }
plus<- Map(adder,c(one=1,two=2))
plus$one(1)

[1] 2

plus$two(1)

[1] 3

__
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.




__
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.


Re: [R] list of closures

2010-08-25 Thread Gabor Grothendieck
On Thu, Aug 26, 2010 at 12:04 AM, Stephen T.  wrote:
>
> Hi, I wanted to create a list of closures. When I use Map(), mapply(), 
> lapply(), etc., to create this list, it appears that the wrong arguments are 
> being passed to the main function. For example:
> Main function:
>> adder <- function(x) function(y) x + y
> Creating list of closures with Map():
>> plus <- Map(adder,c(one=1,two=2))> plus$one(1)[1] 3> plus$two(1)[1] 3
> Examining what value was bound to "x":
>> Map(function(fn) get("x",environment(fn)),plus)$one[1] 2$two[1] 2
>
> This is what I had expected:
>> plus <- list(one=adder(1),two=adder(2))> plus$one(1)[1] 2> plus$two(1)[1] 3
>> Map(function(fn) get("x",environment(fn)),plus)$one[1] 1$two[1] 2
>
> Anyone know what's going on? Thanks much!

R uses lazy evaluation of function arguments.  Try forcing x:

> adder <- function(x) { force(x); function(y) x + y }
> plus <- Map(adder,c(one=1,two=2))
> plus$one(1)
[1] 2
> plus$two(1)
[1] 3

__
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.