-
-
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
RFC: Add a fallback method for sort, plus one for strings #16853
Conversation
@StefanKarpinski You seem to be the supreme overlord of strings, what do you think of being able to sort them? |
I'm a little bit iffy on it, although I suppose that's the logical conclusion of treating them as containers of characters. Since with the other change here, you can just write |
Yeah, I had started this PR idea as just a general fallback method but then I realized that when given a string it returned an array of characters. I figured string in -> string out made more sense. If you're too iffy about it, strings could be a |
I dunno. It seems somewhat silly but harmless. |
Mostly I just wanted to be able to sort things like |
## other iterables and fallback ## | ||
|
||
sort(s::String; kws...) = String(sort(collect(s); kws...)) | ||
sort(n::Number) = throw(MethodError("no method matching sort(::Number)")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MethodError()
takes the function and tuple of arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JeffBezanson Oh neat, I didn't realize that. I'll do it that way. Thanks!
sort(n::Number) = throw(MethodError(sort, n)) | ||
sort(itr; kws...) = sort(collect(itr); kws...) | ||
sort(itr; kws...) = sort!(collect(itr); kws...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope collect
never aliases. x-ref discussion in #15546
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tkelman Probably safer to use not-in-place sort then, yeah?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured collect
always made a new object so it would be safe to modify that in place and avoid an extra copy. I didn't realize it was possible for collect
to alias.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It hopefully isn't possible for collect
to alias, but that's a difficult assumption to verify since people can write their own collect
methods - if they write one that aliases, who is at fault there, the author of the aliasing method or downstream callers who assumed it wouldn't?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I have made sort
no longer yell (i.e. sort!
-> sort
).
Comment by Scott Jones from https://groups.google.com/d/topic/julia-dev/ietfsSAIwnI/discussion
|
Yeah, I'd be fine with sorting a string being an error. |
Is |
I agree that special-casing strings doesn't seem worth it. |
Ah right the specific case of |
@tkelman It's not that it's onerous to type |
Unless there's someone who's really into this idea, I'll go ahead and close this PR |
Doing |
@StefanKarpinski Yeah, I agree; I do think it would be nice to have a catchall for sorting iterables of various types, like |
What else could you get? |
Uhhhhhhhhh ¯\_(ツ)_/¯ Good point. So should I reopen this? Does my implementation seem like a reasonable way to do this? (The string thing aside for the moment.) |
Well, I suppose it depends on what you're sorting. For example, if I wanted to sort a tuple, say Edit: I guess that could be another thing that's special cased, e.g. sort(t::Tuple) = (sort(collect(t))...) |
I would favor this possibility to sort general iterables, but without special cases: this would return an array even for tuples and strings as input. This is more predicable, and more useful sometimes, when the user wants to mutate the result. In the end, it seems that modifying the |
Sorting a string returns a string, sorting a number is a
MethodError
, and sorting other iterablescollect
s them first and returns an array. I separated numbers into a separate call because otherwise theMethodError
text isn't so useful.