Julia 1.7.2 : how to use #sprintf - julia

I am new to julia 1.7.2. I found on julialang.org about #sprintf.
I try to show a number 45 as: 00045
a = #sprintf "%05i" 45 # I get: #sprintf not defined
a = Printf.#sprintf "%05i" 45 # I get: Printf not defined
What is missing in this 1 line program? Thanks

You need to import the Printf module first. For example with using (in which case you do not need to prefix #sprintf with Printf):
julia> using Printf
julia> #sprintf "%05i" 45
"00045"

Related

what is the solution to getting error on julia pluto notebook after entering the "Struct"

syntax: invalid type signature around C:\Users\admin.julia\pluto_notebooks\Revolutionary blueprint.jl#==#396279cf-6e6b-468e-8aa6-772e2279058d:1
enter image description here
enter image description here
You cannot use a dash as part of a variable name, as it is the subtraction operator:
julia> x = 5
5
julia> y = 2
2
julia> x-y
3
So there's no way to distinguish x minus y from a variable called "x-y". Use underscores instead:
julia> struct My_Struct end
julia> struct My-Struct end
ERROR: syntax: invalid type signature around REPL[47]:1
Stacktrace:
[1] top-level scope
# REPL[47]:1

How to interpolate multiple variables in a Markdown string in Julia

In the following Julia 1.5 code:
a, b = 4, 5
"a=$(a), b=($b)" # "a=4, b=5"
using Markdown
md"a=$(a), b=($b)" # a=a, b=b
# but...
Markdown.parse("a=$(a), b=($b)") # "a=4, b=5"
It seems that the Markdown macro thinks two $ indicate a math expression. But the parse handles it OK.
Can someone explain this? is there a way to use the md"..." form for this.
It's not obvious in my opinion, but I think $ with a non-space before is interpreted as a closing LaTeX $ if there is one before.
Some suggestions:
If you're OK with spaces around your = sign, then this works:
julia> md"a = $a, b = $b"
a = 4, b = 5
Or you could make it a list:
julia> md"""
- a=$a
- b=$b
"""
• a=4
• b=5

Is there a function for printing the REPL content to a file in Julia language?

I already have output some content in the REPL. Is there any function to print all this content to a file?
If those outputs have already been printed in the REPL, I guess the only way to save them to a file is copy-pasting manually. But if you would like to save the REPL output history for future use, one way is to overload display:
shell> touch repl_history.txt
julia> using REPL
julia> function REPL.display(d::REPL.REPLDisplay, mime::MIME"text/plain", x)
io = REPL.outstream(d.repl)
get(io, :color, false) && write(io, REPL.answer_color(d.repl))
if isdefined(d.repl, :options) && isdefined(d.repl.options, :iocontext)
# this can override the :limit property set initially
io = foldl(IOContext, d.repl.options.iocontext,
init=IOContext(io, :limit => true, :module => Main))
end
show(io, mime, x)
println(io)
open("repl_history.txt", "a") do f
show(f, mime, x)
println(f)
end
nothing
end
then, let's print something random in the REPL:
julia> rand(10)
10-element Array{Float64,1}:
0.37537591915616497
0.9478991508737484
0.32628512501942475
0.8888960925262224
0.9967927432272801
0.4910769590205608
0.7624517049991089
0.26310423494973545
0.5117608425961135
0.0762255311602309
help?> gcd
search: gcd gcdx significand
gcd(x,y)
Greatest common (positive) divisor (or zero if x and y are both zero).
Examples
≡≡≡≡≡≡≡≡≡≡
julia> gcd(6,9)
3
julia> gcd(6,-9)
3
And here is what the file content looks like:
shell> cat repl_history.txt
10-element Array{Float64,1}:
0.37537591915616497
0.9478991508737484
0.32628512501942475
0.8888960925262224
0.9967927432272801
0.4910769590205608
0.7624517049991089
0.26310423494973545
0.5117608425961135
0.0762255311602309
gcd(x,y)
Greatest common (positive) divisor (or zero if x and y are both zero).
Examples
≡≡≡≡≡≡≡≡≡≡
julia> gcd(6,9)
3
julia> gcd(6,-9)
3
If there is no need to work with REPL interactively, simply use julia script.jl > output.txt might also do the trick.
What you've printed isn't saved anywhere, so no, there's no way to do that. It may be that there's something easy to do but without more details it's impossible to way really.
If you want to save variables to file, you can you JLD2 package. Then you can save each variable as below:
using JLD2, FileIO
hello = "world"
foo = :bar
#save "example.jld2" hello foo

What does assignment like x\y = 1 mean in Julia?

While experimenting with Julia 1.0, I've noticed that I can do something like this:
x\y = 1
The REPL then shows:
\ (generic function with 1 method)
which means its a valid assignment (the interpreter doesn't complain). However, x, y, and x\y all remain undefined.
What is the meaning of such expression?
It is a new function definition that (kind of) shadows the left division operator \ in Base, since the left division operator is already defined for some types in Julia. The new function definition is \(x,y) = 1 (the names of function parameters do not matter) which works for all types of variables. This will prevent julia from loading Base.\ due to name conflict. No matter what the input is your new \ will return the same value.
julia> x\y = 5
julia> a = 3; b = 4;
julia> a\b
5
julia> c = "Lorem ipsum"; d = "dolor";
julia> c\d
5
If you have already used the \ that is defined in Base, your redefinition will throw an error saying that extending Base.\ requires an explicit import with import Base.\. The behavior of defining \ after import Base.\ however will be different. It will extend the operator Base.\.
julia> 1\[1,3]
2-element Array{Float64,1}:
1.0
3.0
julia> import Base.\
julia> x\y=3
\ (generic function with 152 methods)
julia> 1\[1,3]
2-element Array{Int64,1}:
3
3

Change julia promt to include evalutation numbers

When debugging or running julia code in REPL, I usually see error messages showing ... at ./REPL[161]:12 [inlined].... The number 161 means the 161-th evaluation in REPL, I guess. So my question is could we show this number in julia's prompt, i.e. julia [161]> instead of julia>?
One of the advantages of Julia is its ultra flexibility. This is very easy in Julia 0.7 (nightly version).
julia> repl = Base.active_repl.interface.modes[1]
"Prompt(\"julia> \",...)"
julia> repl.prompt = () -> "julia[$(length(repl.hist.history) - repl.hist.start_idx + 1)] >"
#1 (generic function with 1 method)
julia[3] >
julia[3] >2
2
julia[4] >f = () -> error("e")
#3 (generic function with 1 method)
julia[5] >f()
ERROR: e
Stacktrace:
[1] error at .\error.jl:33 [inlined]
[2] (::getfield(, Symbol("##3#4")))() at .\REPL[4]:1
[3] top-level scope
You just need to put the first 2 lines onto your ~/.juliarc and enjoy~
Since there are several changes in the REPL after julia 0.7, these codes do not work in old versions.
EDIT: Well, actually there need a little bit more efforts to make it work in .juliarc.jl. Try this code:
atreplinit() do repl
repl.interface = Base.REPL.setup_interface(repl)
repl = Base.active_repl.interface.modes[1]
repl.prompt = () -> "julia[$(length(repl.hist.history) - repl.hist.start_idx + 1)] >"
end

Resources