Re: [R] inverse currying

2009-10-02 Thread baptiste auguie
After some more digging (grep alist R-devel/ ), I've come up with this,

tools:::as.alist.symbol(x)

sugar = function(fun, id = id){
 ff - formals(fun)
 if( id %in% names(ff))
   stop(paste(id, is part of args(fun)))

 new.arg - tools:::as.alist.symbol(id)

 formals(fun) - c(unlist(ff), new.arg)
 fun

}

foo = function(x, a=1){
 x
}

sugar(foo)
sugar(foo, 'a')
sugar(sugar(foo))

sugar(foo, 'my.new.arg')


Best,

baptiste



2009/10/1 baptiste auguie baptiste.aug...@googlemail.com:
 Dear list,

 I have the following function,

 sugar = function(fun, id = id){
  ff - formals(fun)
  if( id %in% names(ff))
    stop(id is part of args(fun))
  formals(fun) - c(unlist(ff), alist(id=))
  fun
 }

 which one may use on a function foo,

 foo = function(x){
  x
 }

 sugar(foo) # results in the extended closure,

 function (x, id)
 {
    x
 }

 Its limitation (other than not working with .Primitives) is the 'id'
 tag that I add in the formals of fun(). I don't know how to create a
 alist(id=) pairlist where id can be changed. I tried the usual bquote
 and substitute approach but they don't seem to work here. I suppose I
 could do something like,

 parse(text = paste(alist(,id, =), sep=))

 but this is usually not recommended.

 Any ideas?

 Best regards,

 baptiste


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] inverse currying

2009-10-01 Thread baptiste auguie
Dear list,

I have the following function,

sugar = function(fun, id = id){
  ff - formals(fun)
  if( id %in% names(ff))
stop(id is part of args(fun))
  formals(fun) - c(unlist(ff), alist(id=))
  fun
}

which one may use on a function foo,

foo = function(x){
  x
}

sugar(foo) # results in the extended closure,

function (x, id)
{
x
}

Its limitation (other than not working with .Primitives) is the 'id'
tag that I add in the formals of fun(). I don't know how to create a
alist(id=) pairlist where id can be changed. I tried the usual bquote
and substitute approach but they don't seem to work here. I suppose I
could do something like,

parse(text = paste(alist(,id, =), sep=))

but this is usually not recommended.

Any ideas?

Best regards,

baptiste

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.