Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Add delete_first to Array #2433

wants to merge 1 commit into from

Conversation

dsounded
Copy link

@dsounded dsounded commented Apr 8, 2016

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"]

@jhass
Copy link
Member

jhass commented Apr 8, 2016

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*.
Copy link
Member

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

Copy link
Author

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

@asterite
Copy link
Member

asterite commented Apr 8, 2016

I can also see this being useful, but do we need both options? I think having a max optional argument should be enough. In which case you want to delete all elements except an arbitrary number of items?

Also, could you show us your use case?

@dsounded
Copy link
Author

dsounded commented Apr 8, 2016

@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 only comes in pair with except

@asterite
Copy link
Member

asterite commented Apr 8, 2016

@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.

@dsounded
Copy link
Author

dsounded commented Apr 8, 2016

@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

@dsounded
Copy link
Author

dsounded commented Apr 9, 2016

@asterite I am trying to move my ws-server from Rails to Kemal(Crystal framework) and I've got to do that Redis stuff, I can describe it more accurately if you want in a PM(though PR isn't a right place for me).

By the way anyone can monkey-patch whatever he wants with open classes but the community will curse him off for this, because monkey patching isn't a good practice, but I do agree that standard library shouldn't be messy 👍

@dsounded
Copy link
Author

@asterite can this be merged, or at least max option ?

@asterite
Copy link
Member

@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.

@asterite
Copy link
Member

I think instead of this we could add a delete_first(obj) or delete_one(obj) method. I'm not sure deleting arbitrary n values matching a given object by equality is something common, and it can be programmed as an outer function/method.

@dsounded
Copy link
Author

dsounded commented Jul 4, 2016

@asterite maybe delete_exactly(obj, count) or delete_at_most or sth like that ?

@bcardiff
Copy link
Member

bcardiff commented Jul 4, 2016

I would say that delete_first(obj) sounds good to be added to the std. We can add just that.

If we return the index of the deleted object as the result and if we have delete_first(obj, from), implementing an efficient delete-N is trivial. I wouldn't add anything further to the std.

@dsounded dsounded changed the title Add :except and :only option to Array#delete Add delete_first to Array Jul 9, 2016
@dsounded
Copy link
Author

dsounded commented Jul 9, 2016

@bcardiff @asterite I've updated it, I am not sure about from option, but we can discuss it

@epergo
Copy link
Contributor

epergo commented Mar 3, 2018

@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 😃

@dsounded
Copy link
Author

dsounded commented Mar 3, 2018

@epergo No, but I am not sure if guys from core team will merge it

@asterite
Copy link
Member

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 Array in some utility shard .

@asterite asterite closed this Apr 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants