wishlist: functions to manipulate functions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

wishlist: functions to manipulate functions

Deepayan Sarkar
Hi,

Even though R is a functional language and it's common to have
functions as arguments to other functions (notably lapply and
friends), it is not possible to manipulate functions as easily as
other objects. I particularly miss

1. An operator to combine functions (analogous to %*%), e.g.

"%of%" <-
    function(f, g)
{
    function(x) f(g(x))
}

which for instance could be used to do

sapply(ls(), class %of% get)

rather than

sapply(ls(), function(x) class(get(x)))


2. Something to create a new function from an old one with different
defaults, e.g.

update.function <-
    function(object, ...)
{
    args <- list(...)
    function(...)
    {
        dots <- list(...)
        dots[names(args)] <- args
        do.call(object, dots)
    }
}

This is probably more useful in lattice than elsewhere, e.g.

library(lattice)
qqmath(rt(1000, df = 5), distribution = update(qt, df = 5))

instead of

qqmath(rt(1000, df = 5), distribution = function(p) qt(p, df = 5))

Would these be worth adding to the base system?

It would also have been cool if (sin^2 + cos^2) returned a valid
function, but defining Ops.function doesn't seem to have any effect
(probably because oldClass is NULL for functions).

Deepayan
--
http://www.stat.wisc.edu/~deepayan/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Loading...