Sum*_*mer 7 read-eval-print-loop julia
我已经在 REPL 中输出了一些内容。是否有任何功能可以将所有这些内容打印到文件中?
如果这些输出已经在 REPL 中打印,我想将它们保存到文件的唯一方法是手动复制粘贴。但是如果您想保存 REPL 输出历史记录以供将来使用,一种方法是重载display
:
shell> touch repl_history.txt\n\njulia> using REPL\n\njulia> function REPL.display(d::REPL.REPLDisplay, mime::MIME"text/plain", x)\n io = REPL.outstream(d.repl)\n get(io, :color, false) && write(io, REPL.answer_color(d.repl))\n if isdefined(d.repl, :options) && isdefined(d.repl.options, :iocontext)\n # this can override the :limit property set initially\n io = foldl(IOContext, d.repl.options.iocontext,\n init=IOContext(io, :limit => true, :module => Main))\n end\n show(io, mime, x)\n println(io)\n open("repl_history.txt", "a") do f\n show(f, mime, x)\n println(f)\n end\n nothing\n end\n
Run Code Online (Sandbox Code Playgroud)\n\n然后,让我们在 REPL 中随机打印一些内容:
\n\njulia> rand(10)\n10-element Array{Float64,1}:\n 0.37537591915616497\n 0.9478991508737484 \n 0.32628512501942475\n 0.8888960925262224 \n 0.9967927432272801 \n 0.4910769590205608 \n 0.7624517049991089 \n 0.26310423494973545\n 0.5117608425961135 \n 0.0762255311602309 \n\nhelp?> gcd\nsearch: gcd gcdx significand\n\n gcd(x,y)\n\n Greatest common (positive) divisor (or zero if x and y are both zero).\n\n Examples\n \xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\n\n julia> gcd(6,9)\n 3\n\n julia> gcd(6,-9)\n 3\n
Run Code Online (Sandbox Code Playgroud)\n\n文件内容如下:
\n\nshell> cat repl_history.txt\n10-element Array{Float64,1}:\n 0.37537591915616497\n 0.9478991508737484 \n 0.32628512501942475\n 0.8888960925262224 \n 0.9967927432272801 \n 0.4910769590205608 \n 0.7624517049991089 \n 0.26310423494973545\n 0.5117608425961135 \n 0.0762255311602309 \n gcd(x,y)\n\n Greatest common (positive) divisor (or zero if x and y are both zero).\n\n Examples\n \xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\n\n julia> gcd(6,9)\n 3\n\n julia> gcd(6,-9)\n 3\n
Run Code Online (Sandbox Code Playgroud)\n\n如果不需要交互地使用 REPL,只需使用julia script.jl > output.txt
也可以达到目的。