[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A meta function
- From: Drake Wilson <drake@...>
- Date: Fri, 4 Jan 2013 23:28:57 -0600
Quoth Grizzly Bear <6grizzlybear@gmail.com>, on 2013-01-04 17:11:29 -0800:
(rearranged)
> >> I'd like to have a meta function, sth. like
> >>
> >> function fn_matrix(fn_name, arg_foo)
> >> string2var(fn_name) = function(arg_foo) print(fn_name,
> >> arg_foo) end
> >> end
> > Wouldn't you be able to replace string2var(fn_name) with _G[fn_name] to
> > create a global function of the specified name?
> >
> That's cool. Thanks!
But it might be easier, more composable, and more obvious that a
global is being defined if fn_matrix were to return the new function
instead. Then you'd do:
new_fun = fn_matrix(arg_foo)
new_fun(...)
Note that these are already equivalent:
function f(...) ... end
f = function(...) ... end
(Though if you have another reason to use the _name_ of the desired
function in its construction, then you wind up having to pass the name
in as well, thus specifying it twice, which is not so good---in that
case, the tradeoff may be different.)
---> Drake Wilson