[R] Compose in roxygen - order of function application?

Stephen Tucker brown_emu at yahoo.com
Wed Feb 10 18:37:53 CET 2010


Hello,

I wonder if anyone has used the Compose() function in the 'roxygen' package. I find its behavior a bit surprising:

> f <- function(x) x + 1
> g <- function(x) x * 2
> f(g(2))
[1] 5
> Compose(f,g)(2)
[1] 6
> g(f(2))
[1] 6
> Compose(g,f)(2)
[1] 5

I would have expected Compose(f,g)(x) == f(g(x)) but it appears the order of application is reversed. It would be easy for me to modify with rev():
Compose <- function (...) {
  fs <- rev(list(...))
  function(...) Reduce(function(x, f) f(x), fs, ...)
}

But does the current implementation follow another convention I am unaware of?

Thanks -

Stephen



More information about the R-help mailing list