-
-
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
Rename shift!
and unshift!
?
#23902
Comments
We could do a wholesale rename of these functions:
|
Or a left/right (start/end? first/last?) keyword if performance issues could be worked out |
Left and right seem a bit ambiguous; perhaps |
I'm not sure that front and back are clearer than left and right. Of course, for left/right terminology it's a bit unfortunate that we print vectors vertically, but front/back aren't better in that respect. |
Oh, and we already use |
I'm not sure either. Perhaps better ideas: |
The names |
Out of the options so far for renaming all four functions, the suffixed versions I have some reservations about renaming |
I think |
I love |
So far, I like the first post's suggestion the best. |
I think it's good to use |
-2 to |
@JeffBezanson C++ vector uses I assume the existing names can be traced back to the |
I prefer proper English words... :) ( |
or at least mention
|
Another idea: Theoretically, this syntax generalizes well to things like |
Related to #23789 |
Cool idea, @TotalVerb! |
Or merge with |
Ah what? Ok this is a really creative idea :-) It does seem pretty odd to use the functions |
I have to say, as cool as I think this idea is – it reminds me of @mbauman's idea to use reduction functions in indexing, as in
I'm largely between 1 and 2 but there's some appeal to 3 and 4 as well. |
I think that 1) it makes a lot of sense that applying the same operation (pushing or popping) to the beginning and end of an array should have at least related names; |
Stefan's option 2 or 4 seem good to me. I don't think |
Proposal:
I have to say that's tempting, especially if we keep |
Imo using |
Especially since our vectors are vertical (so it should be top/bottom) 🙂. Other options might be start/end or first/last. |
left/right seems fine to me; it's consistent with our reduction terminology ( The other nice thing about left/right is that it has a well-established one-character abbreviation. |
Seriously, I don't know which end of a vector is the front or the back. Is the front index 1 or is that the back? Yes, we sometimes print our vectors vertically, but we also print them left-to-right in other cases. We're already consistently using inherited left/right naming for many other functions. |
How about just having new methods for append! and prepend! where the second argument is a scalar? |
I like best the |
It's not possible to define scalar generically. |
"It's not possible to define scalar generically." Ah, I see. You could specify, say, append!( x::Vector{T}, y::T ) , but you really want y to be "something that can be converted to T". But, the type system doesn't (yet?) know what set of types can be converted. Bummer. |
Let's have a (nonbinding, informative) vote:
|
That choice of emoji is somewhat biasing ;). |
And 👍 on OPs suggestion if you like that best ;) |
I have a few thoughts on renaming these functions so I'll just apologize up front for possibly adding some noise into this conversation. So far Julia's names of |
If we were to go in this direction I would suggest we use |
What I like about the original names is that they just use one word. Trying to keep to one word but make some nicer pairings I came up with
We could leave push/pop on the end and move put/pull to the beginning to be less breaking but I think that would leave things somewhat confusing. |
Another alternative is to use |
@phaverty As long as it's possible to have a I'll repost this: #23902 (comment) @omus Renaming
|
Finally, we could always embrace unicode and go with something like:
Think of the bar in the unicode as the queue. |
Honestly, I would be unhappy if we actually took that approach. That approach definitely is trying to be nice to newcomers and really mean to existing users of Julia. |
@omus, e.g. ↦ is |
Ok, I lied: I have one more thing to say. As a more serious suggestion is we could go with this approach. It loses the nice push/pop but is far clearer as to the operation going on:
|
Feel free to down vote most of these suggestions. After doing some digging on this topic the only true conclusion I came to is that computer scientists embraced push/pop terminology and had no idea what to name shift/unshift. |
Now that we have constant folding, keyword arguments for this should be fast, correct? I don't understand why we would have 4 functions for this instead of 2, especially since multi-word functions are specifically discouraged to promote refactoring. |
Entertaining as this is, it seems that we cannot do better than the status quo here. |
The amount of support for the OP seems substantial? Perhaps closing this issue is a bit premature? :) |
Yes, the OP's suggestion has 11 up and 0 down... |
I interpreted those as support for the general notion of renaming, not for that specific suggestion. |
It's clear to me that after all this discussion we didn't find a really compelling alternative. FWIW |
I like |
Well, I made a PR in #25100 to use Feel free to shoot it down ;-) |
Similar to what @Sacha0 mentioned in #25100. I think if we really want to make a rename I suggest going with: enqueue!(::typeof(first), a, item) = unshift!(a, item)
enqueue!(::typeof(last), a, item) = push!(a, item)
dequeue!(::typeof(first), a) = shift!(a)
dequeue!(::typeof(last), a) = pop!(a) julia> enqueue!(::typeof(first), a, item) = unshift!(a, item);
julia> enqueue!(::typeof(last), a, item) = push!(a, item);
julia> dequeue!(::typeof(first), a) = shift!(a);
julia> dequeue!(::typeof(last), a) = pop!(a);
julia> foo = [1, 2, 3]
3-element Array{Int64,1}:
1
2
3
julia> enqueue!(first, foo, 0)
4-element Array{Int64,1}:
0
1
2
3
julia> dequeue!(first, foo)
0
julia> enqueue!(last, foo, 5)
4-element Array{Int64,1}:
1
2
3
5
julia> dequeue!(last, foo)
5 |
Alternatively: push!(::typeof(first), a, item) = unshift!(a, item)
push!(::typeof(last), a, item) = push!(a, item)
pop!(::typeof(first), a) = shift!(a)
pop!(::typeof(last), a) = pop!(a) We could also keep |
As mentioned on slack a few days ago,
unshift!
may be inherited, but it's a reasonably poor name IMHO. As pre-1.0 is the time for fixing these things, how about it?Since
prepend
is taken, how aboutunshift!
->pushfront!
andshift!
->popfront!
?The text was updated successfully, but these errors were encountered: