On Thu, Aug 5, 2010 at 4:31 PM, Mog <mogu...@gmail.com> wrote:
> Hi All,
>
> I'd like to be able to specify the ordered composition of several
> functions. So I defined compose:
>
> compose <- function(f,g){
>  function(x){f(g(x))}
> }
>
> and some simple test functions:
>
> plus2 <- function(x){x+2}
> plus3 <- function(x){x+3}
> plus4 <- function(x){x+4}
>
>>  (compose(plus2,compose(plus3,plus4)))(3)
> [1] 12
>
> Works fine. But trying to reduce a list of functions by compose fails:
>
>> Reduce(compose,list(plus2,plus3,plus4),right=TRUE)(3)
> Error: evaluation nested too deeply: infinite recursion / 
> options(expressions=)?
>
> As far as I can tell, that reduction should be the same as my call
> above. What am I doing wrong?

Its due to lazy evaluation.  Insert this at the top of compose:

   force(f); force(g)

______________________________________________
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