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

Gabor Grothendieck ggrothendieck at gmail.com
Thu May 5 15:56:27 CEST 2011


On Thu, May 5, 2011 at 9:32 AM, Hadley Wickham <hadley at rice.edu> wrote:
> 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
>      f
>    }
>
> 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.
>

There is some question of whether it should default to the current
environment or the original function's environment.  If the curried
function were regarded as part of a new object whose environment is
the current environment then having the current environment (i.e.  the
object's environment) makes sense but if its regarded purely as an
operation on the original function then the original function's
environment makes more sense.  In any case, an envir= argument to
allow the user to specify it would be desirable.  Perhaps it could
specify the environment to assign (or if a logical or keyword were
specified it would indicate which of the two possibilities just
mentioned to use to make the common choices easy to specify).


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-devel mailing list