From ggrothendieck at gmail.com  Thu May  5 15:56:27 2011
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Thu, 5 May 2011 09:56:27 -0400
Subject: [Rd] Curry: proposed new functional programming, er, function.
In-Reply-To: <BANLkTik-1s+Af541ZkM+mDVkjRx94WM_4w@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>
	<BANLkTik-1s+Af541ZkM+mDVkjRx94WM_4w@mail.gmail.com>
Message-ID: <BANLkTi=D4yC8SUZe+aNarn3VivHFGHCC_g@mail.gmail.com>

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



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