[Rd] Curry: proposed new functional programming, er, function.
Hadley Wickham
hadley at rice.edu
Thu May 5 15:11:23 CEST 2011
> Thanks, Hadley. This (i.e., different ways to prepare curry) is quite instructive to me.
The following update makes the generated function look a little nicer
if you pass in the name of a function:
Curry <- function(FUN, ...) {
args <- match.call(expand.dots = FALSE)$...
args$... <- as.name("...")
if (is.name(FUN)) {
fname <- FUN
} else if (is.character(FUN)) {
fname <- as.name(FUN)
} else {
fname <- as.name("FUN")
}
curry_call <- as.call(c(list(fname), args))
eval(call("function", as.pairlist(alist(... = )), curry_call))
}
And it seems to work ok in terms of preserving evaluation (I can't how
it could be any different to making the anonymous function yourself,
unless I've missed something subtle with environments and scoping)
> hadley <- 1:10
> plothadley <- Curry("plot", x = hadley)
> plothadley
function (...)
plot(x = hadley, ... = ...)
<environment: 0x1031d7c00>
> plothadley()
> plothadley(runif(10))
The only (minor) problem is that I couldn't figure out how to
construct the interior call to yield plot(x = hadley, ...)
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
More information about the R-devel
mailing list