Home > Back-end >  Is there a way to display every line of code in Julia when it is not suppressed with ";" ,
Is there a way to display every line of code in Julia when it is not suppressed with ";" ,

Time:11-29

Say that I am running a Julia script. I would like every line of code to be displayed in the terminal like it is in MATLAB. Is there any way in which can do that? It is clunky for me to write display(...) for every variable that I want to see in the terminal, especially when I want to check my work quickly.

For intance, say that I have this code:

a = [1; 0; 0]
b = [0; 1; 0]
c = [0; 0; 1]
a * transpose(a)
b * transpose(b)
c * transpose(c)

I would like all six of these lines to be automatically displayed in the terminal, instead of having to write, say:

a = [1; 0; 0]
b = [0; 1; 0]
c = [0; 0; 1]
display(a)
display(b)
display(c)
display(a * transpose(a))
display(b * transpose(b))
display(c * transpose(c))

Thank you in advance.

CodePudding user response:

One way to handle that is to write your own macro:

macro displayall(code)
    for i in eachindex(code.args)
        typeof(code.args[i]) == LineNumberNode && continue
        code.args[i] = quote
            display($(esc(code.args[i])));
        end
    end
    return code
end

Now you can use it such as:

julia> @displayall begin
       a = [1; 0; 0]
       b = [0; 1; 0]
       c = [0; 0; 1]
       a * transpose(a)
       b * transpose(b)
       c * transpose(c)
       end
3-element Vector{Int64}:
 1
 0
 0
3-element Vector{Int64}:
 0
 1
 0
3-element Vector{Int64}:
 0
 0
 1
3×3 Matrix{Int64}:
 1  0  0
 0  0  0
 0  0  0
3×3 Matrix{Int64}:
 0  0  0
 0  1  0
 0  0  0
3×3 Matrix{Int64}:
 0  0  0
 0  0  0
 0  0  1
  • Related