I’d love to move to Julia, but MATLAB/Octave has a feature that is indispensable to me: the echo of each instruction ( unless terminated with a ; )
ax = -0.8
ay = 0.6
bx = 0.7241 etc.
Is there an equivalent way, in Julia, like an echo mode, to get the same result?
Thanks in advance.
Usually you just put @show in front of each line where you want to echo the output.
e.g. running a script
a = 3
b = 4
@show c = sqrt(a^2 + b^2)
@show d = c + 1
x = [a,b,c,d]
prints
c = sqrt(a ^ 2 + b ^ 2) = 5.0
d = c + 1 = 6.0
3 Likes
There’s a thread on StackOverflow on this exact topic.
macro echo(code)
for i in eachindex(code.args)
typeof(code.args[i]) == LineNumberNode && continue
code.args[i] = :( display($(esc(code.args[i]))) )
end
return code
end
julia> @echo begin
ax = 2
ay = 3
b = ax * ay
end
2
3
6
Credit: Przemyslaw Szufel
I modified it a bit:
macro echo(code)
for i in eachindex(code.args)
typeof(code.args[i]) == LineNumberNode && continue
code.args[i] = esc(:(@show $(code.args[i])))
end
return code
end
julia> @echo begin
ax = 2
ay = 3
b = ax * ay
end
ax = 2 = 2
ay = 3 = 3
b = ax * ay = 6
6
See which one you prefer.
That being said, most people in Julia use other workflow styles. See also the thread: Best practise: organising code in Julia - #2 by stevengj
If you want to work quasi-interactively and see lots of intermediate outputs, one alternative is to use a Jupyter notebook (IJulia.jl) or Pluto notebook (Pluto.jl) — every code block in its own cell gets a displayed output saved in the notebook, and you can easily go back and edit and view intermediate steps without necessarily re-running the whole thing (though Pluto automatically re-runs all downstream calculations).
10 Likes
If you use Visual Studio Code as your Julia IDE, it has a command to send one line (or block) at a time to the REPL and display the output. It can also display the output in an overlay next to the line itself. You can set one or both of these command to a keyboard shortcut of your choosing and then step through a file by repeatedly pressing it to see each result.
4 Likes