-
Notifications
You must be signed in to change notification settings - Fork 21
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
List tryRemove function #1093
Comments
Note that both Map and Set have functions called just |
I didn't realise that. That makes the issue of naming easy to handle, although some care should be made in documentation since Set guarantees uniqueness of elements whereas in this case only the first element will be removed, not all of them, since to remove all you could just use |
But why specifically the first element? What are the use cases? |
I've needed a function like this many times. Here is one such example. In that case, the list represents a sequence of items that needs to be exhausted, and so having used one, it must be removed. This is because the order in the list isn't really important. However, ultimately the reason to remove the first is because the goal is to remove an element by its value rather than its index (which already exists), and that seems like the most sensible behaviour for such a function (noting again that removing all occurrences can easily be done using |
Ah, using a list like a stack. |
What about |
Not quite like a stack, because in a stack you wouldn't look through to remove a certain element. Only like a stack if the list is in the correct order. The only issue with |
I think that just calling it |
@Tarmil The signatures of those functions return one element or option of one element. |
Yes, agreed. The naming inconsistency in an issue here. Maybe |
List tryRemove function
My proposal is for a function in the List module (and a similar function for the other collections) to remove the first instance of a specified element in a list, which falls back gracefully when the element isn't found by returning the original list.
Sample code
Currently, I believe the neatest implementation of this when writing F# code is to write a function like the above, as opposed to using standard library functions such as
tryFindIndex
andremoveAt
, hence the suggestion for it to exist as its own function.Considerations
Some consideration should be given to:
try
prefix return option type, which the above does not, but it doesn't do so without throwing an exception either.Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: