From yikelu.home at gmail.com  Wed May 23 21:53:25 2012
From: yikelu.home at gmail.com (Yike Lu)
Date: Wed, 23 May 2012 12:53:25 -0700 (PDT)
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: <1337802805327-4631127.post@n4.nabble.com>


Hadley Wickham-2 wrote
> 
>     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.
> 
I've been playing around with this for a while. One flaw I found - it
doesn't handle nested Curries very well (the naive implementation in
roxygen/functional does).

e.g.:

foo=function(x,y,z) x+y+z
Curry(Curry("foo",3),4)(3)
# 10

Curry(Curry(foo,3),4)(3)
# hangs

foo4=function(a,b,c,d)
Curry(Curry(Curry("foo4",3),4),1)(3)
# hangs

I was also curious if there was some trick to force a function eval when the
list of arguments got exhausted (for example, a triple Curry on foo above
would leave no arguments so would trigger eval into 10).

--
View this message in context: http://r.789695.n4.nabble.com/Curry-proposed-new-functional-programming-er-function-tp917654p4631127.html
Sent from the R devel mailing list archive at Nabble.com.



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