wishlist: functions to manipulate functions

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

wishlist: functions to manipulate functions

Deepayan Sarkar

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(...)
        dots <- list(...)
        dots[names(args)] <- args
        do.call(object, dots)

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

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).


[hidden email] mailing list