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? Thanks very much!
David Reiner
2010-Aug-05 20:47 UTC
[R] [SPAM] - Reducing a list of functions by composition fails - Bayesian Filter detected spam
> Funcall <- function(f, ...) f(...) > Reduce(Funcall,list(plus2,plus3,plus4),init=3,right=TRUE)[1] 12 Basically from the examples from ?Reduce. HTH, David L. Reiner XR Trading LLC -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Mog Sent: Thursday, August 05, 2010 3:32 PM To: r-help at r-project.org Subject: [SPAM] - [R] Reducing a list of functions by composition fails - Bayesian Filter detected spam 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? Thanks very much! ______________________________________________ R-help at 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. This e-mail and any materials attached hereto, including, without limitation, all content hereof and thereof (collectively, "XR Content") are confidential and proprietary to XR Trading, LLC ("XR") and/or its affiliates, and are protected by intellectual property laws. Without the prior written consent of XR, the XR Content may not (i) be disclosed to any third party or (ii) be reproduced or otherwise used by anyone other than current employees of XR or its affiliates, on behalf of XR or its affiliates. THE XR CONTENT IS PROVIDED AS IS, WITHOUT REPRESENTATIONS OR WARRANTIES OF ANY KIND. TO THE MAXIMUM EXTENT PERMISSIBLE UNDER APPLICABLE LAW, XR HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS AND IMPLIED, RELATING TO THE XR CONTENT, AND NEITHER XR NOR ANY OF ITS AFFILIATES SHALL IN ANY EVENT BE LIABLE FOR ANY DAMAGES OF ANY NATURE WHATSOEVER, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, CONSEQUENTIAL, SPECIAL AND PUNITIVE DAMAGES, LOSS OF PROFITS AND TRADING LOSSES, RESULTING FROM ANY PERSON'S USE OR RELIANCE UPON, OR INABILITY TO USE, ANY XR CONTENT, EVEN IF XR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES WERE FORESEEABLE.
I realize that the Funcall example with an init parameter will work, but I'd like to be able to get a one-argument function/closure out of that reduction, if at all possible. Sorry, I under-specified in my question. Thanks!
Gabor Grothendieck
2010-Aug-05 21:01 UTC
[R] Reducing a list of functions by composition fails
On Thu, Aug 5, 2010 at 4:31 PM, Mog <mogunus at 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)