2015-07-09 5 views
7

निर्यात करता है, मैं स्वचालित रूप से कुछ फ़ंक्शन उत्पन्न करना चाहता हूं और उन्हें स्वचालित रूप से निर्यात करना चाहता हूं। कुछ ठोस उदाहरण रखने के लिए, मान लें कि मैं एक मॉड्यूल बनाना चाहता हूं, जो सिग्नल लेने वाले कार्यों को प्रदान करता है और इसमें एक औसत औसत/अधिकतम/न्यूनतम/औसत ... लागू होता है।जूलिया स्वचालित रूप से कार्यों को उत्पन्न करता है और उन्हें

कोड पीढ़ी पहले से ही काम करता है:

for fun in (:maximum, :minimum, :median, :mean) 
    fname = symbol("$(fun)filter") 
    @eval ($fname)(signal, windowsize) = windowfilter(signal, $fun, windowsize) 
end 

देते मुझे

maximumfilter 
minimumfilter 
... 

काम करता है लेकिन यह कैसे मैं उन्हें स्वचालित रूप से निर्यात कर सकता हूं? जैसे मैं ऊपर दिए गए लूप जैसे

export $(fname) 

और प्रत्येक फ़ंक्शन को सृजन के बाद निर्यात किया गया है।

+3

क्या 'eval (expr (: export, fname)) 'काम करता है? मैं 'सिम्पी' में ऐसा कुछ उपयोग करता हूं। यकीन नहीं है कि यह सबसे अच्छा तरीका है हालांकि। – jverzani

+0

धन्यवाद, यह मेरे लिए काम करता है! –

उत्तर

5

आप मैक्रो का उपयोग पर विचार कर सकते:

module filtersExample 

macro addfilters(funs::Symbol...) 
    e = quote end # start out with a blank quoted expression 
    for fun in funs 
    fname = symbol("$(fun)filter") # create your function name 

    # this next part creates another quoted expression, which are just the 2 statements 
    # we want to add for this function... the export call and the function definition 
    # note: wrap the variable in "esc" when you want to use a value from macro scope. 
    #  If you forget the esc, it will look for a variable named "maximumfilter" in the 
    #  calling scope, which will probably give an error (or worse, will be totally wrong 
    #  and reference the wrong thing) 
    blk = quote 
     export $(esc(fname)) 
     $(esc(fname))(signal, windowsize) = windowfilter(signal, $(esc(fun)), windowsize) 
    end 

    # an "Expr" object is just a tree... do "dump(e)" or "dump(blk)" to see it 
    # the "args" of the blk expression are the export and method definition... we can 
    # just append the vector to the end of the "e" args 
    append!(e.args, blk.args) 
    end 

    # macros return expression objects that get evaluated in the caller's scope 
    e 
end 

windowfilter(signal, fun, windowsize) = println("called from $fun: $signal $windowsize") 

# now when I write this: 
@addfilters maximum minimum 

# it is equivalent to writing: 
# export maximumfilter 
# maximumfilter(signal, windowsize) = windowfilter(signal, maximum, windowsize) 
# export minimumfilter 
# minimumfilter(signal, windowsize) = windowfilter(signal, minimum, windowsize) 

end 

जब आप इसे लोड, तो आप देखेंगे कार्यों स्वचालित रूप से निर्यात किया जाता है:

julia> using filtersExample 

julia> maximumfilter(1,2) 
called from maximum: 1 2 

julia> minimumfilter(1,2) 
called from minimum: 1 2 

अधिक जानकारी के लिए the manual देखें।

+0

मेरे लिए यह काम नहीं करता है, मुझे लाइन निर्यात $ (esc (fname)) पर "वाक्यविन्यास: अतिरिक्त टोकन" ("अभिव्यक्ति के अंत के बाद" मिला है। –

+0

क्या आप 0.3 पर हैं? मैं 0.4 विकास संस्करण का उपयोग कर रहा हूं , इसलिए यह संभव है कि संभवतः 0.3 के बाद से वाक्यविन्यास बदल गया है। मेरे पास 0.3 स्थापित नहीं है ... क्या कोई और मदद कर सकता है? –

+0

आह आप सही हैं, मैं वास्तव में 0.3 का उपयोग करता हूं। –

संबंधित मुद्दे