[Rd] as.list() for function:s

Martin Maechler maechler at stat.math.ethz.ch
Fri Oct 17 11:27:51 CEST 2008


>>>>> "HenrikB" == Henrik Bengtsson <hb at stat.berkeley.edu>
>>>>>     on Thu, 16 Oct 2008 12:51:40 -0700 writes:

    HenrikB> Hi, a quick comment.  I just notice that as.list() deals with
    HenrikB> function:s the old way inside the "default" function, cf.

    >> as.list.default
    HenrikB> function (x, ...)
    HenrikB> {
    HenrikB> if (typeof(x) == "list")
    HenrikB> return(x)
    HenrikB> if (is.function(x))
    HenrikB> return(c(formals(x), list(body(x))))
    HenrikB> .Internal(as.vector(x, "list"))
    HenrikB> }
    HenrikB> <environment: namespace:base>

    HenrikB> The following should do the same thing cleaner (and faster?):

    HenrikB> as.list.function <- function(x, ...)
    HenrikB> {
    HenrikB> c(formals(x), list(body(x)))
    HenrikB> }

    HenrikB> as.list.default <- function (x, ...)
    HenrikB> {
    HenrikB> if (typeof(x) == "list")
    HenrikB> return(x)
    HenrikB> .Internal(as.vector(x, "list"))
    HenrikB> }

I don't think it will be faster, but definitely cleaner.
I've implemented (a version of) it for R-devel.

Martin



More information about the R-devel mailing list