From maechler at stat.math.ethz.ch  Fri Oct 17 11:27:51 2008
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Fri, 17 Oct 2008 11:27:51 +0200
Subject: [Rd] as.list() for function:s
In-Reply-To: <59d7961d0810161251rc42cb42w3f4dd62bcf82010e@mail.gmail.com>
References: <59d7961d0810161251rc42cb42w3f4dd62bcf82010e@mail.gmail.com>
Message-ID: <18680.23191.369921.592687@stat.math.ethz.ch>

>>>>> "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



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