On Sat, Dec 26, 2015 at 2:03 PM, Stuart Brorson <[email protected]> wrote:
> Julia users,
>
> I'm fiddling around with Julia's strings & metaprogramming.  I am
> constructing a function by concatenating a bunch of strings together
> to create my function, like this:
>
> function return_fcn(N)
>   P = string("function f$N(X::Vector)\n")
>   for i in 1:N
>     P = string(P, "x$i = X[$i];\n");
>   end
>   P = string(P, "mu = X[$(N+1)];\n")
>   etc....
>
> When I execute this code, I get:
>
> julia> y = return_fcn(2)
> "function f2(X::Vector)\nx1 = X[1];\nx2 = X[2];\nmu = X[3];\n"
>
> However, what I really want to see is
>
> function f2(X::Vector)
> x1 = X[1];
> x2 = X[2];
> mu = X[3];
>
> "show(y)" doesn't seem to do what I want.  Later, when I do
>
> eval(parse(y))
>
> then I get a function which executes correctly.  My problem is simply
> that I can't get Julia to give me a string I can read easily.  This
> will be a very big issue for me when N -> 1024, 2048, etc....
>
> Questions:
>
> 1.  How can I escape the \n to get a real <CR><LF> in my displayed
> string?


\n is the result of the escape. Just print the string to see actual new lines

>
> 2.  Is this the optimal way to construct a program for later execution
> (i.e. metaprogramming)?

No. Construct the AST directly. The metaprogramming section of the doc
already covers this.

>
> Thanks for all wisdom you have to offer.
>
> Stuart

Reply via email to