Awesome, thanks!
On Friday, July 3, 2015 at 10:05:52 PM UTC-5, Yichao Yu wrote: > > On Fri, Jul 3, 2015 at 11:04 PM, Yichao Yu <[email protected] <javascript:>> > wrote: > > On Fri, Jul 3, 2015 at 8:37 PM, Kevin Owens <[email protected] > <javascript:>> wrote: > >> Yes, you're right, that won't work. > >> > >> I got to thinking about this from this part of the DataFrames > documentation: > >> > http://dataframesjl.readthedocs.org/en/latest/getting_started.html#the-dataframe-type > > >> > >> df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"]) > >> > >> and this code > >> > >> df = DataFrame() > >> df[:A] = 1:8 > >> df[:B] = ["M", "F", "F", "M", "F", "M", "M", "F"] > >> df > >> > >> If I already had arrays A and B (or an arbitrary number of them) it > would be > >> nice to be able to do > >> > >> df = DataFrame(A, B) > >> > >> and then df would have column names :A and :B. > >> > > > > And that's exactly what I guessed what you wanted (because I've done > > sth similar in python sometime ago :P) > > > > The right way to do this is to specify the name of the variable > > because a variable is always bound to a single object while a object > > can be bound to multiple varialbles. > > > > The way I did it in python was using eval. However, in Julia, the eval > > function is working in the global (module) scope (otherwise the > > compiler cannot reason anything about local variables) so you cannot > > use it to get the value of a variable. One way to do this in julia is > > using macros > > > > What you want is basically to write sth like: > > > > ``` > > <some magic>(A, B) > > ``` > > > > to achieve the effect of > > > > ``` > > DataFrame(A=A, B=B) > > ``` > > > > Well, this is a simple code transformation. (untested code below) > > Not untested anymore.... > > ``` > julia> macro dataframe(args...) > :(DataFrame($([Expr(:kw, arg, arg) for arg in args]...))) > end > > julia> macroexpand(:(@dataframe(A, B))) > :(DataFrame(A=A,B=B)) > > julia> Meta.show_sexpr(macroexpand(:(@dataframe(A, B)))) > (:call, :DataFrame, (:kw, :A, :A), (:kw, :B, :B)) > julia> Meta.show_sexpr(:(DataFrame(A=A, B=B))) > (:call, :DataFrame, (:kw, :A, :A), (:kw, :B, :B)) > ``` > > > > > ``` > > macro dataframe(args...) > > :(DataFrame($([Expr(:kw, arg, arg) for arg in args]...))) > > end > > ``` > > > > If you would like to figure out how to write a macro, the general > > procedure is to see the input and output AST with `Meta.show_sexpr` > > (which is better than `show` for this because some symbols are parsed > > differently in different context) and then write a program to do the > > transformation. > > > >> > >> > >> On Friday, July 3, 2015 at 7:24:55 PM UTC-5, Yichao Yu wrote: > >>> > >>> On Fri, Jul 3, 2015 at 8:21 PM, Kevin Owens <[email protected]> > wrote: > >>> > Gah, nevermind: > >>> > > >>> > function as_symbol(x) > >>> > :(print(x)).args[2] > >>> > end > >>> > >>> This is almost certainly not what you want unless you just want a > >>> function that returns `:x`, in which case, you would be better off to > >>> just return that. > >>> > >>> Can you elaborate on what exactly you would like to do? Depending on > >>> what you really want, there are different ways to implement. > >>> > >>> > > >>> > > >>> > (using print() so that it works regardless of the type) > >>> > > >>> > > >>> > On Friday, July 3, 2015 at 7:16:38 PM UTC-5, Kevin Owens wrote: > >>> >> > >>> >> Say you have an array > >>> >> > >>> >> x = rand(5) > >>> >> > >>> >> or just any variable > >>> >> > >>> >> y = "abc" > >>> >> > >>> >> How would I write a function that I would call like foo(x) and > would > >>> >> return the symbol :x? >
