给定一个文本文件,当前目录中的“hello.jl”:
" Example hello world program."
function hello()
println("hello, world")
end
你会如何向Julia 1.0.0 REPL显示这个?
这是我到目前为止:
julia> disp(f) = for line in readlines(open(f)); println(line); end
disp (generic function with 1 method)
julia> disp("hello.jl")
" Example hello world program."
function hello()
println("hello, world")
end
在朱莉娅有没有内置命令来做到这一点?
在朱莉娅REPL中,命中
;
然后,进入REPL的内置shell模式
shell> head path/to/my/filename
你可以使用run
函数并在Linux中传递Cmd
参数来运行cat
系统命令。
键入分号;
以更改为shell模式:
shell> cat hello.jl
"Example hello world program."
function hello()
println("hello, world")
end
使用run
函数执行Julia之外的命令:
julia> run(`cat hello.jl`) # Only works on Unix like systems.
"Example hello world program."
function hello()
println("hello, world")
end
Process(`cat hello.jl`, ProcessExited(0))
在Windows中,type
命令应该类似于Unix cat
:
julia> show_file(path::AbstractString) = run(@static Sys.isunix() ? `cat $path` : `type $path`)
show_file (generic function with 1 method)
run
返回Process
对象:
julia> show_file("hello.jl")
"Example hello world program."
function hello()
println("hello, world")
end
Process(`cat hello.jl`, ProcessExited(0))
在行的末尾使用分号;
来抑制REPL中的返回输出:
julia> show_file("hello.jl");
"Example hello world program."
function hello()
println("hello, world")
end
或者如果你愿意,你可以在nothing
结束时返回show_file
。
println(String(read("hello.jl")))
要么
"hello.jl" |> read |> String |> println