What is going on here?
julia> struct A
x::Int
end
julia> Base.show(io::IO, a::A) = print(io, "$(a.x)")
julia> v = [A(1), A(2)];
julia> print.(v)
122-element Vector{Nothing}:
nothing
nothing
this is a fresh Julia section.
What is going on here?
julia> struct A
x::Int
end
julia> Base.show(io::IO, a::A) = print(io, "$(a.x)")
julia> v = [A(1), A(2)];
julia> print.(v)
122-element Vector{Nothing}:
nothing
nothing
this is a fresh Julia section.
Or even simpler:
julia> struct A
x::Int
end
julia> v = [A(1), A(2)]
2-element Vector{A}:
A(1)
A(2)
julia> println.(v)
A(1)
A(2)
2-element Vector{Nothing}:
nothing
nothing
Did this always happened?
println
returns nothing
; broadcasting collects the two nothing
s in an array.
you printed 1
then 2
then Julia REPL shows you the [nothing, nothing]
as
2-element Vector{Nothing}:
nothing
nothing
If you want an array of strings, then you need to call a function that returns a string, e.g.
julia> repr.(v)
2-element Vector{String}:
"1"
"2"
Or if you just want the println
output, then put a semicolon after the call to suppress the display of the [nothing, nothing]
return value:
julia> println.(v);
1
2
Thanks for the clarifications, to all.
I’m surprised I didn’t notice this before. This came in the following context. I have a vector of custom structs, but now I notice this is irrelevant, because even with simple arrays it is the same:
If I have an array, when it is displayed, it is adjusted to the screen height:
julia> x
1000-element Vector{Float64}:
0.379186956550142
0.29159820914033496
â‹®
0.20866068645567148
0.2915546149517577
julia>
Now I want to see all the elements (by scrolling the screen, of course). So why not:
julia> println.(x)
And this does print everything, but followed by the array of nothing
s.
So, what is the pleasant way to just display all the elements of an array, in one line? Adding the ;
afterwards, as suggested by @stevengj, or is there another function that people use that does not collect that array of nothings at the end?
(by the way, any more
, less
, type of functions for arrays implemented somewhere?)
How about
julia> printall(x) = for i in x print(i, " "); end
julia> x = rand(10);
julia> printall(x)
0.9225340578326212 0.07352896862906577 0.6508288404547642 0.23723598173792448 0.46214011307549796 0.12091754019493772 0.8345915278753898 0.025769195692800673 0.2621634432952713 0.06305239378926508
Use println(i)
instead of print(i, " ")
if you want one element per line.
Oh, no, I just want to know the most convenient way to do it on the REPL eventually. println.(x);
is fine. Better yet if some package provides sort of more
and less
functions that allow some interactivity in the array display.
julia> x = [1, 2, 3];
julia> foreach(println, x)
1
2
3
that’s what I do and yes I hate how many times I have to do it every day
show(stdout, "text/plain", x)
works. Or you can do println(x)
, though that shows them horizontally.
If you do
julia> using TerminalPager
julia> rand(5000) |> pager
You will get this screen, in which you can scroll up and down, much a-la less!
Off-topic: TerminalPager.jl was once called Less.jl
Ah! So I am not crazy! I was looking for it with that name!
We decided to rename it because less
is a “joke” with more
and it is more restricted to *nix uses.
Using TerminalPager.jl, you can type |
to enter in pager mode. In this case, all output that is larger than the screen will be rendered inside the pager.
sadly I often needs this from Juputer
Maybe is time for a HtmlPager