[R] Curious behaviour of lapply and lists of functions

Julio Sergio Santana juliosergio at gmail.com
Thu Jan 16 02:20:25 CET 2014


Julio Sergio Santana <juliosergio <at> gmail.com> writes:

> ...
>     Producer <- function(f) function(x) 1/f(x)
> 

Counsulting a previous post, I got to the solution, I just need to rewrite 
the function Producer forcing it to eavaluate its argument, as follows

    Producer <- function(f) {f ;function(x) 1/f(x)}

Then, 

    linv <- lapply(lf, Producer)

    linv[[1]](3)
   [1] 0.125

    linv[[2]](3)
   [1] 0.1666667

Which is the desired result.

Thanks everybody for reading.

  -Sergio.




More information about the R-help mailing list