http://stackoverflow.com/questions/35051773/create-local-variables-programmatically-from-a-dictionary-in-julia
On Thu, Jan 28, 2016 at 8:10 AM, Stefan Karpinski <[email protected]> wrote: > https://groups.google.com/forum/#!topic/julia-users/axwQqFeIlCQ > > On Wed, Jan 27, 2016 at 11:05 PM, Yichao Yu <[email protected]> wrote: > >> On Wed, Jan 27, 2016 at 9:47 PM, Fady Shoukry <[email protected]> >> wrote: >> > >> >> >> >> Depending on what API JuMP provide and what API you want to provide, I >> >> think you should either keep the dict (if JuMP can handle it) or use >> >> meta programing to construct an AST/function based on the user input >> >> and evaluate that. >> > >> > >> > Thanks for the quick reply. Could you elaborate on that last part >> regarding >> > constructing an AST/function (with an example if possible)? >> > >> >> A simple example that hopefully shows what I meant, >> >> >> ``` >> julia> vars = [:a, :b] >> 2-element Array{Symbol,1}: >> :a >> :b >> >> julia> expr = :(a + b) >> :(a + b) >> >> julia> function make_function(name, vars, expr) >> :(function $name() >> $([:($v = 1) for v in vars]...) >> $expr >> end) >> end >> make_function (generic function with 1 method) >> >> julia> func_ast = make_function(:new_function, vars, expr) >> :(function new_function() # none, line 3: >> a = 1 >> b = 1 # none, line 4: >> a + b >> end) >> >> julia> eval(func_ast) >> new_function (generic function with 1 method) >> >> julia> new_function() >> 2 >> >> julia> @code_warntype new_function() >> Variables: >> a::Int64 >> b::Int64 >> >> Body: >> begin # none, line 3: >> a = 1 >> b = 1 # none, line 4: >> return (Base.box)(Int64,(Base.add_int)(a::Int64,b::Int64)) >> end::Int64 >> ``` >> > >
