[Rd] Curry: proposed new functional programming, er, function.

Hadley Wickham hadley at rice.edu
Thu May 5 15:32:14 CEST 2011

On Thu, May 5, 2011 at 8:25 AM, Hadley Wickham <hadley at rice.edu> wrote:
>> 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)
> Which indeed I have.  Hmmm, need to think about this more.

Ok, next version.  To follow the usual rules of function scope, we
need to make sure the environment of the function is set to the
environment in which it is created.

    Curry <- function(FUN, ...) {
      args <- match.call(expand.dots = FALSE)$...
      args$... <- as.name("...")

      env <- parent.frame()

      if (is.name(FUN)) {
        fname <- FUN
      } else if (is.character(FUN)) {
        fname <- as.name(FUN)
      } else if (is.function(FUN)){
        fname <- as.name("FUN")
        env$FUN <- FUN
      } else {
        stop("FUN not function or name of function")
      curry_call <- as.call(c(list(fname), args))

      f <- eval(call("function", as.pairlist(alist(... = )), curry_call))
      environment(f) <- env

But I've probably forgotten something else.  Hopefully Luke will chime
in if I'm proceeding down a path that can never be made to work
completely correctly.


Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University

More information about the R-devel mailing list