-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set Printing #7153
Comments
Yes. Although it shouldn't be |
I'm tempted to write something very similar to the matrix output: import Base: writemime, with_output_limit, print_matrix
function writemime(io::IO, ::MIME"text/plain", S::Set)
print(io, summary(S))
if !isempty(S)
println(io, ":")
with_output_limit(()->print_matrix(io, sort!([x for x in S])))
end
end This produces nice-looking, sorted, truncated output. It is |
If we are worried about the overhead of making a copy of import Base: writemime, with_output_limit, print_matrix
function writemime{T}(io::IO, ::MIME"text/plain", S::Set{T})
print(io, summary(S))
if !isempty(S)
println(io, ":")
if length(S) > 10^7
A = copy!(Array(T, 1000), 1, S, 1, 1000)
else
A = sort!(T[x for x in S])
end
with_output_limit(()->print_matrix(io, A))
end
end |
Though I guess you would only want to |
Surely it's not canonical to sort the set prior to printing it? It might confuse people thinking the set is actually sorted. Best and easiest just to pump it out as it comes. |
The set elements are unordered, so it is equally valid to print them in any order we want. So why not print it in an order where it is much easier to see what is in the set. Quiz: with which ordering is it easier to tell what is in the following set:
or
? Especially if we are going to print only a subset of the set, it is much more useful to print the largest and smallest elements (as in my code above) than it is to print some random selection of elements. |
Fail point it would be easier, but you will get people thinking the set is naturally sorted. I suppose that's just the way it is. I suppose it just needs to be clear in the docs what a set is and isn't. |
How do we make this happen? Shall I submit a pull request? |
Sure. |
This seems fixed? Or am I misunderstanding what the issue is? Set(map(x->x^2,1:4:356))
Set([8649,31329,47089,1369,2401,108241,289,27225,43681,625 … 50625,66049,11881,83521,119025,48841,11025,93025,841,34225]) |
The output still doesn't appear to be ordered correctly. |
I figured they weren't supposed to be ordered, as you argued for yourself. In that case, more work is needed. Is there an easy way to see which |
you're right I argued order doesn't matter but as @stevengj said order makes checking for the presence of a value visually much much easier. |
Okay. The question remains, how will you sort Set(Any[linspace(1,4,240), 3, rand(20,30), rand(3), "a", 'a']) ? :) (which by the way looks a lot better than |
@pkofod, since there is no |
Okay, so if method_exists(isless, (T, T))
show a sorted version
else
show unsorted version
end That makes sense. What is the easiest way to find the |
cross-link with #14052 |
Didn't mean to spam this issue - I jacked up the commit message and did not know it would link before creating the pull request. |
I think the problem with sorting is that a set has to iterate its elements in some order, so when we print it we should show what that order is. |
As noted above by #7153 (comment), this is fixed: julia> Set(1:1000)
Set([306, 29, 74, 905, 176, 892, 285, 318, 873, 975 … 632, 618, 341, 186, 321, 420, 6, 856, 322, 218]) Also, consensus (by bureaucratic delay) seems to be against adding ordering to the printing. |
Currently if you display a Set the entire thing is slammed to STDOUT, if it's very long it can take ages/fill up the shell.
Shouldn't Sets have a pretty show function that chops out some elements same as arrays?
The text was updated successfully, but these errors were encountered: