From hadley at rice.edu  Thu May  5 15:11:23 2011
From: hadley at rice.edu (Hadley Wickham)
Date: Thu, 5 May 2011 08:11:23 -0500
Subject: [Rd] Curry: proposed new functional programming, er, function.
In-Reply-To: <79F23BA7BB084E4FA01A8B93904CD02CF669FAA4D7@WIGGUMVS.win.ad.jhu.edu>
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>
Message-ID: <BANLkTikvfsyZaW4wDB8sA-Z7G-rMQ+11Tg@mail.gmail.com>

> Thanks, Hadley. ?This (i.e., different ways to prepare curry) is quite instructive to me.

The following update makes the generated function look a little nicer
if you pass in the name of a function:

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

      if (is.name(FUN)) {
        fname <- FUN
      } else if (is.character(FUN)) {
        fname <- as.name(FUN)
      } else {
        fname <- as.name("FUN")
      }
      curry_call <- as.call(c(list(fname), args))
      eval(call("function", as.pairlist(alist(... = )), curry_call))
    }

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)

> hadley <- 1:10
> plothadley <- Curry("plot", x = hadley)
> plothadley
function (...)
plot(x = hadley, ... = ...)
<environment: 0x1031d7c00>
> plothadley()
> plothadley(runif(10))

The only (minor) problem is that I couldn't figure out how to
construct the interior call to yield plot(x = hadley, ...)

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