Hi all,
A function has three components: arguments, body and environment. Is
there no base function that allows us to create a function from those
three components?
The best I could come up with is:
make_function - function(args, body, env = parent.frame()) {
args - as.pairlist(args)
Am I missing a built in way to do this? Also, is there a built in
equivalent to my mquote (= multiquote, for producing a named list of
quoted inputs)?
Oops, built in equivalent to mquote is alist. (It would be really
handy if that was referenced from quote)
Hadley
--
RStudio / Rice
On 03/10/2012 10:16 AM, Hadley Wickham wrote:
Hi all,
A function has three components: arguments, body and environment. Is
there no base function that allows us to create a function from those
three components?
There is: it is `function`. The parser converts your function
definitions into
Hadley,
You could do this:
make_fun = function(args, body, env)
{
f = function()
formals(f) = args
body(f) = body
environment(f) = env
f
}
If for some reason using function() itself as Duncan suggested won't work.
Note: args will need to be the right kind of pairlist, eg what is returned
from
There is: it is `function`. The parser converts your function definitions
into a call to it. (It has 3 arguments: the formals, the body, and the
srcref. The environment is added when it is evaluated.)
So your make_function below is pretty similar (but because `function` is
primitive,
On Wed, Oct 3, 2012 at 9:33 AM, Gabriel Becker gmbec...@ucdavis.edu wrote:
Hadley,
You could do this:
make_fun = function(args, body, env)
{
f = function()
formals(f) = args
body(f) = body
environment(f) = env
f
}
If for some reason using function() itself as Duncan suggested won't
Err, typo: function() should of course say function() {}
~G
On Wed, Oct 3, 2012 at 7:49 AM, Hadley Wickham h.wick...@gmail.com wrote:
On Wed, Oct 3, 2012 at 9:33 AM, Gabriel Becker gmbec...@ucdavis.edu
wrote:
Hadley,
You could do this:
make_fun = function(args, body, env)
{
f =
On Oct 3, 2012, at 16:49 , Hadley Wickham wrote:
On Wed, Oct 3, 2012 at 9:33 AM, Gabriel Becker gmbec...@ucdavis.edu wrote:
Hadley,
You could do this:
make_fun = function(args, body, env)
{
f = function()
formals(f) = args
body(f) = body
environment(f) = env
f
}
If for some
On Wed, Oct 3, 2012 at 7:47 AM, Hadley Wickham h.wick...@gmail.com wrote:
There is: it is `function`. The parser converts your function definitions
into a call to it. (It has 3 arguments: the formals, the body, and the
srcref. The environment is added when it is evaluated.)
So your
I think `function` does not eval its arguments, and it demands a
pairlist. So this works:
f - eval(substitute(`function`(args, body),
list(args=as.pairlist(alist(a=1)), body=quote(a+1
The other thing to notice is a syntax difference between function and
ordinary calls: when writing
10 matches
Mail list logo