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

Hadley Wickham hadley at rice.edu
Wed May 4 17:53:33 CEST 2011


>> # Use of Curry
>> adaptIntegrate(Curry(f, a=0.2), lower=0, upper=2)
>
> The _concept_ of currying is useful, and maybe more can be done to
> provide guidance and education on how to do it, but adding a function
> that sometimes works and somesimes does surprising things is not the
> way to go.

What about an alternative approach?

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

      curry_call <- as.call(c(list(as.name("FUN")), args))
      function(...) {
        eval(curry_call)
      }
    }

Or maybe

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

      curry_call <- as.call(c(list(as.name("FUN")), args))
      eval(bquote(function(...) .(curry_call)))
    }


I think this is pretty close to what you'd get if you made an
anonymous function (but I'm probably missing something)

Hadley

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



More information about the R-devel mailing list