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

From: Hadley Wickham <hadley_at_rice.edu>
Date: Fri, 25 May 2012 06:49:28 -0700

> 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).

That's easily fixed:

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 <- 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
}

Curry(Curry(foo,3),4)

> 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).

I don't think that would be a good idea - there's a big difference between a function with no arguments and the result of calling that function.

Hadley

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

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Fri 25 May 2012 - 13:52:52 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 25 May 2012 - 21:32:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive