Thanks for your proposals. They are ok and shorter. Feels all of our approaches are a bit "hackish" but maybe I just have to get used to it...
The overall picture is that in a macro where the expr coming in is a kind of DSL I need to translate it to a large number of julia functions. In the end I get them back as a dict or array of individual expr and then need to return a single expr back from the macro. The example I give was just a cleaned up and small part of it. If you know of any generic ways to rather transform/translate the incoming Expr directly rather than having to create individual quoted new expr for the parts and merging them back that would be interesting. I remember seeing something on the mailing list a few months back but can't locate it anymore. Maybe something like Match.jl can be applied to match/transform Expr object ( http://matchjl.readthedocs.org/en/latest/ ) but I think there was something more adapted to Expr's... Regards, Robert On Wed, Jun 4, 2014 at 1:33 PM, Ivar Nesje <[email protected]> wrote: > @mauro I think there is a single function that combines map and reduce > <http://docs.julialang.org/en/release-0.2/stdlib/base/#Base.mapreduce>. > Also it seems like parse(string(ex_funcs...)) would do the > same job. > > What about instead enclosing them in a block expression? I'm not sure I > understand what you try to do, but this seems to work: > > *eval(Expr(:block, ex_funcs[1:2]...))* > > *eval(Expr(:block, ex_funcs...))* > Ivar > > > kl. 13:14:09 UTC+2 onsdag 4. juni 2014 skrev Mauro følgende: >> >> This works: >> parse(reduce(*, map(string, ex_funcs))) >> >> as was suggested in this thread for symbols: >> https://groups.google.com/d/msg/julia-dev/KJNOrV45sZo/zE2_fVzQrS4J >> >> Still seems a bit ugly. Any better ways? >> >> On Wed, 2014-06-04 at 08:58, [email protected] wrote: >> > In a macro I need to first define a large number of functions and then >> > merge them into one block and return that as the code from the macro. >> Is >> > there a simpler/smarter way than what I currently do, which is based on >> > explicitly adding >> > >> > using Base.Test >> > >> > # We want to find a way to merge the definition of two (or more) quoted >> > function >> > # bodies into one quoted expression. >> > >> > ex_a = quote >> > function a() >> > b() >> > end >> > end >> > >> > ex_b = quote >> > function b() >> > 2 >> > end >> > end >> > >> > ex_c = quote >> > function b() >> > c() >> > end >> > function c() >> > 3 >> > end >> > end >> > >> > ex_funcs = [ex_a, ex_b, ex_c] >> > >> > function concat_expr_blocks(a, b) >> > args = a.args >> > for barg in b.args >> > push!(a.args, barg) >> > end >> > a >> > end >> > >> > function concat_expr_blocks(blocks) >> > ex = blocks[1] >> > for i in 2:length(blocks) >> > ex = concat_expr_blocks(ex, blocks[i]) >> > end >> > ex >> > end >> > >> > eval(concat_expr_blocks(ex_funcs[1:2])) >> > @test a() == 2 >> > eval(concat_expr_blocks(ex_funcs)) >> > @test a() == 3 >> > >> > Thanks for any pointers. I'm sure there is an obvious way but I can't >> see >> > it right now... ;) >> > >> > Regards, >> > >> > Robert >> >> -- >> >> -- Best regards, /Robert Feldt -- Tech. Dr. (PhD), Professor of Software Engineering Blekinge Institute of Technology, Software Engineering Research Lab, and Chalmers, Software Engineering Dept Explanea.com - Igniting your Software innovation robert.feldt (a) bth.se or robert.feldt (a) chalmers.se or robert.feldt (a) gmail.com Mobile phone: +46 (0) 733 580 580 http://www.robertfeldt.net <http://www.cse.chalmers.se/~feldt>
