Re: R-alpha: length and as.list on functions

Bill Venables (wvenable@attunga.stats.adelaide.edu.au)
Tue, 9 Apr 1996 12:37:13 +0930


Date: Tue, 9 Apr 1996 12:37:13 +0930
Message-Id: <9604090307.AA05875@attunga.stats.adelaide.edu.au>
From: Bill Venables <wvenable@attunga.stats.adelaide.edu.au>
To: Robert Gentleman <rgentlem@stat.auckland.ac.nz>
Subject: Re: R-alpha: length and as.list on functions
In-Reply-To: <199604090145.NAA02401@stat.auckland.ac.nz>

>>>>> "Robert" == Robert Gentleman <rgentlem@stat.auckland.ac.nz> writes:

    Robert> I haven't been able to figure out why you would want
    Robert> access to the body of the function.  Any suggestions
    Robert> about where and why this would be nice?

It all depends on whether you wish to support any operations on
the language itself.  Things like the function deriv() that
return a function as a result use this facility in a big way.
Here is another simpler example from my toy polynomial library
that takes a polynomial object and returns an S function to
evaluate it.

In the example below the plan is

1. set up a null function to get the header, 
2. construct the function body first as character strings,
3. parse, 
4. make the result of mode "{", and finally
5. assign it to the initially null function body.

Is this kind of thing likely to be within the scope of R?

Bill
-----------------------------------------------------------------
S-PLUS : Copyright (c) 1988, 1995 MathSoft, Inc.
S : Copyright AT&T.
Version 3.3 Release 1 for Sun SPARC, SunOS 4.1.x : 1995 
Working data will be in .Data 
>				 # first see the thing in action.
> library(Polynomial)
> p <- polynomial(1:6)
> p
1 + 2*x + 3*x^2 + 4*x^3 + 5*x^4 + 6*x^5 
> as.function(p)
function(x, nam = as.character(x))
{
	val <- 1 + x * (2 + x * (3 + x * (4 + x * (5 + x * (6)))
		))
	if(!inherits(val, "polynomial"))
		names(val) <- nam
	val
}
>			# here is the method function that does it:
> as.function.polynomial
function(p)
{
	f <- function(x, nam = as.character(x))
	NULL
	body <- c(paste("val <- ", make.character(p, style = "horner")),
		"if(!inherits(val, \"polynomial\")) names(val) <- nam",
		"val")
	body <- parse(text = body)
	mode(body) <- "{"
	f[[3]] <- body
	f
}
> 
-- 
_________________________________________________________________
William Venables, Department of Statistics,  Tel.: +61 8 303 3026
The University of Adelaide,                  Fax.: +61 8 303 3696
South AUSTRALIA.     5005.   Email: Bill.Venables@adelaide.edu.au
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- To (un)subscribe, send
subscribe	or	unsubscribe
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-