From hadley at rice.edu  Thu May  5 15:32:14 2011
From: hadley at rice.edu (Hadley Wickham)
Date: Thu, 5 May 2011 08:32:14 -0500
Subject: [Rd] Curry: proposed new functional programming, er, function.
In-Reply-To: <BANLkTikGcviLYfFska4vkf5GpuCpmKTdJw@mail.gmail.com>
References: <7098abec0711011200va3851o811f60bb100616ce@mail.gmail.com>
	<BANLkTi=jud5hhis5y-veQKZHzSYYoPqnBw@mail.gmail.com>
	<79F23BA7BB084E4FA01A8B93904CD02CF669FAA4C9@WIGGUMVS.win.ad.jhu.edu>
	<4DC16917.5090206@gmail.com>
	<BANLkTikuy3OP8j5aymKuE_zmHtYLCkBZWg@mail.gmail.com>
	<79F23BA7BB084E4FA01A8B93904CD02CF669FAA4D7@WIGGUMVS.win.ad.jhu.edu>
	<BANLkTikvfsyZaW4wDB8sA-Z7G-rMQ+11Tg@mail.gmail.com>
	<BANLkTikGcviLYfFska4vkf5GpuCpmKTdJw@mail.gmail.com>
Message-ID: <BANLkTik-1s+Af541ZkM+mDVkjRx94WM_4w@mail.gmail.com>

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.

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



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