Re: [julia-users] Macros generating Functions

2015-06-02 Thread Tom Lee
I think I was channeling this line from the Style Guide section of the manual when I made my earlier comment on preferring functions: Don’t overuse macros Be aware of when a macro could really be a function instead. [then a sensible warning about not using @eval inside a macro] But I suppose

Re: [julia-users] Macros generating Functions

2015-06-01 Thread Josh Langsfeld
You're right, and the manual even talks about the using eval to prevent code reuse. I guess it also depends on whether you are creating a bunch of functions in a one-time shot, or exposing a function creation interface that will be called as needed. Your example seems to be a case of the

Re: [julia-users] Macros generating Functions

2015-05-31 Thread Yichao Yu
On Sun, May 31, 2015 at 8:48 AM, Tom Lee m...@tomlee.id.au wrote: No, in that example you create an anonymous function. I'm not certain on the semantics, but n effectively points to this nameless function. Anonymous functions are not as fast as generic ones (at least in 0.3, at least

Re: [julia-users] Macros generating Functions

2015-05-31 Thread Tom Lee
I'll concede that if you know the function name at runtime, Mauro's solution may be a little cleaner, especially if it will be called a lot. There are plenty of examples in Base of @eval being used to define functions, such as lines 11-18 here:

Re: [julia-users] Macros generating Functions

2015-05-31 Thread Kevin Squire
Actually, it's name is n: julia function getfn() return function(); 1; end end getfn (generic function with 1 method) julia const n = getfn() (anonymous function) julia n() 1 On Sat, May 30, 2015 at 5:09 PM, David P. Sanders dpsand...@gmail.com wrote: El domingo, 31 de mayo

Re: [julia-users] Macros generating Functions

2015-05-31 Thread Tom Lee
No, in that example you create an anonymous function. I'm not certain on the semantics, but n effectively points to this nameless function. Anonymous functions are not as fast as generic ones (at least in 0.3, at least sometimes) and you cannot add new methods to them for multiple dispatch (as

Re: [julia-users] Macros generating Functions

2015-05-30 Thread Jameson Nash
But @eval is still a macro, so it is even better to rewrite this without that: function getfn() return function(); 1; end end const n = getfn() On Sat, May 30, 2015 at 2:30 PM David Gold david.gol...@gmail.com wrote: Something to note about Tom's method is that the name function must be passed

Re: [julia-users] Macros generating Functions

2015-05-30 Thread David P. Sanders
El domingo, 31 de mayo de 2015, 0:37:45 (UTC+2), Jameson escribió: But @eval is still a macro, so it is even better to rewrite this without that: function getfn() return function(); 1; end end const n = getfn() This does not give quite the same answer, though, since the function does

Re: [julia-users] Macros generating Functions

2015-05-30 Thread David Gold
Something to note about Tom's method is that the name function must be passed to gf as a symbol, unlike in the case of a macro. However, in most cases this slight difference probably will not warrant a macro. On Friday, May 29, 2015 at 8:58:56 PM UTC-4, Tom Lee wrote: You don't need to use a

Re: [julia-users] Macros generating Functions

2015-05-29 Thread Tom Lee
You don't need to use a macro, a function can do this: julia function gf(n::Symbol = gensym()) @eval function $(n)() 1 end end I've also made the n argument optional, with gensym creating a unique name by default - the newly defined function is returned by gf, so

Re: [julia-users] Macros generating Functions

2015-05-28 Thread Mauro
Like this: julia macro gf(n) quote function $(esc(n))() 1 end end end julia @gf foo foo (generic function with 1 method) julia foo() 1 On Thu, 2015-05-28 at 12:06, Vasudha Khandelwal vasudhakhandelw...@gmail.com wrote: Can I use macros to generate

[julia-users] Macros generating Functions

2015-05-28 Thread Vasudha Khandelwal
Can I use macros to generate functions with names passed as argument to the macro?