-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add delete_first to Array #2433
Conversation
I can see the usefulness but I'm not too confident about the naming, it could easily be confused as "except this specific object" or "only this specific object". But then the only alternatives I could come up with, keep and max, are not really better. |
def delete(obj) | ||
reject! { |e| e == obj } != nil | ||
# | ||
# :only option removes only specified number of items from `self` that are equal to *obj*. |
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.
They're really not symbols, use *only*
, see http://crystal-lang.org/docs/conventions/documenting_code.html
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.
@jhass ty will check this out
I can also see this being useful, but do we need both options? I think having a Also, could you show us your use case? |
@asterite It's based on the task: maybe someone will need to take care that some objects will be in the array only needed times(1 for the most cases, but probably the other situation will come as well) Plus, there is a common practice that |
@dsounded But do you need it in some project of yours? I'm hesitant to add methods that don't come from a real use case. In any case, others can reopen Array and add this overload, and eventually if many need it we can consider adding it to the standard library. |
@asterite I've got similar situation but with Redis: control count of connection of one user to the other users channels and track if he is active now |
@asterite I am trying to move my ws-server from By the way anyone can monkey-patch whatever he wants with |
@asterite can this be merged, or at least |
@dsounded No, it's not clear that this should be added to the standard library. You can always add such functionality in your projects, either by reopening Array or adding a class method to a module/class. |
I think instead of this we could add a |
@asterite maybe |
I would say that If we return the index of the deleted object as the result and if we have |
@dsounded Is anything left to be done in this PR? It seems it is finished, is a small change so if everyone agrees, I think this could be merged 😃 |
@epergo No, but I am not sure if guys from core team will merge it |
Given the so many 👎 for this PR, we are going to close them. Thank you for brining this issue, though! The workaround is a simple one-liner: a = [1, 2, 1, 2, 3]
# delete first "2"
a.index(2).try { |i| a.delete_at(i) } This could be extracted to a helper method, or added to |
a = ["a", "b", "b", "b", "c"]
a.delete_first("b") => "b"
a # => ["a", "b", "b", "c"]
a = ["a", "b", "b", "b", "c"]
a.delete_first("d") => nil
a # => ["a", "b", "b", "b", "c"]