On Wed, 2015-06-17 at 21:23, Ben Ward <[email protected]> wrote:
> For the toy example you are right, parametric function would be better - 
> what I'm actually trying to do is define several `searchsortfirst` 
> functions, where the lt function is different. 
>
> This is because I have a vector of one immutable composite type of several 
> values I wish to sort and search. I could sort/search them by their first 
> value, second value, or third value, and there is no canonical lt for the 
> type. 
>
> Base.sort can be provided an anonymous function to use as lt - but 
> performance is absolutely critical in my use case and I need to squeeze as 
> much performance as I can. Benchmark's I have done have shown that using 
> anonymous functions slow things down. However, if I copy the searchsorted 
> code and modify it to contain my custom lt condition, then everything is 
> faster.
>
> Therefore I wanted to make a macro that would define several of my custom 
> searchsortedfirst functions: If I provided it a name for the function, the 
> types/arguments is accepts, and then the custom lt expression. It would 
> return a definition of the custom function with the lt condition hard coded 
> in.

Sounds like you should use FastAnonymous.jl or NumericFuns.jl or
Functors:
https://github.com/JuliaLang/julia/blob/e97588db65f590d473e7fbbb127f30c01ea94995/base/functors.jl

>
>
> On Wednesday, June 17, 2015 at 8:07:11 PM UTC+1, Tom Breloff wrote:
>>
>> My gut reaction is that you don't want to use a macro here.  Can you use a 
>> parametric definition:
>> f{T}(vectype:T) = <do something useful with the T>
>>
>> or can you just use multiple dispatch:
>> f{T<:FloatingPoint}(v::Vector{T}) = <something for floats>
>> f{T<:Integer}(v::Vector{T}) = <something for ints>
>>
>> What's your use case?
>>
>> But to answer your question... I think this should work:
>>
>>
>> julia> macro customFun(vectype::Expr, name::Symbol)
>>            quote
>>                function $(esc(name))(v::$(esc(vectype)))
>>                    println(typeof(v))
>>                end
>>            end
>>        end
>>
>> julia> @customFun Vector{Int} f
>> f (generic function with 1 method)
>>
>> julia> f(Int[])
>> Array{Int64,1}
>>
>>
>>
>>
>> On Wednesday, June 17, 2015 at 2:13:41 PM UTC-4, Ben Ward wrote:
>>>
>>> Hi, I want to create a macro with which I can create a function with a 
>>> custom bit of code:
>>>
>>> In the repl I can do a toy example:
>>>
>>>
>>> *name = :hi*
>>>
>>>
>>> *vectype = Vector{Int}*
>>>
>>>
>>> *quote**  function ($name)(v::$vectype)*
>>>
>>>     *println("hi")*
>>>
>>>   *end*
>>>
>>> *end*
>>>
>>> However if I try to put this in a macro and use it I get an error:
>>>
>>> *macro customFun(vectype::DataType, name::Symbol)*
>>>
>>>
>>> *  quote**   function ($name)(v::$vectype)*
>>>
>>> *      println("hi World!")*
>>>
>>> *    end*
>>>
>>> *  end*
>>>
>>>
>>>
>>> *end*
>>>
>>> *@customFun(Vector{Int}, :hi)*
>>>
>>> What am I doing wrong? I'd like to use macro arguments to provide a 
>>> function's name, and the datatype of the argument. I haven't used macros to 
>>> define functions more complex than simple one liners.
>>>
>>> Thanks,
>>> Ben.
>>>
>>

Reply via email to